find the list with the minimum size over a list of list












5















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?










share|improve this question

























  • What's wrong with a simple if-else?

    – Nicholas K
    Nov 20 '18 at 16:59













  • @NicholasK i call the getTheMinList many times with different size arguments, what i wrote was just a tiny example.

    – zak zak
    Nov 20 '18 at 17:41
















5















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?










share|improve this question

























  • What's wrong with a simple if-else?

    – Nicholas K
    Nov 20 '18 at 16:59













  • @NicholasK i call the getTheMinList many times with different size arguments, what i wrote was just a tiny example.

    – zak zak
    Nov 20 '18 at 17:41














5












5








5








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?










share|improve this question
















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?







java java-stream






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 19:56









Stefan Zobel

2,44031828




2,44031828










asked Nov 20 '18 at 16:57









zak zakzak zak

496214




496214













  • What's wrong with a simple if-else?

    – Nicholas K
    Nov 20 '18 at 16:59













  • @NicholasK i call the getTheMinList many times with different size arguments, what i wrote was just a tiny example.

    – zak zak
    Nov 20 '18 at 17:41



















  • What's wrong with a simple if-else?

    – Nicholas K
    Nov 20 '18 at 16:59













  • @NicholasK i call the getTheMinList many times with different size arguments, what i wrote was just a tiny example.

    – zak zak
    Nov 20 '18 at 17:41

















What's wrong with a simple if-else?

– Nicholas K
Nov 20 '18 at 16:59







What's wrong with a simple if-else?

– Nicholas K
Nov 20 '18 at 16:59















@NicholasK i call the getTheMinList many times with different size arguments, what i wrote was just a tiny example.

– zak zak
Nov 20 '18 at 17:41





@NicholasK i call the getTheMinList many times with different size arguments, what i wrote was just a tiny example.

– zak zak
Nov 20 '18 at 17:41












2 Answers
2






active

oldest

votes


















4














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());





share|improve this answer





















  • 1





    Another suggestion: Try programming to an interface.

    – nullpointer
    Nov 20 '18 at 17:09











  • Thank you, i use the var arg because i call the getTheMinList many times with different size arguments.

    – zak zak
    Nov 20 '18 at 17:40











  • @zakzak List<List<Integer>> can as well accept varying size outer List as an argument if you'd noticed. This means you can call it as getTheMinList(List.of(List.of(0, 2, 3), List.of(0, 2, 3, 4))) and getTheMinList(List.of(List.of(0, 2, 3), List.of(0, 2, 3, 4), List.of(1,2,3))) as well.

    – nullpointer
    Nov 20 '18 at 17:42













  • yes but i should send a list of list as argument every time, in my program i send many list as argument, like getTheMinList(l1,l2) ,getTheMinList(l1,l2,l3) ans so on...and i m using only java 8.

    – zak zak
    Nov 20 '18 at 17:43








  • 1





    @zakzak the return type of min is Optional<T> for which you either check for an Optional.empty or can fallback to a default value using Optional.orElse API.

    – nullpointer
    Nov 20 '18 at 18:11



















2














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.






share|improve this answer
























  • thank you, i will review my code.in general every list should be not null before the call.

    – zak zak
    Nov 20 '18 at 17:47






  • 1





    When you stay with varargs, you can still use this solution as simple as return Collections.min(Arrays.asList(lists), Comparator.comparingInt(List::size)); This uses Arrays.asList which creates a lightweight wrapper around the array, allowing to apply the entirety of the Collection API on an existing array…

    – Holger
    Nov 21 '18 at 8:54











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397893%2ffind-the-list-with-the-minimum-size-over-a-list-of-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














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());





share|improve this answer





















  • 1





    Another suggestion: Try programming to an interface.

    – nullpointer
    Nov 20 '18 at 17:09











  • Thank you, i use the var arg because i call the getTheMinList many times with different size arguments.

    – zak zak
    Nov 20 '18 at 17:40











  • @zakzak List<List<Integer>> can as well accept varying size outer List as an argument if you'd noticed. This means you can call it as getTheMinList(List.of(List.of(0, 2, 3), List.of(0, 2, 3, 4))) and getTheMinList(List.of(List.of(0, 2, 3), List.of(0, 2, 3, 4), List.of(1,2,3))) as well.

    – nullpointer
    Nov 20 '18 at 17:42













  • yes but i should send a list of list as argument every time, in my program i send many list as argument, like getTheMinList(l1,l2) ,getTheMinList(l1,l2,l3) ans so on...and i m using only java 8.

    – zak zak
    Nov 20 '18 at 17:43








  • 1





    @zakzak the return type of min is Optional<T> for which you either check for an Optional.empty or can fallback to a default value using Optional.orElse API.

    – nullpointer
    Nov 20 '18 at 18:11
















