List<Map.Entry<Integer, Integer>> inflList = new ArrayList<>(inflMap.entrySet()); inflList.sort(Map.Entry.comparingByValue(Comparator.reverseOrder())); 解释这段代码
时间: 2024-04-29 11:20:08 浏览: 119
这段代码的作用是将一个 `Map<Integer, Integer>` 类型的数据 `inflMap` 转换成一个 `List<Map.Entry<Integer, Integer>>` 类型的数据 `inflList`,并且按照 `Map.Entry` 中的 value 值进行降序排列。
具体来说,首先通过 `inflMap.entrySet()` 方法将 `inflMap` 中的元素以 `Map.Entry<Integer, Integer>` 的形式放入一个 `Set` 类型的对象中,然后通过 `new ArrayList<>(...)` 将这个 `Set` 对象转换成一个 `List` 对象,这里使用了泛型,使得转换后的 `List` 中的元素类型为 `Map.Entry<Integer, Integer>`。
接下来,通过 `inflList.sort(...)` 方法对 `inflList` 进行排序,其中 `Map.Entry.comparingByValue(...)` 方法返回一个 `Comparator` 对象,用于对 `Map.Entry` 中的 value 进行比较,`Comparator.reverseOrder()` 方法返回一个逆序的比较器,表示按照 value 值进行降序排列。最终,`inflList` 中的元素按照 value 值从大到小排列。
相关问题
public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private Map<String, Integer> counts = new HashMap<>(); @Override protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable value : values) { sum += value.get(); } counts.put(key.toString(), sum); } @Override protected void cleanup(Context context) throws IOException, InterruptedException { // 对 counts 中的键值对按照要求排序 List<Map.Entry<String, Integer>> list = new ArrayList<>(counts.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { if (o1.getValue() .equals(o2.getValue()) ) { return o2.getKey().compareTo(o1.getKey()); } else { return o2.getValue().compareTo(o1.getValue()); } } }); // 将排序后的结果写入输出文件中 for (Map.Entry<String, Integer> entry : list) { Text word = new Text(entry.getKey()); IntWritable count = new IntWritable(entry.getValue()); context.write(word, count); } } }分析一下这段代码
这段代码是一个 MapReduce 的 Reducer 类,用于对输入数据进行聚合操作。它的输入键值对类型为 Text 和 IntWritable,输出键值对类型也是 Text 和 IntWritable。该类定义了一个名为 counts 的 Map 对象,用于保存每个 key 对应的总和。
在 reduce() 方法中,它遍历了所有的 values,将其相加得到了一个 sum 值,并将其保存到 counts 中。
在 cleanup() 方法中,它首先将 counts 中的键值对存入一个 ArrayList 中,并按照排序规则进行排序,排序规则是:先按照值降序排序,如果值相同则按照键升序排序。排序后,它遍历排序后的 ArrayList,将其中的键值对写入输出文件中。
这段代码的作用是对输入的数据进行聚合操作,并按照一定规则排序后输出结果。
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Map<String, List<Integer>> map = new HashMap(); int n = 0; List<String> lines = new ArrayList<>(); while (true) { String line = in.nextLine(); if (line.equals("!!!!!")) break; n++; lines.add(line); String[] words = line.split(" "); for (String word : words) { if (map.containsKey(word)) { if (map.get(word).contains(n)) continue; map.get(word).add(n); } else { List<Integer> index = new ArrayList<>(); index.add(n); map.put(word, index); } } } List<Map.Entry<String, List<Integer>>> list = new ArrayList<>(map.entrySet()); Collections.sort(list, Comparator.comparing(Map.Entry::getKey)); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } while (true) { String[] keys = in.nextLine().split(" "); List<List<Integer>> index = new ArrayList<>(); for (int i = 0; i < keys.length; i++) { for (int j = 0; j < list.size(); j++) { if (list.get(j).getKey().equals(keys[i])) { index.add(list.get(j).getValue()); } } } if (index.size() == 0) { System.out.println("found 0 results"); continue; } List<Integer> result = index.get(0); if (keys.length >= 2) { for (int i = 1; i < index.size(); i++) { result.retainAll(index.get(i)); } if (result.size()==0){ System.out.println("found 0 results"); continue; } } System.out.println(result); for (int i : result) { System.out.println("line "+i+":"+lines.get(i-1)); } } } }
这是一段 Java 代码,它实现了一个简单的文本搜索功能。用户可以输入多行文本,程序会将每个单词与所在的行数存储在一个 HashMap 中。用户可以输入多个关键词,程序会返回包含所有关键词的行号。如果没有找到任何匹配,程序会输出 "found 0 results"。如果找到了匹配的行,程序会输出行号和具体的文本内容。
阅读全文
相关推荐
















