1

As the question reads.... and I do NOT want to use multiple maps, just one map.

My goal is to get a list of the names I enter in the input. I have tried like a hundred different for-loops, but I always tend to end up with a list of the whole map and/or that the duplicate key is overridden.

 import java.util.*;
public class Another {
public static void main(String[] args) {
 Scanner scan = new Scanner(System.in);
 String name;
 HashMap<String, ToA>wordkey = new HashMap<String, ToA>();
 ToA a = new ToA("Doolin", "Bill", "18580824-1464");
 ToA b = new ToA("Dalton", "Bob", "18701005-2232");
 ToA c = new ToA("James", "Jesse", "18470905-2401");
 ToA d = new ToA("Dalton", "Emmet", "18710713-0818");
 wordkey.put("Doolin", a);
 wordkey.put("Dalton", b);
 wordkey.put("James", c);
 wordkey.put("Dalton", d);
 System.out.println("Efternamn:");
 name = scan.next();
}
}
 public class ToA{
 private String fname, lname, dob;
public ToA(String fname, String lname, String dob){
 this.fname = fname;
 this.lname = lname;
 this.dob = dob;
}
public String getFname(){
 return fname;
}
public String getLname(){
 return lname;
}
public String getDob(){
 return dob;
}
public String toString(){
 return "\nFirstname: " + fname + "\nSurname: " + lname + "\nDateOfBirth: " + dob;
}
}

For inputting Dalton, I would like the output Firstname: Bill Surname: Dalton DateOfBirth: 18701005-2232

Firstname: Emmet Surname: Dalton DateOfBirth: 18710713-0818

I'm really stuck with this so any help is highly appreciated, Thanks

asked Nov 5, 2015 at 9:06
5
  • 4
    Why not a Map<String, List<ToA>> ? Commented Nov 5, 2015 at 9:08
  • A normal map doesn't allow multiple values for one key (think about it: which ToA should be returned if you look for the key "Dalton"?) so you'll have to use another solution. One would be to use Map<String, List<ToA>> as Stefan suggested, another would be to use a Guava Multimap which basically is the same but with an easier to use API (e.g. it handles creating/deleting the lists for your etc.). Commented Nov 5, 2015 at 9:11
  • Ok... I was on the impression that it could be done with values() and for-loops... Commented Nov 5, 2015 at 9:13
  • Possible duplicate of HashMap with multiple values under the same key Commented Nov 5, 2015 at 9:13
  • No, values() allows you to iterate over the values in the map but you'd still have only one value per key. Commented Nov 5, 2015 at 9:13

2 Answers 2

3

To post my comment as an answer: use a Map<String, List<ToA>> like this:

Map<String, List<ToA>> wordkey = new HashMap<>();
ToA a = new ToA("Doolin", "Bill", "18580824-1464");
ToA b = new ToA("Dalton", "Bob", "18701005-2232");
ToA c = new ToA("James", "Jesse", "18470905-2401");
ToA d = new ToA("Dalton", "Emmet", "18710713-0818");
wordkey.put("Doolin", Arrays.asList(a));
wordkey.put("James", Arrays.asList(c));
wordkey.put("Dalton", Arrays.asList(b, d));

To print the names based on the input, you can do something like this:

System.out.println("Efternamn:");
name = scan.next();
List<ToA> toas = wordkey.get(name);
if (toas != null) {
 System.out.println("ToAs");
 for (ToA toa : toas) {
 System.out.println("ToA: " + toa);
 }
}
else {
 System.out.println("No ToAs found for input: " + name);
}
answered Nov 5, 2015 at 9:12

9 Comments

Well, lets say the user wants to add another bandit to this crew. He will have to input firstname, lastname and dob (I will ofcouse have to make scanners or such for this). What I was thinking was, that if you have multiple lists, is there a risk that the input info will be left out from any list?
You would do a get on your map with the entered name and you would get your list of people with that name back. Just add to that list and put it back into the map and you're done.
cool. Thank you very much. You guys are awsome. Now i gotta read some more about muliple maps, hehe.
I'm really sorry all, but I'm new to this. How am I ment to print this out? I'm just getting the whole list, again :-(
Do you mean printing the values in the list that you stored in your map?
|
1

There are several possibilities for what you are trying to achieve. A simple one would be to use Guavas Multimap or to use Apaches MultiMap.

Another possibility is to "wrap" the Map in a class and keep a List<ToA> as Value of the Map. You'd override the put, remove and get methods to what you need

answered Nov 5, 2015 at 9:13

5 Comments

I'd prefer Guava's Multimap over Apache Commons since it supports generics and seems to have more implementation choices. But that's just my personal opinion. :)
In my opinion it's rather overkill to create a custom Map class and to override those method, when the same thing can be achieved in a simpler and more maintainable way.
you're totally right! i'll include it into my answer if that's ok for you ;)
Ok, thank you very much. If I later would like to add more people to my map, which way would be the best, to prevent any info from not being added (I'm thinking if you use multiple lists)?
@Joakim i'm sorry i don't really get what you mean ;) could you clarify with a example please?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.