0

I am trying to iterate over three lists of different size but not getting the exact logic of how i can retrieve data from them and store in another list. I was able to handle up to two list until I add some more filtration to the elements. For now I am using 3 for loops but i want to use Java 8 streams if possible. Can someone please suggest me the correct logic for the below iterations.

public class CustomDto {

public static void main(String... args) {

    List<String> list1 = Arrays.asList("Hello", "World!");
    List<String> list2 = Arrays.asList("Hi", "there");
    List<String> list3 = Arrays.asList("Help Me");
    Map<Integer, Object> map = new HashMap<>();

    for (int i = 0; i < list1.size(); i++) {
        List<String> list4 = new LinkedList();
        for (int j = 0; j < list2.size(); j++) {

            for (int k = 0; k < list3.size(); k++) {

                if (!(list2.get(j).equals(list3.get(k))))
                    list4.add(list2.get(j));
            }

            if (j > list4.size() - 1) {
                list4.add(null);
            }
        }
        map.put(i, list4);
    }
}

}

All i want to convert the above code into stream, in which i can iterate a list inside another list and can use the index of one another.

9
  • 1
    what is list and uniqueCombinationsList? this is really unclear at this point Commented Aug 28, 2018 at 10:23
  • Edited the code again please have a look. Commented Aug 28, 2018 at 10:29
  • 1
    how about you remove all the irrelevant code and present us a much simpler example? Commented Aug 28, 2018 at 10:29
  • 2
    to me, no. we still don't know what list2 is, can you make the same example using some Lists that contain some strings for example? Something we could easily verify in our IDE\s for example? Commented Aug 28, 2018 at 10:40
  • 1
    @Federico Peralta Schaffner I have added the comments. Commented Aug 28, 2018 at 15:39

2 Answers 2

1
public static void main(String... args) {
    List<String> list1 = Arrays.asList("Hello", "World!");
    List<String> list2 = Arrays.asList("Hi", "there");
    List<String> list3 = Arrays.asList("Help Me");

    List<String> list4 = concat(list1, list2, list3); //you can add as many lists as you like here
    System.out.println(list4);
}

private static List<String> concat(List<String>... lists) {
    return Stream.of(lists)
            .flatMap(List::stream)
            .collect(Collectors.toList());
}

Output

[Hello, World!, Hi, there, Help Me]
Sign up to request clarification or add additional context in comments.

Comments

0

Try this create a multiple dimension array out of List, from there you can use stream too

Customdto[][] listArray = new Customdto[][]{  l1.toArray(new Customdto[]{})
        ,  l2.toArray(new Customdto[]{}),  l3.toArray(new Customdto[]{})};
    int size = listArray[0].length > listArray[1].length && listArray[0].length > listArray[2].length ?listArray[0].length
            :(listArray[1].length > listArray[2].length ? listArray[1].length:listArray[2].length);
    for(int i = 0; i <size;i++)
    {
        if(listArray[0].length >i && listArray[1].length >i && listArray[2].length >i &&
                listArray[0][i].equals(listArray[1][i]) 
                && listArray[1][i].getCalendarDate().equals(listArray[2][i].getCalendarDate()))
        {
            l4.add(listArray[1][i]);
        }else
        {
            l4.add(null);
        }
    }

Tried with Below Input

List<Customdto> l1 = new ArrayList<Customdto>();
        List<Customdto> l2 = new ArrayList<Customdto>();
        List<Customdto> l3 = new ArrayList<Customdto>();
        List<Customdto> l4 = new ArrayList<Customdto>();
        l1.add(new Customdto(1));
        l1.add(new Customdto(2));
        l1.add(new Customdto(3));
        l1.add(new Customdto(4));
        l2.add(new Customdto(1));
        l2.add(new Customdto(2));
        l2.add(new Customdto(3));
        l3.add(new Customdto(1));
        l3.add(new Customdto(2));

Output is

[Customdto [id=1], Customdto [id=2], null, null]

4 Comments

Thanks for your efforts @Nishant, But i have modified the code again , also I am expecting to use streams here, and trying not to use any loops if possible.
From your new program I am not clear what you want to achieve @Vinay. Can you explain a bit more
I want to iterate 3 lists using streams and collect all the data including null values in list4
Tested your code and it just out list 2 times in map.{0=[Hi, there], 1=[Hi, there]}. I am still not clear what you want to do with your list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.