4














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());





share|improve this answer





















  • 1





    Another suggestion: Try programming to an interface.

    – nullpointer
    Nov 20 '18 at 17:09











  • Thank you, i use the var arg because i call the getTheMinList many times with different size arguments.

    – zak zak
    Nov 20 '18 at 17:40











  • @zakzak List<List<Integer>> can as well accept varying size outer List as an argument if you'd noticed. This means you can call it as getTheMinList(List.of(List.of(0, 2, 3), List.of(0, 2, 3, 4))) and getTheMinList(List.of(List.of(0, 2, 3), List.of(0, 2, 3, 4), List.of(1,2,3))) as well.

    – nullpointer
    Nov 20 '18 at 17:42













  • yes but i should send a list of list as argument every time, in my program i send many list as argument, like getTheMinList(l1,l2) ,getTheMinList(l1,l2,l3) ans so on...and i m using only java 8.

    – zak zak
    Nov 20 '18 at 17:43








  • 1





    @zakzak the return type of min is Optional<T> for which you either check for an Optional.empty or can fallback to a default value using Optional.orElse API.

    – nullpointer
    Nov 20 '18 at 18:11














4












4








4







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());





share|improve this answer















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());






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 17:08

























answered Nov 20 '18 at 17:03









nullpointernullpointer

46.5k1199191




46.5k1199191








  • 1





    Another suggestion: Try programming to an interface.

    – nullpointer
    Nov 20 '18 at 17:09











  • Thank you, i use the var arg because i call the getTheMinList many times with different size arguments.

    – zak zak
    Nov 20 '18 at 17:40











  • @zakzak List<List<Integer>> can as well accept varying size outer List as an argument if you'd noticed. This means you can call it as getTheMinList(List.of(List.of(0, 2, 3), List.of(0, 2, 3, 4))) and getTheMinList(List.of(List.of(0, 2, 3), List.of(0, 2, 3, 4), List.of(1,2,3))) as well.

    – nullpointer
    Nov 20 '18 at 17:42













  • yes but i should send a list of list as argument every time, in my program i send many list as argument, like getTheMinList(l1,l2) ,getTheMinList(l1,l2,l3) ans so on...and i m using only java 8.

    – zak zak
    Nov 20 '18 at 17:43








  • 1





    @zakzak the return type of min is Optional<T> for which you either check for an Optional.empty or can fallback to a default value using Optional.orElse API.

    – nullpointer
    Nov 20 '18 at 18:11














  • 1





    Another suggestion: Try programming to an interface.

    – nullpointer
    Nov 20 '18 at 17:09











  • Thank you, i use the var arg because i call the getTheMinList many times with different size arguments.

    – zak zak
    Nov 20 '18 at 17:40











  • @zakzak List<List<Integer>> can as well accept varying size outer List as an argument if you'd noticed. This means you can call it as getTheMinList(List.of(List.of(0, 2, 3), List.of(0, 2, 3, 4))) and getTheMinList(List.of(List.of(0, 2, 3), List.of(0, 2, 3, 4), List.of(1,2,3))) as well.

    – nullpointer
    Nov 20 '18 at 17:42













  • yes but i should send a list of list as argument every time, in my program i send many list as argument, like getTheMinList(l1,l2) ,getTheMinList(l1,l2,l3) ans so on...and i m using only java 8.

    – zak zak
    Nov 20 '18 at 17:43








  • 1





    @zakzak the return type of min is Optional<T> for which you either check for an Optional.empty or can fallback to a default value using Optional.orElse API.

    – nullpointer
    Nov 20 '18 at 18:11








1




1





Another suggestion: Try programming to an interface.

– nullpointer
Nov 20 '18 at 17:09





Another suggestion: Try programming to an interface.

– nullpointer
Nov 20 '18 at 17:09













Thank you, i use the var arg because i call the getTheMinList many times with different size arguments.

– zak zak
Nov 20 '18 at 17:40





Thank you, i use the var arg because i call the getTheMinList many times with different size arguments.

– zak zak
Nov 20 '18 at 17:40













