Reputation: 301
I want to transform pairs of numbers to a range of integers so I can perform functions on them. for example each of these lines:
1-4
5-6
1-2
4-7
should be transformed to array i.e: [1,2,3,4]. my goal is to do a count on the most frequent number. i am trying to do it like the word count example, but the problem is how to create the range stream from the two numbers in each line?
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()));
Upvotes: 6
Views: 2073
Reputation: 159135
If you want to see all numbers at max frequency, it can be done like this:
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());
}
Test
System.out.println(findMaxOccurs("1-4", "5-6", "1-2", "4-7"));
Output
[1, 2, 4, 5, 6]
If you might want to know the frequency of those numbers too, it would be better to split that into two methods:
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());
}
Test
System.out.println(findMaxOccurring("1-4", "5-6", "1-2", "4-7"));
System.out.println(findMaxOccurringNumbers("1-4", "5-6", "1-2", "4-7"));
Output
2=[1, 2, 4, 5, 6]
[1, 2, 4, 5, 6]
Upvotes: 0
Reputation: 45329
The following pipeline splits each line on -
, then uses IntStream
to create a range of numbers between the two.
The result is a flatten stream of all these inner integers, followed by a counting group by (number). The max "count" is then found on the values of this map.
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);
With your example data, it prints 1=2
(1
found 2 times) - there are many values found exactly twice.
Upvotes: 4