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
2 Answers 2
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);
}
9 Comments
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.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
5 Comments
Map
class and to override those method, when the same thing can be achieved in a simpler and more maintainable way.
"Dalton"
?) so you'll have to use another solution. One would be to useMap<String, List<ToA>>
as Stefan suggested, another would be to use a GuavaMultimap
which basically is the same but with an easier to use API (e.g. it handles creating/deleting the lists for your etc.).values()
allows you to iterate over the values in the map but you'd still have only one value per key.