@zakzak List<List<Integer>> can as well accept varying size outer List as an argument if you'd noticed. This means you can call it as getTheMinList(List.of(List.of(0, 2, 3), List.of(0, 2, 3, 4))) and getTheMinList(List.of(List.of(0, 2, 3), List.of(0, 2, 3, 4), List.of(1,2,3))) as well.

– nullpointer
Nov 20 '18 at 17:42







@zakzak List<List<Integer>> can as well accept varying size outer List as an argument if you'd noticed. This means you can call it as getTheMinList(List.of(List.of(0, 2, 3), List.of(0, 2, 3, 4))) and getTheMinList(List.of(List.of(0, 2, 3), List.of(0, 2, 3, 4), List.of(1,2,3))) as well.

– nullpointer
Nov 20 '18 at 17:42















yes but i should send a list of list as argument every time, in my program i send many list as argument, like getTheMinList(l1,l2) ,getTheMinList(l1,l2,l3) ans so on...and i m using only java 8.

– zak zak
Nov 20 '18 at 17:43







yes but i should send a list of list as argument every time, in my program i send many list as argument, like getTheMinList(l1,l2) ,getTheMinList(l1,l2,l3) ans so on...and i m using only java 8.

– zak zak
Nov 20 '18 at 17:43






1




1





@zakzak the return type of min is Optional<T> for which you either check for an Optional.empty or can fallback to a default value using Optional.orElse API.

– nullpointer
Nov 20 '18 at 18:11





@zakzak the return type of min is Optional<T> for which you either check for an Optional.empty or can fallback to a default value using Optional.orElse API.

– nullpointer
Nov 20 '18 at 18:11













2














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.






share|improve this answer
























  • thank you, i will review my code.in general every list should be not null before the call.

    – zak zak
    Nov 20 '18 at 17:47






  • 1





    When you stay with varargs, you can still use this solution as simple as return Collections.min(Arrays.asList(lists), Comparator.comparingInt(List::size)); This uses Arrays.asList which creates a lightweight wrapper around the array, allowing to apply the entirety of the Collection API on an existing array…

    – Holger
    Nov 21 '18 at 8:54
















2














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.






share|improve this answer
























  • thank you, i will review my code.in general every list should be not null before the call.

    – zak zak
    Nov 20 '18 at 17:47






  • 1





    When you stay with varargs, you can still use this solution as simple as return Collections.min(Arrays.asList(lists), Comparator.comparingInt(List::size)); This uses Arrays.asList which creates a lightweight wrapper around the array, allowing to apply the entirety of the Collection API on an existing array…

    – Holger
    Nov 21 '18 at 8:54














2












2








2







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 17:22









AomineAomine

41.9k74071




41.9k74071













  • thank you, i will review my code.in general every list should be not null before the call.

    – zak zak
    Nov 20 '18 at 17:47






  • 1





    When you stay with varargs, you can still use this solution as simple as return Collections.min(Arrays.asList(lists), Comparator.comparingInt(List::size)); This uses Arrays.asList which creates a lightweight wrapper around the array, allowing to apply the entirety of the Collection API on an existing array…

    – Holger
    Nov 21 '18 at 8:54



















  • thank you, i will review my code.in general every list should be not null before the call.

    – zak zak
    Nov 20 '18 at 17:47






  • 1





    When you stay with varargs, you can still use this solution as simple as return Collections.min(Arrays.asList(lists), Comparator.comparingInt(List::size)); This uses Arrays.asList which creates a lightweight wrapper around the array, allowing to apply the entirety of the Collection API on an existing array…

    – Holger
    Nov 21 '18 at 8:54

















thank you, i will review my code.in general every list should be not null before the call.

– zak zak
Nov 20 '18 at 17:47





thank you, i will review my code.in general every list should be not null before the call.

– zak zak
Nov 20 '18 at 17:47




1




1





When you stay with varargs, you can still use this solution as simple as return Collections.min(Arrays.asList(lists), Comparator.comparingInt(List::size)); This uses Arrays.asList which creates a lightweight wrapper around the array, allowing to apply the entirety of the Collection API on an existing array…

– Holger
Nov 21 '18 at 8:54





When you stay with varargs, you can still use this solution as simple as return Collections.min(Arrays.asList(lists), Comparator.comparingInt(List::size)); This uses Arrays.asList which creates a lightweight wrapper around the array, allowing to apply the entirety of the Collection API on an existing array…

– Holger
Nov 21 '18 at 8:54


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397893%2ffind-the-list-with-the-minimum-size-over-a-list-of-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory