How to store more than one string in a Map?
You're in object denial. You should use an object that holds the number, name, address, phone (maybe you could call it ContactInformation
) and put that into the map.
No. a Map
has only one key. If you want your value to contain more information, wrap the strings in a new class:
public class PersonalInfo {
private final String name;
private final String address;
private final String phone;
// constructor and getters
}
map.put(number, new PersonalInfo(name, address, phone));
The 'correct' solution is to use an object that holds the values in named fields, but in the spirit of answering the question asked, a simple (if unclean) solution would be to use:
Map<String,List<String>> yourMap = new HashMap<String,List<String>>();
List<String> info = new ArrayList<String>();
info.add(number);
info.add(name);
info.add(address);
info.add(phone);
yourMap.put(key, info);
Note google-collections has a series of classes that implement this structure right out of the box called ListMultimap and it's implementation ArrayListMultimap