Java 按值顺序打印列表中的前40个元素
我仍然无法获得前40名,请让我向您展示我的代码:Java 按值顺序打印列表中的前40个元素,java,Java,我仍然无法获得前40名,请让我向您展示我的代码: Map<String, Integer> doubleCount= new HashMap<>(); SortedMap<String,Integer> newMap= new TreeMap<>(doubleCount); Map<String,Integer> newDouble40 = newMap.headMap("40"); System.out.println(newDou
Map<String, Integer> doubleCount= new HashMap<>();
SortedMap<String,Integer> newMap= new TreeMap<>(doubleCount);
Map<String,Integer> newDouble40 = newMap.headMap("40");
System.out.println(newDouble40);
您将获得一个按值排序的hashMap
但是如何仅打印前40页???试试:
int i = 0;
for(Map.Entry<K,V> entry: sortByValues(doubleCount)){
System.out.println(entry.getKey() + ": " + entry.getValue());
if (++i == 40) break;
}
inti=0;
for(Map.Entry:sortByValue(doubleCount)){
System.out.println(entry.getKey()+“:”+entry.getValue());
如果(++i==40)中断;
}
?你不能使用吗
"public Collection<V> values()"
“公共集合值()”
函数获取所有值,然后使用
for(int i=0; i<40; i++){System.out.println(valuelist.get(i))}
for(int i=0;i
"public Collection<V> values()"
for(int i=0; i<40; i++){System.out.println(valuelist.get(i))}