admin管理员组

文章数量:1032831

SortedMap接口介绍

Java中的SortedMap是一个接口,是Map接口的子接口。映射中的条目根据其键按升序排序进行维护。

简而言之,它保证了条目按升序键顺序维护。它允许非常有效地操作地图的子集。

SortedMap 是在 Java 1.2 版本中添加的。它存在于java.util.SortedMap包中。

SortedMap接口的层次结构


SortedMap接口扩展Map接口。TreeMap 和 ConcurrentSkipListMap 类实现了 SortedMap 接口。java 中 SortedMap 接口的层次结构图如下图所示。


SortedMap 是一个通用接口,声明如下所示:

代码语言:javascript代码运行次数:0运行复制
interface SortedMap<K,V>
     extends Map<K,V>

此处,K 表示此映射维护的键的类型,V 表示映射值的类型。

Java 中的SortedMap方法


除了 Map 接口提供的方法外,SortedMap 还提供了几种其他方法,如下所示:

1.  Comparator comparator():此方法返回用于对此映射中的键进行排序的比较器。如果映射使用其键的自然顺序,则返回 null。

2. Set<Map.Entry<K,V>> entrySet():此方法返回此映射中包含的键值对(映射)的集合视图。

3. Set<K> keySet():它返回此映射中包含的键的集合视图。

4. K firstKey():它返回调用映射中的第一个(最低或最小)键。

5. K lastKey():它返回调用映射中的最后一个(最高或最大)键。

6. SortedMap<K,V> headMap(K toKey):此方法返回映射中键严格小于toKey的一部分。

7. SortedMap<K,V> tailMap(K fromKey):此方法返回键大于或等于fromKey的映射部分。

8. SortedMap<K,V> subMap(K fromKey, K toKey):此方法返回映射的一部分,其键范围从 fromKey(包)到 toKey(不含)。

从映射接口继承的方法

clear(), containsKey(), containsValue(), equals(), get(), hashCode(), isEmpty(), put(), putAll(), remove(), size()

如果调用映射中没有条目,则有几个方法会引发一个名为 NoSuchElementException 的异常。当对象与映射中的元素不兼容时,将引发 ClassCastException。

如果在映射中不允许使用 null 时尝试使用 null 对象,则会引发名为 NullPointerException 的运行时异常。

SortedMap示例程序


让我们举一个示例程序,这些程序基于 Java 中 SortedMap 接口定义的方法执行各种操作。

由于Java SortedMap是一个接口,它只能与实现SortedMap接口的类一起使用。Java 中的 TreeMap 类实现了 SortedMap 接口。

1. 添加和删除元素:让我们创建一个程序,我们将分别使用 put() 和 remove() 方法在 SortedMap 中添加和删除元素。但是,插入顺序不会保留在TreeMap 中。

在内部,对于每个元素,键都按照升序进行比较和排序。查看以下源代码以更好地理解。

程序源代码 1:

代码语言:javascript代码运行次数:0运行复制
import java.util.SortedMap;
import java.util.TreeMap;
public class SortedMapEx {	
public static void main(String[] args)
{	
// Create a SortedMap.
  SortedMap<Integer, String> smap = new TreeMap<>();
  
// Adding entries in TreeMap.
   smap.put(10, "Ten");
   smap.put(20, "Twenty");
   smap.put(05, "Five");
   smap.put(07, "Seven");
   smap.put(40, "Forty");
   
System.out.println("Entries in map: " +smap);
  
// Removing an entry.
   Object removeEntry = smap.remove(05);
   System.out.println("Removed entry: " +removeEntry);
   System.out.println("Updated entries in map after remove operation: " +smap);
   
 }
}
代码语言:javascript代码运行次数:0运行复制
Output:
      Entries in map: {5=Five, 7=Seven, 10=Ten, 20=Twenty, 40=Forty}
      Removed entry: Five
      Updated entries in map after remove operation: {7=Seven, 10=Ten, 20=Twenty, 40=Forty}

2. 更新条目:添加条目后,如果要更新条目,可以通过使用 put() 方法再次添加条目来完成。

由于 SortedMap 中的条目是使用键编制索引的,因此只需插入要更改的键的更新值即可更新或更改键的值。

程序源代码 2:

