0

I have the list of projects(String) and respective users which I am taking from users as an input :

Project1,User1 Project1,User2 Project1,User3 Project2,User4 Project2,User5

I would like to have this data in the format like:

Project1: User1,User2,User3 Project2:User4,User5

I am trying the same thing to put in hashmap

HashMap<String, HashSet> hsp = new HashMap<String, HashSet>();


HashSet<String> userHash = new HashSet<String>();

Can any one help me to understand how to do it? I am accepting both project name and user name from user.

1 Answer 1

1

Initialize HashMap and HashSet as follows

    HashSet<String> hashset=new HashSet<String>();
    HashSet<String> hashset2=new HashSet<String>();
    HashMap<String, HashSet> hashmap=new HashMap<String, HashSet>();

Now add elements to hashset

    hashset.add("User1");
    hashset.add("User2");
    hashset.add("User3");

    hashset2.add("User4");
    hashset2.add("User5");

Finally add HashSet to HashMap

    hashmap.put("project1", hashset);
    hashmap.put("project2", hashset2);

So your desired o/p will be as follows

    Hash map is {project2=[User4, User5], project1=[User3, User2, User1]}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.