nc界面排序
In Java, SortedMap is an interface of Collection framework. SortedMap extends Map interface and also provide the ordering of the elements. Traversing the sorted elements becomes easier.
在Java中,SortedMap是Collection框架的接口。 SortedMap扩展了Map接口,还提供了元素的顺序。 遍历排序的元素变得更加容易。
下面是SortedMap接口的方法。 (Below are the methods of SortedMap Interface.)
S.no. | Methods | Description |
---|---|---|
1 | subMap(K fromKey, K toKey) | It is used to view the elements from a given range |
2 | headMap(K toKey) | It is used to get the key which is less than than the toKey. |
3 | tailMap(K fromKey) | It is used to get the key which is greater than or equal to the fromKey. |
4 | firstKey() | It is used to get the lowest key. |
5 | lastKey() | It is used to get the highest key. |
6 | comparator() | It is used to get the comparator used in the Map. |
7 | values() | It is used to get the values of the Map |
8 | keySet() | It is used to get the set view from the Map. |
9 | entrySet() | It is used to get the set view of the mapping which are contained in the map. |
序号 | 方法 | 描述 |
---|---|---|
1个 | subMap(K fromKey,K toKey) | 用于查看给定范围内的元素 |
2 | headMap(K toKey) | 它用于获取小于toKey的密钥。 |
3 | tailMap(K fromKey) | 它用于获取大于或等于fromKey的密钥。 |
4 | firstKey() | 它用于获取最低密钥。 |
5 | lastKey() | 它用于获取最高密钥。 |
6 | 比较器() | 它用于获取Map中使用的比较器。 |
7 | values() | 用于获取Map的值 |
8 | keySet() | 它用于从地图获取设置视图。 |
9 | entrySet() | 它用于获取映射中包含的映射的设置视图。 |
Example:
例:
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
public class SortedMapDemo1
{
public static void main(String[] args)
{
SortedMap<Integer, String> a = new TreeMap<Integer, String>();
a.put(new Integer(2), "Red");
a.put(new Integer(3), "Pink");
a.put(new Integer(5), "Yellow");
a.put(new Integer(4), "Black");
a.put(new Integer(1), "Green");
Set b = a.entrySet();
Iterator i = b.iterator();
while (i.hasNext())
{
Map.Entry c = (Map.Entry)i.next();
int key1 = (Integer)c.getKey();
String value1 = (String)c.getValue();
System.out.println("Key : " + key1 + " || value : " + value1);
}
}
}
翻译自: https://www.studytonight.com/java/sorted-map-interface.php
nc界面排序