import java.util.*; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { String[] urls = {"news.qq.com", "news.sina.com.cn", "news.qq.com", "news.qq.com", "game.163.com", "game.163.com", "www.huawei.com", "www.cctv.com", "3", "www.huawei.com", "www.cctv.com", "www.huawei.com", "www.cctv.com", "www.huawei.com", "www.cctv.com", "www.huawei.com", "www.cctv.com", "www.huawei.com", "3"}; HashMap<String, Integer> statistic = new HashMap<>(); for (String url : urls) { if (url.contains(".")) { if (statistic.containsKey(url)) { statistic.put(url, statistic.get(url) + 1); } else { statistic.put(url, 1); } } else { TreeMap<Integer, List<String>> collect = statistic.entrySet().stream().collect(Collectors.groupingBy(Map.Entry::getValue, () -> new TreeMap<>((o1, o2) -> o2 - o1), Collectors.collectingAndThen(Collectors.toList(), entries -> entries.stream().map(Map.Entry::getKey).sorted().collect(Collectors.toList())))); ArrayDeque<String> result = new ArrayDeque<>(); for (List<String> value : collect.values()) { for (String s : value) { result.offerLast(s); if (result.size() == Integer.parseInt(url)) { break; } } if (result.size() == Integer.parseInt(url)) { break; } } System.out.println(result.stream().collect(Collectors.joining(","))); } } } }
标签:www,Collectors,url,OD,huawei,华为,statistic,机试,com From: https://www.cnblogs.com/kitor/p/17625880.html