代码语言:javascript代码运行次数:0运行复制
import java.util.SortedMap;
import java.util.TreeMap;
public class SortedMapEx2 {	
public static void main(String[] args)
{	
// Create a SortedMap.
  SortedMap<Integer, String> smap = new TreeMap<>();
  
   smap.put(06, "Six");
   smap.put(20, "Twenty");
   smap.put(05, "Fie"); // Here, an entry is wrong.
   smap.put(07, "Seven");
   smap.put(40, "Forty");
   
System.out.println("Entries in map: " +smap.entrySet());
  
// Updating the spelling of wrong entry.
   smap.put(05, "Five");
   System.out.println("Updated entries in map: " +smap.entrySet());
   System.out.println("SubMap: " +smap.subMap(10, 45));
 }
}
代码语言:javascript代码运行次数:0运行复制
Output:
      Entries in map: [5=Fie, 6=Six, 7=Seven, 20=Twenty, 40=Forty]
      Updated entries in map: [5=Five, 6=Six, 7=Seven, 20=Twenty, 40=Forty]
      SubMap: {20=Twenty, 40=Forty}

3. 让我们再举一个示例程序,基于 headMap()、tailMap()、firstKey() 和 lastKey() 方法执行各种操作。查看以下源代码以更好地理解。

程序源代码 3:

代码语言:javascript代码运行次数:0运行复制
import java.util.SortedMap;
import java.util.TreeMap;
public class SortedMapEx3 {	
public static void main(String[] args)
{	
// Create a SortedMap.
  SortedMap<Integer, String> smap = new TreeMap<Integer,String>();
  
// Adding entries in TreeMap.
   smap.put(90, "John");
   smap.put(85, "Deep");
   smap.put(100, "Sophia");
   smap.put(35, "Olivea");
   smap.put(39, "Shubh");
   
System.out.println("Marks in maths: " +smap); 
System.out.println("Students that passed maths exam: " +smap.tailMap(40));
System.out.println("Students that failed in maths exam: " +smap.headMap(40));

System.out.println("Lowest marks: " +smap.firstKey());
System.out.println("Highest marks: " +smap.lastKey());
 }
}
代码语言:javascript代码运行次数:0运行复制
Output:
      Marks in maths: {35=Olivea, 39=Shubh, 85=Deep, 90=John, 100=Sophia}
      Students that passed maths exam: {85=Deep, 90=John, 100=Sophia}
      Students that failed in maths exam: {35=Olivea, 39=Shubh}
      Lowest marks: 35
      Highest marks: 100
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2009-04-20,如有侵权请联系 cloudcommunity@tencent 删除java程序集合接口排序

SortedMap接口介绍

Java中的SortedMap是一个接口,是Map接口的子接口。映射中的条目根据其键按升序排序进行维护。

简而言之,它保证了条目按升序键顺序维护。它允许非常有效地操作地图的子集。

SortedMap 是在 Java 1.2 版本中添加的。它存在于java.util.SortedMap包中。

SortedMap接口的层次结构


SortedMap接口扩展Map接口。TreeMap 和 ConcurrentSkipListMap 类实现了 SortedMap 接口。java 中 SortedMap 接口的层次结构图如下图所示。


SortedMap 是一个通用接口,声明如下所示:

代码语言:javascript代码运行次数:0运行复制
interface SortedMap<K,V>
     extends Map<K,V>

此处,K 表示此映射维护的键的类型,V 表示映射值的类型。

Java 中的SortedMap方法


除了 Map 接口提供的方法外,SortedMap 还提供了几种其他方法,如下所示:

1.  Comparator comparator():此方法返回用于对此映射中的键进行排序的比较器。如果映射使用其键的自然顺序,则返回 null。

2. Set<Map.Entry<K,V>> entrySet():此方法返回此映射中包含的键值对(映射)的集合视图。

3. Set<K> keySet():它返回此映射中包含的键的集合视图。

4. K firstKey():它返回调用映射中的第一个(最低或最小)键。

5. K lastKey():它返回调用映射中的最后一个(最高或最大)键。

6. SortedMap<K,V> headMap(K toKey):此方法返回映射中键严格小于toKey的一部分。

7. SortedMap<K,V> tailMap(K fromKey):此方法返回键大于或等于fromKey的映射部分。

8. SortedMap<K,V> subMap(K fromKey, K toKey):此方法返回映射的一部分,其键范围从 fromKey(包)到 toKey(不含)。

从映射接口继承的方法

clear(), containsKey(), containsValue(), equals(), get(), hashCode(), isEmpty(), put(), putAll(), remove(), size()

如果调用映射中没有条目,则有几个方法会引发一个名为 NoSuchElementException 的异常。当对象与映射中的元素不兼容时,将引发 ClassCastException。

如果在映射中不允许使用 null 时尝试使用 null 对象,则会引发名为 NullPointerException 的运行时异常。

SortedMap示例程序


