Java中的NavigableMap接口及示例

这个 导航地图 接口是 集合框架 .它属于 JAVAutil 它是 分类地图 它提供了方便的导航方法,如lowerKey、floorKey、ceilingKey和higherKey,以及这种流行的导航方法。它还提供了从Java中现有地图创建子地图的方法,例如关键点小于指定关键点的headMap、关键点大于指定关键点的tailMap,以及严格包含toKey和fromKey之间的关键点的subMap。 实现NavigableMap的一个示例类是 树图 .

null

宣言:

public interface NavigableMap<K,V> extends SortedMap<K,V>

在这里 K 是键对象类型和 五、 是值对象类型。

导航地图的层次结构

NavigableMap Interface in Java with Example

它实现了 Map , 分类地图 接口。 ConcurrentNavigableMap 扩展导航地图。 ConcurrentSkipListMap 树图 实现导航地图。

例子:

JAVA

// Java program to demonstrate
// the NavigableMap interface
import java.util.NavigableMap;
import java.util.TreeMap;
public class NavigableMapExample {
public static void main(String[] args)
{
// Instantiate an object
// Since NavigableMap
// is an interface so we
// use TreeMap
NavigableMap<String, Integer> nm
= new TreeMap<String, Integer>();
// Add elements using put() method
nm.put( "C" , 888 );
nm.put( "Y" , 999 );
nm.put( "A" , 444 );
nm.put( "T" , 555 );
nm.put( "B" , 666 );
nm.put( "A" , 555 );
// Print the contents on the console
System.out.println( "Mappings of NavigableMap : "
+ nm);
System.out.printf( "Descending Set  : %s%n" ,
nm.descendingKeySet());
System.out.printf( "Floor Entry  : %s%n" ,
nm.floorEntry( "L" ));
System.out.printf( "First Entry  : %s%n" ,
nm.firstEntry());
System.out.printf( "Last Key : %s%n" , nm.lastKey());
System.out.printf( "First Key : %s%n" ,
nm.firstKey());
System.out.printf( "Original Map : %s%n" , nm);
System.out.printf( "Reverse Map : %s%n" ,
nm.descendingMap());
}
}


输出:

Mappings of NavigableMap : {A=555, B=666, C=888, T=555, Y=999}Descending Set  : [Y, T, C, B, A]Floor Entry  : C=888First Entry  : A=555Last Key : YFirst Key : AOriginal Map : {A=555, B=666, C=888, T=555, Y=999}Reverse Map : {Y=999, T=555, C=888, B=666, A=555}

实现类

NavigableMap有两个实现类,分别是 ConcurrentSkipListMap 树图 . 树图 是一个基于红黑树的NavigableMap实现,它根据其键的自然顺序或 比较器 在地图创建时提供,具体取决于使用的构造函数。TreeMap的预期时间成本为 日志(n) 用于插入、删除和访问操作。树映射是不同步的,必须在外部完成。

语法:

NavigableMap<K, V> objectName = new TreeMap<K, V>();

NavigableMap上的基本操作

1.添加元素

要向NavigableMap添加元素,我们可以使用Map界面的任何方法。下面的代码显示了如何使用它们。您可以在代码中看到,插入顺序没有保留。如果施工时未提供比较器,则遵循自然顺序。

JAVA

// Java program for adding elements
// to a NavigableMap
import java.util.*;
class AddingElementsExample {
public static void main(String args[])
{
// Instantiate an object
// Since NavigableMap is an interface
// We use TreeMap
NavigableMap<Integer, String> nmap
= new TreeMap<Integer, String>();
// Add elements using put()
nmap.put( 3 , "Geeks" );
nmap.put( 2 , "For" );
nmap.put( 1 , "Geeks" );
// Print the contents on the console
System.out.println( "Mappings of NavigableMap : "
+ nmap);
}
}


输出:

Mappings of NavigableMap : {1=Geeks, 2=For, 3=Geeks}

2.删除元素

为了移除元素,我们使用Map接口的方法,因为NavigableMap是Map的后代。我们可以使用 删除() 方法,该方法获取键值并从该树状图中移除键的映射(如果该映射存在于映射中)。我们可以使用 清除() 删除地图的所有元素。

JAVA

