HashMap with multiple values under the same key

2018-12-31 16:42发布

Is it possible for us to implement a HashMap with one key and two values. Just as HashMap?

Please do help me, also by telling (if there is no way) any other way to implement the storage of three values with one as the key?

标签: java
19条回答
弹指情弦暗扣
3楼-- · 2018-12-31 17:05

If you use Spring Framework. There is: org.springframework.util.MultiValueMap.

To create unmodifiable multi value map:

Map<String,List<String>> map = ...
MultiValueMap<String, String> multiValueMap = CollectionUtils.toMultiValueMap(map);

Or use org.springframework.util.LinkedMultiValueMap

查看更多
ら面具成の殇う
4楼-- · 2018-12-31 17:05

The easiest way would be to use a google collection library:

import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap;

public class Test {

public static void main(final String[] args) {

    // multimap can handle one key with a list of values
    final Multimap<String, String> cars = ArrayListMultimap.create();
    cars.put("Nissan", "Qashqai");
    cars.put("Nissan", "Juke");
    cars.put("Bmw", "M3");
    cars.put("Bmw", "330E");
    cars.put("Bmw", "X6");
    cars.put("Bmw", "X5");

    cars.get("Bmw").forEach(System.out::println);

    // It will print the:
    // M3
    // 330E
    // X6
    // X5
}

}

maven link: https://mvnrepository.com/artifact/com.google.collections/google-collections/1.0-rc2

more on this: http://tomjefferys.blogspot.be/2011/09/multimaps-google-guava.html

查看更多
明月照影归
5楼-- · 2018-12-31 17:06

I use Map<KeyType, Object[]> for associating multiple values with a key in a Map. This way, I can store multiple values of different types associated with a key. You have to take care by maintaining proper order of inserting and retrieving from Object[].

Example: Consider, we want to store Student information. Key is id, while we would like to store name, address and email associated to the student.

       //To make entry into Map
        Map<Integer, String[]> studenMap = new HashMap<Integer, String[]>();
        String[] studentInformationArray = new String[]{"name", "address", "email"};
        int studenId = 1;
        studenMap.put(studenId, studentInformationArray);

        //To retrieve values from Map
        String name = studenMap.get(studenId)[1];
        String address = studenMap.get(studenId)[2];
        String email = studenMap.get(studenId)[3];
查看更多
千与千寻千般痛.
6楼-- · 2018-12-31 17:06

I prefer the following to store any number of variables without having to create a separate class.

final public static Map<String, Map<String, Float>> myMap    = new HashMap<String, Map<String, Float>>();
查看更多
路过你的时光
7楼-- · 2018-12-31 17:08

Here is THE ANSWER : ) : )

String key= "services_servicename"

ArrayList data;

for(int i = 0; i lessthen data.size(); i++){

        HashMap<String, String> servicesNameHashmap = new HashMap<String, String>();
        servicesNameHashmap.put(key,data.get(i).getServiceName());
        mServiceNameArray.add(i,servicesNameHashmap);   }

I have got the Best Results.

You Just Have to Create New HashMap Like

HashMap servicesNameHashmap = new HashMap();

in Your For Loop. It will have Same Effect Like Same Key And Multiple Values.

Happy Coding :)

查看更多
登录 后发表回答