Map排序,Stream流

import java.util.*;

import java.util.Scanner;

import java.util.stream.Collectors;

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int n = in.nextInt();

int manger = in.nextInt();

Map<String,Integer> map = new LinkedHashMap<>();

for(int i=0; i<n; i++){

map.put(in.next(),in.nextInt());

}

Map<String,Integer> map_new;

if(manger == 0){

map_new = map.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).collect(

Collectors.toMap(

Map.Entry :: getKey,

Map.Entry :: getValue,

(oldVal, newVal) ->oldVal,

LinkedHashMap::new

)

);

} else {

map_new = map.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.<String,Integer>comparingByValue()).reversed()).collect(

Collectors.toMap(

Map.Entry :: getKey,

Map.Entry :: getValue,

(oldVal, newVal) ->oldVal,

LinkedHashMap::new

)

);

}

map_new.forEach((k,v) -> System.out.println(k + " " + v));

}

}

全部评论

相关推荐

嵌入式小辣鸡:包装好一点,校内的奖项可以不用写,校内项目经历最后两点写的太差了,详细讲一下内容,名字变一下。只需要写项目实现了什么,自己在其中做了什么就好,查看图片
点赞 评论 收藏
分享
04-28 11:34
西北大学 运营
牛客4396号:不好意思,这个照片猛一看像丁真
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务