// Java Program for deleting
// elements from NavigableMap
import java.util.*;
class RemovingElementsExample {
public static void main(String args[])
{
// Instantiate an object
// Since NavigableMap
// is an interface
// We use TreeMap
NavigableMap<Integer, String> nmap
= new TreeMap<Integer, String>();
// Add elements using put()
nmap.put( 3 , "Geeks" );
nmap.put( 2 , "Geeks" );
nmap.put( 1 , "Geeks" );
nmap.put( 4 , "For" );
// Print the contents on the console
System.out.println( "Mappings of NavigableMap : "
+ nmap);
// Remove elements using remove()
nmap.remove( 4 );
// Print the contents on the console
System.out.println(
"NavigableMap, after remove operation : "
+ nmap);
// Clear the entire map using clear()
nmap.clear();
System.out.println(
"NavigableMap, after clear operation : "
+ nmap);
}
}


输出:

Mappings of NavigableMap : {1=Geeks, 2=Geeks, 3=Geeks, 4=For}NavigableMap, after remove operation : {1=Geeks, 2=Geeks, 3=Geeks}NavigableMap, after clear operation : {}

3.访问元素

我们可以使用get()方法访问NavigableMap的元素,下面给出了一个例子。

JAVA

// Java Program for accessing
// elements in a NavigableMap
import java.util.*;
public class AccessingElementsExample {
public static void main(String[] args)
{
// Instantiate an object
// Since NavigableMap is an interface
// We use TreeMap
NavigableMap<Integer, String> nmap
= new TreeMap<Integer, String>();
// Add elements using put()
nmap.put( 8 , "Third" );
nmap.put( 6 , "Second" );
nmap.put( 3 , "First" );
nmap.put( 11 , "Fourth" );
// Accessing the elements using get()
// with key as a parameter
System.out.println(nmap.get( 3 ));
System.out.println(nmap.get( 6 ));
System.out.println(nmap.get( 8 ));
System.out.println(nmap.get( 11 ));
// Display the set of keys using keySet()
System.out.println( "The NavigableMap key set: "
+ nmap.keySet());
}
}


输出:

FirstSecondThirdFourthThe NavigableMap key set: [3, 6, 8, 11]

4.穿越

我们可以使用 迭代器接口 遍历集合框架的任何结构。因为迭代器处理一种类型的数据,所以我们使用Entry将这两种不同的类型解析为兼容的格式。然后使用next()方法打印NavigableMap的元素。另一个著名的方法是使用 每人 把钥匙拿回来。键的值是通过使用getValue()方法找到的。

JAVA

// Java Program for traversing
// a NavigableMap
import java.util.*;
class TraversalExample {
public static void main(String args[])
{
// Instantiate an object
// Since NavigableMap is an interface
// We use TreeMap
NavigableMap<Integer, String> nmap
= new TreeMap<Integer, String>();
// Add elements using put()
nmap.put( 3 , "Geeks" );
nmap.put( 2 , "For" );
nmap.put( 1 , "Geeks" );
// Create an Iterator over the
// NavigableMap
Iterator<NavigableMap.Entry<Integer, String> > itr
= nmap.entrySet().iterator();
System.out.println( "Traversing using Iterator: " );
// The hasNext() method is used to check if there is
// a next element The next() method is used to
// retrieve the next element
while (itr.hasNext()) {
NavigableMap.Entry<Integer, String> entry
= itr.next();
System.out.println( "Key = " + entry.getKey()
+ ", Value = "
+ entry.getValue());
}
System.out.println( "Traversing using for-each: " );
// Iterate using for-each loop
for (Map.Entry mapElement : nmap.entrySet()) {
// get the key using getKey()
int key = ( int )mapElement.getKey();
// Finding the value
String value = (String)mapElement.getValue();
System.out.println( "Key = " + key
+ ", Value = " + value);
}
}
}


输出:

Traversing using Iterator:  Key = 1, Value = GeeksKey = 2, Value = ForKey = 3, Value = GeeksTraversing using for-each:  Key = 1, Value = GeeksKey = 2, Value = ForKey = 3, Value = Geeks

注: 每次我们说“NavigableMap的元素”,都必须注意,这些元素实际上存储在一个实现类的对象中,在本例中是TreeMap。

导航地图的方法

NavigableMap继承了 地图 界面 分类地图 界面父接口给出了添加元素、删除元素和遍历的基本方法。下表给出了导航地图的方法。在这里

  • K –地图中键的类型。
  • 五、 –映射到映射中的值的类型。

方法

描述