让我们举一个示例程序,这些程序基于 Java 中 SortedMap 接口定义的方法执行各种操作。

由于Java SortedMap是一个接口,它只能与实现SortedMap接口的类一起使用。Java 中的 TreeMap 类实现了 SortedMap 接口。

1. 添加和删除元素:让我们创建一个程序,我们将分别使用 put() 和 remove() 方法在 SortedMap 中添加和删除元素。但是,插入顺序不会保留在TreeMap 中。

在内部,对于每个元素,键都按照升序进行比较和排序。查看以下源代码以更好地理解。

程序源代码 1:

代码语言:javascript代码运行次数:0运行复制
import java.util.SortedMap;
import java.util.TreeMap;
public class SortedMapEx {	
public static void main(String[] args)
{	
// Create a SortedMap.
  SortedMap<Integer, String> smap = new TreeMap<>();
  
// Adding entries in TreeMap.
   smap.put(10, "Ten");
   smap.put(20, "Twenty");
   smap.put(05, "Five");
   smap.put(07, "Seven");
   smap.put(40, "Forty");
   
System.out.println("Entries in map: " +smap);
  
// Removing an entry.
   Object removeEntry = smap.remove(05);
   System.out.println("Removed entry: " +removeEntry);
   System.out.println("Updated entries in map after remove operation: " +smap);
   
 }
}
代码语言:javascript代码运行次数:0运行复制
Output:
      Entries in map: {5=Five, 7=Seven, 10=Ten, 20=Twenty, 40=Forty}
      Removed entry: Five
      Updated entries in map after remove operation: {7=Seven, 10=Ten, 20=Twenty, 40=Forty}

2. 更新条目:添加条目后,如果要更新条目,可以通过使用 put() 方法再次添加条目来完成。

由于 SortedMap 中的条目是使用键编制索引的,因此只需插入要更改的键的更新值即可更新或更改键的值。

程序源代码 2:

代码语言:javascript代码运行次数:0运行复制
import java.util.SortedMap;
import java.util.TreeMap;
public class SortedMapEx2 {	
public static void main(String[] args)
{	
// Create a SortedMap.
  SortedMap<Integer, String> smap = new TreeMap<>();
  
   smap.put(06, "Six");
   smap.put(20, "Twenty");
   smap.put(05, "Fie"); // Here, an entry is wrong.
   smap.put(07, "Seven");
   smap.put(40, "Forty");
   
System.out.println("Entries in map: " +smap.entrySet());
  
// Updating the spelling of wrong entry.
   smap.put(05, "Five");
   System.out.println("Updated entries in map: " +smap.entrySet());
   System.out.println("SubMap: " +smap.subMap(10, 45));
 }
}
代码语言:javascript代码运行次数:0运行复制
Output:
      Entries in map: [5=Fie, 6=Six, 7=Seven, 20=Twenty, 40=Forty]
      Updated entries in map: [5=Five, 6=Six, 7=Seven, 20=Twenty, 40=Forty]
      SubMap: {20=Twenty, 40=Forty}

3. 让我们再举一个示例程序,基于 headMap()、tailMap()、firstKey() 和 lastKey() 方法执行各种操作。查看以下源代码以更好地理解。

程序源代码 3:

代码语言:javascript代码运行次数:0运行复制
import java.util.SortedMap;
import java.util.TreeMap;
public class SortedMapEx3 {	
public static void main(String[] args)
{	
// Create a SortedMap.
  SortedMap<Integer, String> smap = new TreeMap<Integer,String>();
  
// Adding entries in TreeMap.
   smap.put(90, "John");
   smap.put(85, "Deep");
   smap.put(100, "Sophia");
   smap.put(35, "Olivea");
   smap.put(39, "Shubh");
   
System.out.println("Marks in maths: " +smap); 
System.out.println("Students that passed maths exam: " +smap.tailMap(40));
System.out.println("Students that failed in maths exam: " +smap.headMap(40));

System.out.println("Lowest marks: " +smap.firstKey());
System.out.println("Highest marks: " +smap.lastKey());
 }
}
代码语言:javascript代码运行次数:0运行复制
Output:
      Marks in maths: {35=Olivea, 39=Shubh, 85=Deep, 90=John, 100=Sophia}
      Students that passed maths exam: {85=Deep, 90=John, 100=Sophia}
      Students that failed in maths exam: {35=Olivea, 39=Shubh}
      Lowest marks: 35
      Highest marks: 100
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2009-04-20,如有侵权请联系 cloudcommunity@tencent 删除java程序集合接口排序

本文标签: SortedMap接口介绍