How to achieve this map structure <String, List <>>

I have data as below:

Key value ----- ------ car toyota car bmw car honda fruit apple fruit banana computer acer computer asus computer ibm ... 

(Each line above the data is an object with the fields "key" and "value", all in one list List<DataObject> )

I would like to build the data for Map<String, List<String>> as follows:

  "car" : ["toyota", "bmw", "honda"] "fruit" : ["apple","banana"] "computer" : ["acer","asus","ibm"] 

How to achieve a higher Map structure from data objects?

****** In addition, ******

I'm more interested in using pure JDK classes or interfaces to achieve results instead of using an external library. Any help?

+8
java data-structures multimap
source share
6 answers

I would use the guavas Multimap implementation. But this is easily done with the standard JDK.

An example of a standard JDK:

 public static void main(String[] args) { Scanner s = new Scanner( "car toyota\n" + "car bmw\n" + "car honda\n" + "fruit apple\n" + "fruit banana\n" + "computer acer\n" + "computer asus\n" + "computer ibm"); Map<String, List<String>> map = new LinkedHashMap<String, List<String>>(); while (s.hasNext()) { String key = s.next(); if (!map.containsKey(key)) map.put(key, new LinkedList<String>()); map.get(key).add(s.next()); } System.out.println(map); } 

Guava example:

 public static void main(String[] args) { Scanner s = new Scanner( "car toyota\n" + "car bmw\n" + "car honda\n" + "fruit apple\n" + "fruit banana\n" + "computer acer\n" + "computer asus\n" + "computer ibm"); Multimap<String, String> map = LinkedListMultimap.create(); while (s.hasNext()) map.put(s.next(), s.next()); System.out.println(map); } 

Output (both implementations):

 {car=[toyota, bmw, honda], fruit=[apple, banana], computer=[acer, asus, ibm]} 
+7
source share

Below snippet will help you.

  HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>(); ArrayList<String> carList = new ArrayList<String>(); carList.add("toyota"); carList.add("bmw"); carList.add("honda"); map.put("car", carList); ArrayList<String> fruitList = new ArrayList<String>(); fruitList .add("apple"); fruitList .add("banana"); map.put("fruit", fruitList ); 
+3
source share
  Map<String, List<String>> myMaps = new HashMap<String, List<String>>(); for (DataObject item : myList) { if (!myMaps.containsKey(item.getKey())) { myMaps.put(item.getKey(), new ArrayList<String>()); } myMaps.get(item.getKey()).add(item.getValue()); } 
+3
source share

Iterate over objects. For each object, get the corresponding list from the map. If null, create a new list and place it on the map. Then add the value to the list.

Or use the Guava ListMultimap , which will do it for you.

+2
source share
 Map<String, List<String>> data = new HashMap<String, List<String>>(); data.put("car", Arrays.asList("toyota", "bmw", "honda")); data.put("fruit", Arrays.asList("apple","banana")); data.put("computer", Arrays.asList("acer","asus","ibm")); 
+1
source share

Is something like this possible?

 Map<String, List<String>> dataMap = new HashMap<String, List<String>>(); 

pseudo code:

 for (String key : keys) { if (!dataMap.containsKey(key)) { dataMap.put(key, new ArrayList<String>()); } dataMap.get(key).add(getValue(key)); } 

Alternatively use Guava ListMultiMap .

0
source share

All Articles