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

JJ72
  • 62
  • 9
  • 4
    Why not a Map> ? – Stefan Nov 05 '15 at 09: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>` 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.). – Thomas Nov 05 '15 at 09:11
  • Ok... I was on the impression that it could be done with values() and for-loops... – JJ72 Nov 05 '15 at 09:13
  • Possible duplicate of [HashMap with multiple values under the same key](http://stackoverflow.com/questions/4956844/hashmap-with-multiple-values-under-the-same-key) – Jiri Tousek Nov 05 '15 at 09:13
  • No, `values()` allows you to iterate over the values in the map but you'd still have only one value per key. – Thomas Nov 05 '15 at 09:13

2 Answers2

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);
}
Stefan
  • 1,433
  • 1
  • 19
  • 30
  • 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? – JJ72 Nov 05 '15 at 09:21
  • 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. – Stefan Nov 05 '15 at 09:22
  • cool. Thank you very much. You guys are awsome. Now i gotta read some more about muliple maps, hehe. – JJ72 Nov 05 '15 at 09:26
  • 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 :-( – JJ72 Nov 05 '15 at 10:43
  • Do you mean printing the values in the list that you stored in your map? – Stefan Nov 05 '15 at 11:41
  • Well, here is the problem. I want to print out ONLY the names from the users input "input: Dalton, output: the 2 Dalton-names" ,but I only manage to output the whole list. Thank you very much for your time :-) – JJ72 Nov 05 '15 at 12:02
  • I have added an example to do this in my answer. – Stefan Nov 05 '15 at 12:06
  • Aaaaahh.... List toas = wordkey.get(name); That was the missing link. I've been on this for days now and you helped me. I'm really thankfull. Now I think I understand it too :-) – JJ72 Nov 05 '15 at 13:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94336/discussion-between-joakim-and-stefan). – JJ72 Nov 05 '15 at 13:24
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

ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
  • I'd prefer Guava's [Multimap](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multimap.html) over Apache Commons since it supports generics and seems to have more implementation choices. But that's just my personal opinion. :) – Thomas Nov 05 '15 at 09:15
  • 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. – Stefan Nov 05 '15 at 09:17
  • you're totally right! i'll include it into my answer if that's ok for you ;) – ParkerHalo Nov 05 '15 at 09:17
  • 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)? – JJ72 Nov 05 '15 at 09:17
  • @Joakim i'm sorry i don't really get what you mean ;) could you clarify with a example please? – ParkerHalo Nov 05 '15 at 09:18