Rookie
Rookie

Reputation: 8790

Put arraylist of hashmap into another

I want to put an ArrayList<HashMap<String, Object>> into another ArrayList<HashMap<String, Object>>. How can I do that?

I was trying with the assignment operator (=) but I found out that it just points to the reference, I want to put the elements into another.

Upvotes: 0

Views: 829

Answers (3)

Miquel
Miquel

Reputation: 15675

You can use the addAll method, but bear in mind that'll not duplicate the HashMaps and much less the Objects within!

Upvotes: 4

chaosguru
chaosguru

Reputation: 1983

You can create a new ArrayList Object and just call the addAll method and pass the ArrayList you want to copy

Another way is Collections.copy(desc,src) but this is not feasible since the size of both the List must be equal.

Upvotes: 1

Jason
Jason

Reputation: 4130

Try this:

List<HashMap<String, Object>> newList = new ArrayList<HashMap<String,Object>>();
for (HashMap<String, Object> hm: oldList) {
    newList.add(hm);
}

Upvotes: 1

Related Questions