Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按值顺序打印列表中的前40个元素_Java - Fatal编程技术网

Java 按值顺序打印列表中的前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

我仍然无法获得前40名,请让我向您展示我的代码:

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))}