Java 8流-嵌套映射到列表

时间:2018-08-13 09:12:07

标签: java list java-8 hashmap java-stream

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

我有两个清单。第一个列表中的项目必须与第二个列表进行比较,并且必须建立新的列表。

样本输入

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

输出:

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

P.S:应对照第二个列表检查第一个列表中的names

4 个答案:

答案 0 :(得分:4)

您需要这样的东西:

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

如果我理解您的问题,那么您有三个不同的对象,如下所示:

@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;
}

此示例的输出为:

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

对于注释,我正在使用Lombok

答案 1 :(得分:0)

这是您要找的吗?

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

答案 2 :(得分:0)

您的属性太笼统了,我认为您最好引入一些自定义类来代替map并满足您的要求,这是一个您问题中 input output 的简单演示为:

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

输出:

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

答案 3 :(得分:0)

您可能需要以下内容:

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

此解决方案假定以下两个输入对象和输出对象的结构:

  • 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;