This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

iterating a hashmap  RSS feed

 
micha fretz
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi out there. i read a textfile, fiddle with it and store values into a hashmap: HashMap<String, String[]> metadata = new HashMap<String, String[]>();
what i want is every combination from the input-file-data.
let's say the file is:
generic a=1,3
generic b=2,4

then output should be
a=1, b=2; a=1, b=4; a=3, b=2; a=3, b=4. i don't know how many keys the map will contain, so it should be generic somehow.

by now my output is:
data 1
data 2
data 3
test a
test b
rums 12
rums 13
found 3 generics
test: a
test: b
data: 1
data: 2
data: 3
rums: 12
rums: 13

so just displaying the values as they came in... could someone please help me getting the right code for getting all combinations?

thanks!

 
Carey Brown
Bartender
Posts: 2985
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably HashMap is the wrong tool for the job. A HashMap is designed to provide quick random access when given a key. Since you are processing the entries in order you will: a) not get the entries in the order that they were read, b) not use the HashMap strengths, and c) probably incur performance penalties.

I might suggest loading the keys into an ArrayList<String> and the values into an ArrayList<ArrayList<String>>. When adding a key to the first list be sure to add a value ArrayList entry to the second ArrayList in order to keep them in sync, even if the value ArrayList is empty.

In regards to outputting every permutation, that will require nested loops.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

What I couldn't understand when I saw this thread last night, is how you manage to get it to print "data" when "data" doesn't appear in your code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!