-1

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

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?

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

enter image description here

Learn Hadoop
  • 2,760
  • 8
  • 28
  • 60

1 Answers1

1

Your stream pipeline is basically correct.

You just need to assign the result to a Map. Assuming getId() methods return a 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()).

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.

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

Eran
  • 387,369
  • 54
  • 702
  • 768
  • I tried the same. But problem is eclipse is not giving auto populate the method , ie . r.getWorkpace() is not coming under suggestion. and another one is Map map is causing because of result would be Map. any idea. I know it is returning String,String – Learn Hadoop Dec 13 '17 at 09:00
  • @LearnHadoop The `Rally` class you posted is missing the `getWorkspace()` and `getProject()` methods, which you should add. The error messages are sometimes misleading. For example, I got the `Map` error message when I used `getID()` (as you did in the question) instead of the correct method name, which is `getId()`. – Eran Dec 13 '17 at 09:03
  • Just i have posted partial code. Complete code below. `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; } }` – Learn Hadoop Dec 13 '17 at 09:08
  • @LearnHadoop you should post that in the question (you can edit your question). And I added the missing getter methods and the code passed compilation for me. – Eran Dec 13 '17 at 09:10
  • Edit and updated my code. Hope it wil give some idea. – Learn Hadoop Dec 13 '17 at 09:13
  • @LearnHadoop you still have to change `getID()` to `getId()` in your stream pipeline. that was the only issue with the code you posted. – Eran Dec 13 '17 at 09:14
  • Updated the code.. Not sure.. why i can't convert into map. Seems pipeline code is fine and without any compilation issue. if i assign the result to Map ,then it is throwing compile time error – Learn Hadoop Dec 13 '17 at 09:23
  • @LearnHadoop your updated code passes compilation for me. What error are you getting? – Eran Dec 13 '17 at 09:25
  • Pasted issue in picture? can you please check?.. Eclipse is not providing getWorkspace() method – Learn Hadoop Dec 13 '17 at 09:32
  • @LearnHadoop So your problem is that auto-completion doesn't work? What if you type getWorkspace()? Do you get any error? – Eran Dec 13 '17 at 09:35
  • S. Also if add this line Map re = rallyInList.stream() .collect(Collectors.toMap(s -> s.getWorkspace().getId(), b -> b.getProject().getId())); System.out.println(re); 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. Am i correct? But i lamda it is throwing error – Learn Hadoop Dec 13 '17 at 09:42
  • @LearnHadoop So the issue is duplicate keys. You should have mentioned that before. You should add a merge function that resolves duplicates. See edit. – Eran Dec 13 '17 at 09:44
  • Duplicate issue also after dot it is not listing all methods in that instance. – Learn Hadoop Dec 13 '17 at 09:47