Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java multi mapping arraylist

Is it possible to map key to Multi Dimensional Array List. Some thing like following example..

Map<K,V>

Where K is key for list of alphabet and V is a multi dimensional array list or normal array list that stores list of word. Some thing like a application that reads a dictionary file. I want to see an example. Example can be anything related to Map and Multi Dimensional Array-list. Or is there any other efficient way to implement collection? I have never used such implementations so if there is already a thread related to mine QA please post the link.

like image 227
Milan Avatar asked Dec 25 '11 19:12

Milan


People also ask

Can an ArrayList have multiple data types?

It is more common to create an ArrayList of definite type such as Integer, Double, etc. But there is also a method to create ArrayLists that are capable of holding Objects of multiple Types. We will discuss how we can use the Object class to create an ArrayList. Object class is the root of the class hierarchy.

How do you add multiple ArrayLists in Java?

Approach: ArrayLists can be joined in Java with the help of Collection. addAll() method. This method is called by the destination ArrayList and the other ArrayList is passed as the parameter to this method. This method appends the second ArrayList to the end of the first ArrayList.

Can an ArrayList in Java have different data types?

The ArrayList class implements a growable array of objects. ArrayList cannot hold primitive data types such as int, double, char, and long.

Which is faster map or ArrayList?

The ArrayList has O(n) performance for every search, so for n searches its performance is O(n^2). The HashMap has O(1) performance for every search (on average), so for n searches its performance will be O(n). While the HashMap will be slower at first and take more memory, it will be faster for large values of n.


1 Answers

You can always do Map<String, <List<String>>. e.g.

Map<String, List<String>> multimap = new HashMap<String, List<String>>();
String key = "asdf";
List<String> values = Arrays.asList("foo", "bar");
multimap.put(key, values);

You can also use the Multimap<String, String> interface in Google Guava - might be a better fit for your needs. It simplifies the coding somewhat -

Multimap<String, String> multimap = new ArrayListMultimap<String, String>();
String key = "asdf";
multimap.put(key, "foo");
multimap.put(key, "bar");
like image 96
I82Much Avatar answered Oct 01 '22 07:10

I82Much