简体   繁体   中英

can i use from jdk, Map 1key to multi value?

i want use map have multi value

ex)

HashMap<String, ArrayList<String>> 

but it seems too bad

may be jdk have map, this role?

thank so read this question

It is perfectly valid to define the mutimap the way you have done. JDK does not prvoide a collection called multimap. Althtough apache commons has defined a MultiMap interface with implentation classes such as MultiHashMap and MultiValueMap .

If you don't want to use apache commons MultiMap then here is the tutorial to help you learn how you can create your own multimap using java HashMap:

http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html

Try Map

Map<String, ArrayList<String>> sampleMap = new HashMap<String, ArrayList<String>>();

        ArrayList<String> sampleList = new ArrayList<String>();
        sampleList.add("Value1");
        sampleList.add("Value2");
        sampleList.add("Value3");

        sampleMap.put("Key1", sampleList);
        sampleMap.put("Key2", sampleList);
        sampleMap.put("Key3", sampleList);

        System.out.println("SampleMap : "+sampleMap);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM