Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I store HashMap<String, ArrayList<String>> inside a list?

Tags:

My hashmap stores the string as key and arraylist as the values. Now, I need to embed this into a list. That is, it will be of the following form:

List<HashMap<String, ArrayList<String>>> 

These are the declarations I have used:

Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>(); ArrayList<String> arraylist = new ArrayList<String>(); map.put(key,arraylist); List<String> list = new ArrayList<String>(); 

Can anyone help me which method and how to use in the list to proceed storing my map into it?

like image 773
user2376600 Avatar asked May 13 '13 06:05

user2376600


People also ask

Can I store ArrayList in HashMap?

Array List can be converted into HashMap, but the HashMap does not maintain the order of ArrayList. To maintain the order, we can use LinkedHashMap which is the implementation of HashMap.

Can we store string in HashMap?

String is as a key of the HashMap When you create a HashMap object and try to store a key-value pair in it, while storing, a hash code of the given key is calculated and its value is placed at the position represented by the resultant hash code of the key.

Can you store a list in a HashMap?

Among those, HashMap is a collection of key-value pairs that maps a unique key to a value. Also, a List holds a sequence of objects of the same type. We can put either simple values or complex objects in these data structures.


2 Answers

Always try to use interface reference in Collection, this adds more flexibility.
What is the problem with the below code?

List<Map<String,List<String>>> list = new ArrayList<Map<String,List<String>>>();//This is the final list you need Map<String, List<String>> map1 = new HashMap<String, List<String>>();//This is one instance of the  map you want to store in the above list. List<String> arraylist1 = new ArrayList<String>(); arraylist1.add("Text1");//And so on.. map1.put("key1",arraylist1); //And so on... list.add(map1);//In this way you can add. 

You can easily do it like the above.

like image 72
Shreyos Adikari Avatar answered Oct 21 '22 12:10

Shreyos Adikari


First, let me fix a little bit your declaration:

List<Map<String, List<String>>> listOfMapOfList =      new HashList<Map<String, List<String>>>(); 

Please pay attention that I used concrete class (HashMap) only once. It is important to use interface where you can to be able to change the implementation later.

Now you want to add element to the list, don't you? But the element is a map, so you have to create it:

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

Now you want to populate the map. Fortunately you can use utility that creates lists for you, otherwise you have to create list separately:

mapOfList.put("mykey", Arrays.asList("one", "two", "three")); 

OK, now we are ready to add the map into the list:

listOfMapOfList.add(mapOfList); 

BUT:

Stop creating complicated collections right now! Think about the future: you will probably have to change the internal map to something else or list to set etc. This will probably cause you to re-write significant parts of your code. Instead define class that contains you data and then add it to one-dimentional collection:

Let's call your class Student (just as example):

public Student {     private String firstName;     private String lastName;     private int studentId;      private Colectiuon<String> courseworks = Collections.emtpyList();      //constructors, getters, setters etc } 

Now you can define simple collection:

Collection<Student> students = new ArrayList<Student>(); 

If in future you want to put your students into map where key is the studentId, do it:

Map<Integer, Student> students = new HashMap<Integer, Student>(); 
like image 37
AlexR Avatar answered Oct 21 '22 14:10

AlexR