To copy all the key-value pairs from one Map into another: public void putAll(Map map) : HashMap « Collections « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Collections » HashMap 
9.26.15.To copy all the key-value pairs from one Map into another: public void putAll(Map map)Previous/Next
import java.util.HashMap;
import java.util.Map;

public class MainClass {
  public static void main(String[] a) {
    Map map = new HashMap();
    map.put("key1""value1");
    map.put("key2""value2");
    map.put("key3""value3");
    map.put(null, null);

    Map map2 = new HashMap();
    map2.put("key4""value4");
    map2.put("key5""value5");
    map2.put("key6""value6");
    map.putAll(map2);

    System.out.println(map);
  }
}
{key1=value1, key3=value3, key6=value6, null=null, key5=value5, key2=value2, key4=value4}

The value is replaced for every key in the passed-in map that already exists in the HashMap.

9.26.HashMap
9.26.1.HashMap Class: a basic key-value map where the elements are unordered
9.26.2.Iterate through the values of HashMap
9.26.3.Get Synchronized Map from HashMap
9.26.4.Check if a particular key exists in HashMap
9.26.5.Check if a particular value exists in HashMap
9.26.6.For keys of a map
9.26.7.For values of a map
9.26.8.For both the keys and values of a map
9.26.9.Storing Primitive Types in a Collection
9.26.10.Get Set view of Keys from HashMap
9.26.11.Get Size of HashMap
9.26.12.Adding Key-Value Pairs: public Object put(Object key, Object value)
9.26.13.If key is not in add the key value pair
9.26.14.Unlike a Hashtable, both the key and the value for a HashMap can be null
9.26.15.To copy all the key-value pairs from one Map into another: public void putAll(Map map)
9.26.16.Displaying Contents: public String toString()
9.26.17.Remove value from HashMap
9.26.18.Removing Key-Value Pairs: public Object remove(Object key)
9.26.19.Removal of all elements from a map: public void clear()
9.26.20.Sizing Hash Maps: find out the number of key-value pairs within the HashMap
9.26.21.Fetching Keys and Values: find a value based upon a specific key with: public Object get(Object key)
9.26.22.Getting the set of all keys with the keySet() method: public Set keySet()
9.26.23.To get the set of all the values in the hash map: public Collection values()
9.26.24.To get a set of Map.Entry elements back from a HashMap: public Set entrySet()
9.26.25.Finding Elements
9.26.26.Cloning Hash Map: public Object clone()
9.26.27.Creating a Copy of a Collection: the objects are not cloned.
9.26.28.Checking Hash Maps for Equality: public boolean equals(Object o)
9.26.29.Sort an HashMap based on the keys
9.26.30.Serializing Hash Maps
9.26.31.Use Iterator to loop through the map key set
9.26.32.Create Java Hashtable from HashMap
9.26.33.How do I...Store simple pairs of data for quick lookup?
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.