我想将数字对转换为整数范围,以便可以对它们执行功能。 例如以下各行:
1-4
5-6
1-2
4-7
应转换为数组,即[1,2,3,4]。 我的目标是对最频繁的号码进行计数。 我正在尝试像单词计数示例一样进行操作,但是问题是如何从每行中的两个数字创建范围流?
Path path = Paths.get(args[0]);
Map<String, Long> wordCount = Files.lines(path)
.flatMap(line -> Arrays.stream(line.trim().split("-")))
.
.map(word -> word.replaceAll("[^a-zA-Z]", "").toLowerCase().trim())
.filter(num -> num.length() > 0)
.map(number -> new SimpleEntry<>(number, 1))
.collect(Collectors.groupingBy(SimpleEntry::getKey, Collectors.counting()));
答案 0 :(得分:4)
以下管道在-
上拆分每一行,然后使用IntStream
在两者之间创建一个数字范围。
结果是所有这些内部整数的平坦流,后跟一个计数组(数字)。然后,在此地图的值上找到最大“计数”。
String s = "1-4\n" + "5-6\n" + "1-2\n" + "4-7"; //simpler version with inline text
Optional<Entry<Integer, Long>> result =
Stream.of(s.split("\n")) //replace with Files.lines(path) for real stream
.map(line -> line.split("-"))
.map(array -> new int[] { Integer.parseInt(array[0].trim()),
Integer.parseInt(array[1].trim()) })
.map(array -> IntStream.rangeClosed(array[0], array[1]))
.flatMapToInt(Function.identity())
.boxed()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.stream()
.max(Comparator.comparingLong(Entry::getValue));
result.ifPresent(System.out::println);
使用示例数据,它会打印1=2
(1
被发现2次)-许多值恰好被发现两次。
答案 1 :(得分:0)
如果要以最大频率查看所有数字,可以这样做:
private static List<Integer> findMaxOccurs(String... ranges) {
return Optional
.ofNullable(
Arrays.stream(ranges)
.map(r -> r.split("-"))
.flatMap(r -> IntStream.rangeClosed(Integer.parseInt(r[0]),
Integer.parseInt(r[1]))
.boxed())
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
// We now have Map<Integer, Long> mapping Number to Frequency
.entrySet()
.stream()
.collect(Collectors.groupingBy(Entry::getValue, TreeMap::new,
Collectors.mapping(Entry::getKey, Collectors.toList())))
// We now have TreeMap<Long, List<Integer>> mapping Frequency to Numbers
.lastEntry()
)
.map(Entry::getValue)
.orElse(Collections.emptyList());
}
测试
System.out.println(findMaxOccurs("1-4", "5-6", "1-2", "4-7"));
输出
[1, 2, 4, 5, 6]
如果您也想知道这些数字的频率,最好将其分为两种方法:
private static Entry<Long, List<Integer>> findMaxOccurring(String... ranges) {
return Arrays.stream(ranges)
.map(r -> r.split("-"))
.flatMap(r -> IntStream.rangeClosed(Integer.parseInt(r[0]),
Integer.parseInt(r[1])).boxed())
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
// We now have Map<Integer, Long> mapping Number to Frequency
.entrySet()
.stream()
.collect(Collectors.groupingBy(Entry::getValue, TreeMap::new,
Collectors.mapping(Entry::getKey, Collectors.toList())))
// We now have TreeMap<Long, List<Integer>> mapping Frequency to Numbers
.lastEntry();
}
private static List<Integer> findMaxOccurringNumbers(String... ranges) {
return Optional.ofNullable(findMaxOccurring(ranges))
.map(Entry::getValue)
.orElse(Collections.emptyList());
}
测试
System.out.println(findMaxOccurring("1-4", "5-6", "1-2", "4-7"));
System.out.println(findMaxOccurringNumbers("1-4", "5-6", "1-2", "4-7"));
输出
2=[1, 2, 4, 5, 6]
[1, 2, 4, 5, 6]