Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays.asList also for maps? [duplicate]

Tags:

I have the below code:

Map<String, Map<Double, String>> map = new HashMap<>(); Map<Double,String> Amap = new HashMap<>(); map.put(getValuesTypes.FUT(), HERE); 

Instead of creating a Map first and put it at "HERE", I'm looking for a function like I could use with a List there Arrays.asList(...) so that i can just enter at "Here" ?.asMap({1.0,"A"}, {2.0,"B"})

like image 936
PowerFlower Avatar asked Nov 06 '16 13:11

PowerFlower


People also ask

Does arrays asList make a copy?

The method only creates a List wrapper upon the underlying array. Because of which, both the array and the newly created list continue to refer to the exact same elements. That is why, no elements are copied when we use asList method.

What does array asList do?

The asList() method of java. util. Arrays class is used to return a fixed-size list backed by the specified array. This method acts as a bridge between array-based and collection-based APIs, in combination with Collection.

Does Map allow duplicate values?

Duplicate keys are not allowed in a Map. Basically, Map Interface has two implementation classes HashMap and TreeMap the main difference is TreeMap maintains an order of the objects but HashMap will not. HashMap allows null values and null keys. Both HashSet and HashMap are not synchronized.

What does array asList return?

asList returns a fixed-size list that is​ backed by the specified array; the returned list is serializable and allows random access.


1 Answers

You can initialize HashMap like this.

new HashMap<Double, String>() {     {         put(1.0, "ValA");         put(2.0, "ValB");     } }; 
like image 118
nakano531 Avatar answered Oct 24 '22 22:10

nakano531