import java.util.*;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.function.Function;
import java.util.stream.Collectors;
public class testMain {
public static class Person {
int age;
String name;
Person (int age,String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static void main(String[] args) {
ConcurrentSkipListMap<Integer, Person> skipListMap = new ConcurrentSkipListMap<>();
skipListMap.put(11,new Person(11,"mike"));
skipListMap.put(12,new Person(22,"lynn") );
skipListMap.put(13,new Person(33,"john"));
skipListMap.put(14,new Person(44,"mickey"));
skipListMap.put(15,new Person(55,"fiona") );
NavigableSet<Integer> descendingSet = skipListMap.descendingKeySet();
//遍历排好序的集合
Iterator<Integer> descendingIterator = descendingSet.iterator();
while(descendingIterator.hasNext()){
Integer key = Integer.valueOf(String.valueOf(descendingIterator.next()));
System.out.println(key + " : " + skipListMap.get(key).getName()
+ " : " +skipListMap.get(key).getAge()
+":"+skipListMap.get(key).getClass());
}
}
}
15 : fiona : 55:class testMain$Person
14 : mickey : 44:class testMain$Person
13 : john : 33:class testMain$Person
12 : lynn : 22:class testMain$Person
11 : mike : 11:class testMain$Person
参考文献:https://deepinout.com/java/java-collection/g_treemap-descendingkeyset-method-in-java-with-examples.html
开发工具:IntelliJ IDEA 2022.2.1 (Ultimate Edition)