7:54 pm - Thursday April 25, 2019

Add all key value pair from another Map – Java copy map

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

The method public void putAll(Map<?extends K,?extends V>m) copies all the mappings from one map to another map. The function map1.putAll(map2) is used to copy from one map to another.

Example Program


import java.util.LinkedHashMap;

public class LinkedHashMapPutAll {

public static void main(String[] args) {

LinkedHashMap lhm = new LinkedHashMap();

lhm.put("1", "Add");

lhm.put("2", "Sub");

lhm.put("3", "Mul");

LinkedHashMap lhm1 = new LinkedHashMap();

lhm1.put("1", "java");

lhm1.put("2", "c");

lhm1.put("3", "c++");

System.out.println("Populating two Maps");

System.out.println("Before - Map 1: " + lhm);

System.out.println("Before - Map 2: " + lhm1);

lhm.putAll(lhm1);

System.out.println("After - Map 1: " + lhm);

System.out.println("After - Map 2: " + lhm1);

}

}

Output

    Populating two Maps

    Before – Map 1: {1=Add, 2=Sub, 3=Mul}

    Before – Map 2: {1=java, 2=c, 3=c++}

    After – Map 1: {1=java, 2=c, 3=c++}

    After – Map 2: {1=java, 2=c, 3=c++}

BACK

Filed in: Java Examples

Comments are closed.