ceilingEntry(K键) 返回与大于或等于给定键的最小键关联的键值映射,如果没有这样的键,则返回null。
天花板键(K键) 返回大于或等于给定密钥的最小密钥,如果没有此类密钥,则返回null。
下降键集() 返回此映射中包含的键的反序NavigableSet视图。
下降地图() 返回此映射中包含的映射的逆序视图。
firstEntry() 返回与此映射中最小键关联的键值映射,如果映射为空,则返回null。
楼面租金(K键) 返回与小于或等于给定键的最大键关联的键值映射,如果没有这样的键,则返回null。
地板(K键) 返回小于或等于给定密钥的最大密钥,如果没有此类密钥,则返回null。
头像图(K toKey) 返回此地图中键严格小于toKey的部分的视图。
头像图(K toKey,含布尔值) 返回此映射中键小于(或等于,如果inclusive为true)toKey的部分的视图。
higherEntry(K键) 返回与严格大于给定键的最小键关联的键值映射,如果没有这样的键,则返回null。
高键(K键) 返回严格大于给定密钥的最小密钥,如果没有此类密钥,则返回null。
lastEntry() 返回与此映射中最大键关联的键值映射,如果映射为空,则返回null。
洛伦特里(K键) 返回与严格小于给定键的最大键关联的键值映射,如果没有这样的键,则返回null。
洛尔基(K键) 返回严格小于给定密钥的最大密钥,如果没有此类密钥,则返回null。
navigableKeySet() 返回此映射中包含的键的NavigableSet视图。
pollFirstEntry() 移除并返回与此映射中最小键关联的键值映射,如果映射为空,则返回null。
pollLastEntry() 移除并返回与此映射中最大键关联的键值映射,如果映射为空,则返回null。

子映射(K-fromKey,boolean)

from inclusive,K toKey,boolean to inclusive)

返回此地图中关键帧范围从fromKey到toKey的部分的视图。
子贴图(K fromKey,K toKey) 返回此地图部分的视图,其关键帧范围为fromKey,inclusive,toKey,exclusive。
尾图(K fromKey) 返回此映射中键大于或等于fromKey的部分的视图。
tailMap(K fromKey,包含布尔值) 返回此映射中键大于(或等于,如果inclusive为true)fromKey的部分的视图。

方法继承自java接口。util。分类地图

方法

描述

比较器() 返回用于在此映射中对键进行排序的比较器,如果此映射使用其键的自然排序,则返回null。
入口集() 返回此映射中包含的映射的集合视图。
firstKey() 返回此映射中当前的第一个(最低)键。
键集() 返回此映射中包含的键的集合视图。
lastKey() 返回此映射中当前的最后一个(最高)键。
价值观() 返回此映射中包含的值的集合视图。

方法继承自java接口。util。地图

方法

描述

清除() 从此映射中删除所有映射(可选操作)。

计算(K键,双功能

五、扩展V>重新映射功能)

尝试计算指定键及其当前映射值的映射(如果没有当前映射,则为null)。

computeIfAbsent(K键,函数

扩展V>映射功能)

如果指定的键尚未与值关联(或映射为null),则尝试使用给定的映射函数计算其值,并将其输入该映射,除非为null。
computeIfPresent(K键,双功能重新映射功能) 如果指定键的值存在且非空,则会尝试在给定键及其当前映射值的情况下计算新映射。
containsKey(对象键) 如果此映射包含指定键的映射,则返回true。
containsValue(对象值) 如果此映射将一个或多个键映射到指定值,则返回true。
等于(对象o) 将指定的对象与此映射进行相等性比较。
forEach(双消费者行动) 对该映射中的每个条目执行给定操作,直到所有条目都已处理或该操作引发异常为止。
获取(对象键) 返回指定键映射到的值,如果此映射不包含该键的映射,则返回null。
getOrDefault(对象键,V默认值) 返回指定键映射到的值,如果此映射不包含该键的映射,则返回defaultValue。
hashCode() 返回此映射的哈希代码值。
isEmpty() 如果此映射不包含键值映射,则返回true。
合并(K键、V值、双函数重新映射函数) 如果指定的键尚未与值关联或与null关联,则将其与给定的非null值关联。
输入(K键,V值) 将指定值与此映射中的指定键关联(可选操作)。
putAll(地图m) 将指定映射的所有映射复制到此映射(可选操作)。
putIfAbsent(K键,V值) 如果指定的键尚未与值关联(或映射为null),则将其与给定值关联并返回null,否则返回当前值。
移除(对象键) 从该映射中删除密钥的映射(可选操作)。
删除(对象键、对象值) 仅当指定项当前映射到指定值时,才删除该项。
更换(K键,V值) 仅当指定项当前映射到某个值时,才替换该项。
替换(K键,V旧值,V新值) 仅当当前映射到指定值时,才替换指定键的项。
replaceAll(双功能功能) 将每个条目的值替换为对该条目调用给定函数的结果,直到处理完所有条目或函数引发异常为止。
大小() 返回此映射中的键值映射数。

参考: https://docs.oracle.com/javase/8/docs/api/java/util/NavigableMap.html 本文由 普拉蒂克·阿加瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享