Java 8 - 从Stream Object转换为java.util.Map

时间:2022-06-19 15:48:30

The workspace and project instance objects are associated with Rally object which hold in List object.

工作空间和项目实例对象与保存在List对象中的Rally对象相关联。

class Rally {
    Workspace workspace;

    public Workspace getWorkspace() {
        return workspace;
    }

    public void setWorkspace(Workspace workspace) {
        this.workspace = workspace;
    }

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    Project project;

    Rally(String name, Workspace workspace, Project project) {
        this.workspace = workspace;
        this.project = project;

    }
}

class Workspace {

    public Workspace(String id) {
        this.id = id;
    }

    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

class Project {
    private String id;

    public Project(String id) {
        super();
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

}

How to get workspace id and project id to another HashMap?

如何将工作区标识和项目标识获取到另一个HashMap?

rallyInList.stream().collect(Collectors.toMap(r->r.getWorkspace().getId(),b->b.getProject().getId()));

Java 8  - 从Stream Object转换为java.util.Map

1 个解决方案

#1


1  

Your stream pipeline is basically correct.

您的流管道基本上是正确的。

You just need to assign the result to a Map. Assuming getId() methods return a String:

您只需将结果分配给Map。假设getId()方法返回一个String:

Map<String,String> map =
  rallyInList.stream()
             .collect(Collectors.toMap(r->r.getWorkspace().getId(),
                                       r->r.getProject().getId(),
                                       (v1,v2)->v2));

Also make sure all the methods you are using actually exist (for example, it should be getId(), not getID()).

还要确保您使用的所有方法实际存在(例如,它应该是getId(),而不是getID())。

EDIT:

it is causing the issue while runtime. illegal state exception. Normally if we add duplicate key in hashmap, it will allow and update value alone.

它在运行时导致问题。非法国家例外。通常,如果我们在hashmap中添加重复键,它将仅允许和更新值。

The variant of Collectors.toMap you are using doesn't allow duplicate keys. You have to use the variant that requires a merge function.

您正在使用的Collectors.toMap变体不允许重复键。您必须使用需要合并功能的变体。

#1


1  

Your stream pipeline is basically correct.

您的流管道基本上是正确的。

You just need to assign the result to a Map. Assuming getId() methods return a String:

您只需将结果分配给Map。假设getId()方法返回一个String:

Map<String,String> map =
  rallyInList.stream()
             .collect(Collectors.toMap(r->r.getWorkspace().getId(),
                                       r->r.getProject().getId(),
                                       (v1,v2)->v2));

Also make sure all the methods you are using actually exist (for example, it should be getId(), not getID()).

还要确保您使用的所有方法实际存在(例如,它应该是getId(),而不是getID())。

EDIT:

it is causing the issue while runtime. illegal state exception. Normally if we add duplicate key in hashmap, it will allow and update value alone.

它在运行时导致问题。非法国家例外。通常,如果我们在hashmap中添加重复键,它将仅允许和更新值。

The variant of Collectors.toMap you are using doesn't allow duplicate keys. You have to use the variant that requires a merge function.

您正在使用的Collectors.toMap变体不允许重复键。您必须使用需要合并功能的变体。