-1

I have a final map which is initialized in the constructor. I am adding the data to map in loop in addData() method. Is there a good way to add data to final map instead of using loop? Is there any feature in guava?

class TestClass {
 private final Map<K,v> map1;

 public TestClass() {
  map1 = Maps.newHashMap();
 }

 public void addData(Map<K,V> data) {
  for(Entry<K,V> entry : data.entrySet()) {
    map1.put(entry.getKey(), entry.getValue());
  }
 }
}
2
  • 1
    Your little code snippet is full of compile errors. "Class" must be "class", "provate" must be "private", the type parameters are not declared. Additionally, you should read the Map API, especially the putAll method. Voting to close. Commented Jul 8, 2014 at 6:41
  • Other than compilation errors mentioned above, this question has already been posted before, I personally believe this was the best answer but you can also take a look at the other answers on this post. stackoverflow.com/a/7194798/1906826 Commented Jul 8, 2014 at 6:42

1 Answer 1

1

You overlooked putAll

map1.putAll(data);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.