1

How do I correctly instantiate a HashMap whose values are variants of List<Number> - i.e., the values could take the form of List<Long> and/or List<Double>. Here's what I've tried so far:

HashMap<String, List<Number>> foo = new HashMap<String, List<Number>>();
foo.put("Tenticles", new ArrayList<Long>());

The error message: The method put(String, List<Number>) ... is not applicable for the arguments (String, List<Long>).

I also want to be able to add a Long to a constituent List<Long> and likewise with any Double.

EDIT These answers don't show how to add a Long or a Double to one of the constituent lists.

1
  • This is going to be tricky to do if you can make it work at all, because the covariance stuff will get messy. You could totally make it work, though, with foo.put("Tenticles", new ArrayList<Number>()), though. Commented Oct 8, 2013 at 3:01

3 Answers 3

3

You can use wildcards

e.g.

Map<String, List<? extends Number>> foo = new HashMap<String, List<? extends Number>>();

If you are using Java 7, you can do the following to avoid some duplication

Map<String, List<? extends Number>> foo = new HashMap<>();

To add an item to the List

List<Long> longs= new ArrayList<>();
longs.add(10L);
foo.put("numbers", longs);
Sign up to request clarification or add additional context in comments.

3 Comments

How do I then add something to the arraylist that go inside
You can do it a number of ways. Here is an exmaple: List<Long> longs = new ArrayList<>(); longs.add(10L); List<Double> doubles = new ArrayList<>(); doubles.add(23.20); List<Number> numbers = new ArrayList<>(); numbers.addAll(longs); numbers.addAll(doubles); foo.put("numbers", numbers); I apologize for the formatting. :P
The answer now contains an example of adding elements to the list contained in the Map
2

Use a wildcard

HashMap<String, List<? extends Number>> foo = new HashMap<String, List<? extends Number>>();
LinkedList<Long> list = new LinkedList<>();
list.add(10L);
foo.put("Tenticles", list);

Your List objects now must be a sub-type of Number.

6 Comments

How do I then add something to the arraylist that go inside
@user2763361 Just keep a reference to the List and call its add method with the element you want to add. You can always retrieve the List with get(int).
It doesn't work. The method add(capture#7-of ?) in the type ... not applicable for the arguments long.
@user Please update your question with how you are trying to use it. It works fine for me.
foo.get("Tenticles").add(10L).
|
0

Here's what you need to do:-

    final HashMap<String, List<? extends Number>> foo = new HashMap<>();
    foo.put( "Tenticles", new ArrayList<Long>() );

The syntax List<? extends Number> means a list of objects where each object implements Number.

1 Comment

How do I then add something to this arraylist? It's not working :s

Your Answer

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.