I have a list of list and i want to return the list that have the min size using java Stream.
Here what i tried:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Example {
public static void main( String[] args ) {
ArrayList<Integer> list1 = new ArrayList<>();
list1.add(0);list1.add(2);list1.add(3);
ArrayList<Integer> list2 = new ArrayList<>();
list2.add(0);list2.add(2);list2.add(3);list2.add(4);
System.out.println( getTheMinList(list1,list2).size());
}
public static ArrayList<Integer> getTheMinList(ArrayList<Integer>... lists) {
return Arrays.stream(lists)
.map(list->list.size())
/*here i stoped*/
;
}
}
The program should print 3
because the first list has the minimum size.
Note that I can't change the signature of the getTheMinList(). Could anyone please give me a hint?
Use Python's built-in min() function with a key argument to find the shortest string in a list. Call min(lst, key=len) to return the shortest string in lst using the built-in len() function to associate the weight of each string—the shortest string will be the minimum.
To get the length of a list in Python, you can use the built-in len() function. Apart from the len() function, you can also use a for loop and the length_hint() function to get the length of a list. In this article, I will show you how to get the length of a list in 3 different ways.
Python has got in-built method – len() to find the size of the list i.e. the length of the list. The len() method accepts an iterable as an argument and it counts and returns the number of elements present in the list.
Python's inbuilt function allows us to find it in one line, we can find the minimum in the list using the min() function and then use the index() function to find out the index of that minimum element.
You can use Stream.min
comparing the List.size
:
public static List<Integer> getTheMinList(List<Integer>... lists){
return Arrays.stream(lists)
.min(Comparator.comparingInt(List::size))
.orElse(new ArrayList<>());
}
or possibly better if you could get rid of var-args and unchecked assignment
private static List<Integer> getTheMinList(List<List<Integer>> lists){
return lists.stream()
.min(Comparator.comparingInt(List::size))
.orElse(new ArrayList<>());
}
// invoked as
System.out.println(getTheMinList(List.of(list1, list2)).size());
Just another idea...
as @NullPointer suggested, I’d make the method take a List<List<Integer>>
rather than the varargs.
Given this change you can simply do:
Collections.min(lists, Comparator.comparingInt(List::size));
Short, simple and readable right?
Note that the above will throw a NoSuchElementException
upon the collection being empty which may aswell be the expected behaviour you’re wanting but just in case that’s not the the intended behaviour you may want to check for emptiness before the call to min or use the stream approach suggested by @NullPointer which uses the Optional API to return an alternative value if Stream#min returns an empty Optional.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With