Muthu
Muthu

Reputation: 1550

Extract information from array

I am having a array like String[] selectedJobs = {"job,1","job,2", "Job2,1", "job3,1"};

I want to extract information like

Job -> 1,2

Job2 -> 1

Job3 -> 1

Map<String, List<String>> jobs = new HashMap<String, List<String>>();

and I want to store each job with corresponding list of integers in map.

How to do this ?

Upvotes: 0

Views: 90

Answers (2)

assylias
assylias

Reputation: 328618

  1. split the 1st string on "," -> "job", "1"
  2. check there is a "job" key in the map, if not create it and put a new arraylist for that key
  3. add "1" to the arraylist
  4. loop

Upvotes: 2

Miquel
Miquel

Reputation: 15675

What have you tried? The algorithm shouldn't be tricky: Go through your array, split on "," for each String, and put it in the HashMap you describe.

Upvotes: 1

Related Questions