使用Java8Stream和Lambda,我想从列表中形成一个特定的列表。详情如下

使用Java8Stream和Lambda,我想从列表中形成一个特定的列表。详情如下,lambda,grouping,java-stream,Lambda,Grouping,Java Stream,假设我有一个类似的列表 { {1-a12-abc,firstname,john}, {2-a12-abc,firstname,tom}, {1-a12-abc,lastname,doe}, {3-a22-abc,city,Delhi} } 我们可以认为每个列表都包含一些PersonId,属性,以及属性值。我希望每个Id,属性列表和相应的值列表一起显示 使用Java 8流API,我希望得到如下输出: { {{1-a12-abc},{firstnam

假设我有一个类似的列表

{
   {1-a12-abc,firstname,john},
   {2-a12-abc,firstname,tom},
   {1-a12-abc,lastname,doe},
   {3-a22-abc,city,Delhi}
}
我们可以认为每个列表都包含一些
PersonId
属性
,以及
属性值
。我希望每个
Id
,属性列表和相应的值列表一起显示

使用Java 8流API,我希望得到如下输出:

    {
        {{1-a12-abc},{firstname,lastname},{john,doe}},
        {{2-a12-abc},{firstname},{tom}},
        {{3-a22-abc},{city},{Delhi}}
    }
其中分组将与上面给出的每个列表的第一个元素一起进行。每个组的第二和第三个元素将形成一个列表


请帮助解决这个问题

这是一个使用java 8流的版本:

// Initial list
public class AttributeValue {
    String id;
    String attribute;
    String attributeValue;

    AttributeValue(String id, String attribute, String attributeValue) {
        this.id = id;
        this.attribute = attribute;
        this.attributeValue = attributeValue;
    }

    @Override
    public String toString() {
        return "{" + id + "," + attribute + "," + attributeValue + "}";
    }

    // ommiting equals() and hashCodes() methods for simplification
}

// final list
public class Record {
    String id;
    List<String> attributes;
    List<String> attributeValues;

    Record(String id, List<String> attributes, List<String> attributeValues) {
        this.id = id;
        this.attributes = attributes;
        this.attributeValues = attributeValues;
    }

    @Override
    public String toString() {
        return "{{" + id + "},"
                + attributes.stream().collect(Collectors.joining(",", "{", "}")) + ","
                + attributeValues.stream().collect(Collectors.joining(",", "{", "}")) + "}";
    }

    // ommiting equals() and hashCodes() methods for simplification
}

private List<Record> transform(List<AttributeValue> values) {
    return values.stream()
            .collect(Collectors.groupingBy(value -> value.id, LinkedHashMap::new, Collectors.toList()))
            .entrySet().stream()
            .map(pair -> new Record(
                pair.getKey(),
                pair.getValue().stream().map(value -> value.attribute).collect(Collectors.toList()),
                pair.getValue().stream().map(value -> value.attributeValue).collect(Collectors.toList())
            ))
            .collect(Collectors.toList());
}
//初始列表
公共类属性值{
字符串id;
字符串属性;
字符串属性值;
AttributeValue(字符串id、字符串属性、字符串属性值){
this.id=id;
this.attribute=属性;
this.attributeValue=attributeValue;
}
@凌驾
公共字符串toString(){
返回“{”+id+”、“+attribute+”、“+attributeValue+”}”;
}
//ommiting equals()和hashCodes()方法进行简化
}
//最终名单
公开课记录{
字符串id;
列出属性;
列出属性值;
记录(字符串id、列表属性、列表属性值){
this.id=id;
this.attributes=属性;
this.attributeValues=attributeValues;
}
@凌驾
公共字符串toString(){
返回“{{+id+},”
+attributes.stream().collect(收集器.joining(“,”,“{,“}”))+“,”
+attributeValue.stream().collect(收集器.joining(“,”,“{,“}”)+“}”;
}
//ommiting equals()和hashCodes()方法进行简化
}
私有列表转换(列表值){
返回值。stream()
.collect(Collectors.groupingBy(value->value.id,LinkedHashMap::new,Collectors.toList())
.entrySet().stream()
.map(对->新记录(
pair.getKey(),
pair.getValue().stream().map(value->value.attribute).collect(Collectors.toList()),
pair.getValue().stream().map(value->value.attributeValue).collect(Collectors.toList())
))
.collect(Collectors.toList());
}
使用junit5和assertj进行流利性单元测试:

@Test
void parse_a_van_page_with_empty_json_data() throws Exception {
    List<AttributeValue> initial = Arrays.asList(
            new AttributeValue("1-a12-abc", "firstname", "john"),
            new AttributeValue("2-a12-abc", "firstname", "tom"),
            new AttributeValue("1-a12-abc", "lastname", "doe"),
            new AttributeValue("3-a22-abc", "city", "Delhi")
    );

    List<Record> expected = Arrays.asList(
            new Record("1-a12-abc", Arrays.asList("firstname", "lastname"), Arrays.asList("john", "doe")),
            new Record("2-a12-abc", Collections.singletonList("firstname"), Collections.singletonList("tom")),
            new Record("3-a22-abc", Collections.singletonList("city"), Collections.singletonList("Delhi"))
    );

    assertThat(transform(initial)).containsAll(expected);

    // Some printing
    System.out.println("initial: ");
    initial.forEach(System.out::println);

    System.out.println("expected: ");
    expected.forEach(System.out::println);
}
@测试
void parse_a_van_page_with_empty_json_data()引发异常{
List initial=Arrays.asList(
新属性值(“1-a12-abc”、“名字”、“约翰”),
新属性值(“2-a12-abc”、“名字”、“汤姆”),
新属性值(“1-a12-abc”、“姓氏”、“doe”),
新属性值(“3-a22-abc”、“城市”、“德里”)
);
预期列表=Arrays.asList(
新记录(“1-a12-abc”,Arrays.asList(“firstname”,“lastname”),Arrays.asList(“john”,“doe”),
新记录(“2-a12-abc”,Collections.singletonList(“名字”),Collections.singletonList(“tom”),
新记录(“3-a22-abc”,收藏。单音列表(“城市”),收藏。单音列表(“德里”))
);
资产(转换(初始)).containsAll(预期);
//一些印刷品
System.out.println(“首字母:”);
initial.forEach(System.out::println);
System.out.println(“预期:”);
expected.forEach(System.out::println);
}
查看
Stream.map()
Stream.collect()
收集器.groupingBy()
。当你陷入困境时,你可以回来询问更多细节。但是,不要期望有人在不展示自己的努力的情况下给你一个完整的解决方案。