📜  Java集合 emptyNavigableMap() 方法和示例

📅  最后修改于: 2022-05-13 01:54:34.032000             🧑  作者: Mango

Java集合 emptyNavigableMap() 方法和示例

Java Collections emptyNavigableMap()方法用于获取没有元素的地图。 Navigable map 是一种数据结构,可以保存具有键值对的元素。

句法:

public static final  SortedMap emptyNavigableMap()  

在哪里,

  • 关键是关键元素
  • value是值元素

参数:此方法不带任何参数。

返回类型:它将返回一个不可变的空导航地图。

示例 1:

Java
// Java program to create an empty navigable map
import java.util.*;
  
public class GFG {
    // main method
    public static void main(String[] args)
    {
        // create an empty navigable map
        SortedMap data
            = Collections.emptyNavigableMap();
  
        // display
        System.out.println(data);
    }
}


Java
// Java program to show the error while using
// Collections emptyNavigableMap() Method
import java.util.*;
  
public class GFG {
    // main method
    public static void main(String[] args)
    {
        // create an empty navigable map
        SortedMap data
            = Collections.emptyNavigableMap();
        
        // add 3 elements
        data.put("1", "ojaswi");
        data.put("2", "ramya");
        data.put("3", "deepu");
  
        // display
        System.out.println(data);
    }
}


Java
// Java program to show the error while using
// Collections emptyNavigableMap() Method
import java.util.*;
  
public class GFG {
    // main method
    public static void main(String[] args)
    {
        // create an empty navigable map
        SortedMap data
            = Collections.emptyNavigableMap();
        
        // add 3 elements
        data.put(1, 34);
        data.put(2, 45);
        data.put(3, 56);
  
        // display
        System.out.println(data);
    }
}


输出
{}

示例 2:在这个程序中,我们将创建一个空的可导航地图并向地图添加元素。这将返回一个错误。

Java

// Java program to show the error while using
// Collections emptyNavigableMap() Method
import java.util.*;
  
public class GFG {
    // main method
    public static void main(String[] args)
    {
        // create an empty navigable map
        SortedMap data
            = Collections.emptyNavigableMap();
        
        // add 3 elements
        data.put("1", "ojaswi");
        data.put("2", "ramya");
        data.put("3", "deepu");
  
        // display
        System.out.println(data);
    }
}

输出:

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableMap.put(Collections.java:1459)
    at GFG.main(GFG.java:10)

示例 3:

Java

// Java program to show the error while using
// Collections emptyNavigableMap() Method
import java.util.*;
  
public class GFG {
    // main method
    public static void main(String[] args)
    {
        // create an empty navigable map
        SortedMap data
            = Collections.emptyNavigableMap();
        
        // add 3 elements
        data.put(1, 34);
        data.put(2, 45);
        data.put(3, 56);
  
        // display
        System.out.println(data);
    }
}

输出:

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableMap.put(Collections.java:1459)
    at GFG.main(GFG.java:10)