Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maps with multiple types of values in java

Tags:

java

types

key

map

I have to accomplish a strange peculiar scenario. Its described as follows:

I have to design a Map where the 'Keys' are always of String type. However, the 'Value' for one of the key may be a String or a List(depends on the number of values a particular key can have. 'Value' will be a String if that particular key has only one value and it has to be a List if the key contains many values). How to accomplish this scenario?

For Example: there are 2 keys in a map namely "Name" and "Phone Nos". A person can have only one name and multiple phone numbers. So here the first key i.e. "Name" should have 'String' type for Value, whereas for the second key i.e. "Phone Nos" should have 'List' type for Value. How to declare such a Map. Is it possible?

like image 813
Surya Chandra Avatar asked Feb 13 '12 11:02

Surya Chandra


People also ask

Can a Map have multiple values Java?

Java has several implementations of the interface Map, each one with its own particularities. However, none of the existing Java core Map implementations allow a Map to handle multiple values for a single key.

How do I put multiple values on a Map?

If this is an application requirement, the three best ways to solve the 'multiple values per key in a map in Java' problem are: Stick with the standard APIs and add a collection class like a 'Vector' or 'ArrayList' to your map or set. Use the MultiMap and MultiValueMap classes from the Apache Commons library.

Can a HashMap have multiple values?

In these cases, we can use Collections such as list, set, etc. to insert multiple values into the same key in HashMap.

Can HashMap store different data types?

A HashMap stores key-value mappings. In this tutorial, we'll discuss how to store values of different types in a HashMap.


4 Answers

It is possible to do something like Map<String, Object>.

But: I would strongly suggest to think your design over. You should use a class for your persons instead. That way you could do: Map<String, Person> with Person having getters and setters for names, phone numbers and other information.

Example Person class:

public class Person {

    private String name;
    private List<String> phoneNumbers = Collections.emptyList();

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setPhoneNumbers(List<String> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }

    public void addPhoneNumber(String number) {
        phoneNumbers.add(number);
    }

    public List<String> getPhoneNumbers() {
        return phoneNumbers;
    }
}
like image 93
nfechner Avatar answered Nov 06 '22 19:11

nfechner


Well, why not make a Map<String, List<String>>? you could always just add one element to your list if there is just one value or use a common supertype of String and List, thus you would get

Map<String, Object>
like image 21
bvanvelsen Avatar answered Nov 06 '22 21:11

bvanvelsen


Of course there is. Pick your poison:

Map<String, Object> mixed = new HashMap<String, Object>();

Map<String, Serializable> mixed = new HashMap<String, Serializable>();

@SuppressWarnings("rawtypes")
Map mixed = new HashMap();
like image 32
Perception Avatar answered Nov 06 '22 21:11

Perception


I would do one of the following:

  1. Use a Multimap<String, String>. The Multimap would contain one or more values associated with each key.

  2. Use a Map<String, Either<String, List<String>>>. Use a Either to distinguish between a single or multiple values.

like image 1
John B Avatar answered Nov 06 '22 20:11

John B