简体   繁体   中英

Java 8 Streams - Nested Maps to List

firstlist
  .stream()
  .map( x -> { 
            return secondList
               .stream()
               .map( y -> { //return a string } )
               .collect(Collectors.toList()) // "Output" I need
              }
       )
    .//Get the "Output" here

I have two list. the item in first list have to compared with second list and new list have to built.

Sample Input

List 1 : [ { "id" ; 3, "names" : ["test","test2"] }]
List 2 : [ {"name": :"test" , "age" :3}]

Output:

List 3 : [ {"id" : 3, "name" : "test", "age" :3} ]

PS: The names in first list should be checked against second list

You need something like this :

List<ObjectA> firstlist = new ArrayList<>();
firstlist.add(new ObjectA(3, Arrays.asList("test", "test2")));
List<ObjectB> secondList = new ArrayList<>();
secondList.add(new ObjectB("test", 3));

List<ObjectC> result = firstlist.stream()
        .flatMap(a -> secondList.stream()
                .filter(b -> a.getNames().contains(b.getName()))
                .map(c -> new ObjectC(a.getId(), c.getName(), c.getAge()))
        ).collect(Collectors.toList());

If I understand your question you have three Object different like this:

@Getter @Setter @AllArgsConstructor @NoArgsConstructor
public class ObjectA {
    private int id;
    private List<String> names;
}

@Getter @Setter @AllArgsConstructor @NoArgsConstructor
public class ObjectB {
    private String name;
    private int age;
}

//And the result Object you want to get
@Getter @Setter @AllArgsConstructor @NoArgsConstructor @ToString
public class ObjectC {
    private int id;
    private String name;
    private int age;
}

The outputs of this example is :

[ObjectC(id=3, name=test, age=3)]

For the annotation I'm using Lombok for that

is it what you are looking for?

        firstList
            .stream()
            .filter(item -> secondList
                .stream().anyMatch(item::matches))
            .collect(Collectors.toList());

Your attributes are so general, I think you'd better introduce some custom class to replace the map and to meet your requirements, here is a simple demo for the input and output in your question as:

public class SimpleFlatMap {
    public static void main(String... args) {
        List<EntityWithNames> listOne = new ArrayList<>();
        listOne.add(new EntityWithNames());
        List<EntityWithAge> listTwo = new ArrayList<>();
        listTwo.add(new EntityWithAge("test", 3));
        List<EntityWithNameAndAge> listThree = listOne.stream().map(withNames -> {
            EntityWithAge entityWithAge = listTwo.stream()
                    .filter(withAge -> withNames.names.contains(withAge.name))
                    .findAny()
                    .orElse(new EntityWithAge());
            return new EntityWithNameAndAge(withNames.id, entityWithAge.name, entityWithAge.age);
        }).collect(Collectors.toList());
        System.out.println(listThree);
    }

    static class EntityWithNames {
        Long id;
        List<String> names;

        public EntityWithNames() {
            id = 3L;
            names = new ArrayList<>(Arrays.asList("test", "test1"));
        }
    }

    static class EntityWithAge {
        String name;
        int age;

        public EntityWithAge() {
            name = "default";
            age = -1;
        }

        public EntityWithAge(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }

    static class EntityWithNameAndAge {
        Long id;
        String name;
        int age;

        public EntityWithNameAndAge(Long id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        @Override
        public String toString() {
            return String.format("id: %d, name: %s, age: %d", id, name, age);
        }
    }
}

The output:

[id: 3, name: test, age: 3]

You might want the following:

List<ClassWithId>  list1 = new ArrayList<>();
List<ClassWithAge> list2 = new ArrayList<>();

list1.add(new ClassWithId(3, Arrays.asList("test", "test2")));
list2.add(new ClassWithAge("test", 4));

List<ClassResult> list3 = list2.stream()
                               .map(i -> new ClassResult(
                                               list1.stream()
                                                    .filter(j -> j.getList().contains(i.getName()))
                                                    .map(j -> j.getId())
                                                    .findFirst().get(), 
                                               i.getName(), i.getAge()))
                               .collect(Collectors.toList());

This solution assumes the structure of the following two input objects and the output object:

  • ClassWithId

     private int id; private List<String> list; 
  • ClassWithAge

     private String name; private int age; 
  • ClassResult

     private int id; private int age; private String name; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM