Piotr Szczepanik :
I need to implement the method that takes a Stream and integer and returns the list of Strings. The list should be grouped according to int numbers - ordered of highest to lowest and return only numbers
I want to do it in one stream and get something like this:
public static List<String> groupFunc(Stream<Nums> nums, int pass) {
List<Object> myList = nums.filter(s-> s.getScore() >= pass).sorted(Comparator.naturalOrder(s)).collect(Collectors.toList());
}
List<Nums> nums = new ArrayList<Nums>();
nums.add(new Nums("A", 80));
nums.add(new Nums("B", 57));
nums.add(new Nums("C", 21));
groupFunc(nums.stream(), 50).forEach(System.out::println);
// should print "A","B"
Could you help me with this stream? I don't know if I should use filter+soreted. Or maybe foreach? This Comparator.naturalOrder
is just temporary.
SDJ :
The utility methods in interface Comparator
are very useful to help with these types of operations:
public static List<String> groupFunc(Stream<Nums> nums, int pass) {
return nums
.filter(s-> s.getScore() >= pass)
.sorted(Comparator.comparing(Nums::getScore).reversed())
.map(Nums::getName)
.collect(Collectors.toList());
}