11:39 pm - Saturday January 21, 2017

Creating a clone(duplicate) of HashMap using Object clone

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  

                 This function is used to provide a copy of an object of a class and it implements cloneable interface. Copies the objects and its content to it new variable.

Example Program


import java.util.HashMap;
import java.util.Set;

public class Hash1 {

	public static void main(String[] args)

	{

		HashMap<String, Object> h1 = new HashMap<String, Object>();

		HashMap<String, Object> h2 = new HashMap<String, Object>();

		System.out.println("Access Specifiers List in java:\n");

		h1.put("Public", "No restriction");

		h1.put("Protected", "Less Restriction");

		h1.put("Private", "Restricted fn");

		h1.put("default", "No permission to access");

		Set set = h1.entrySet();

		for (Object obj : set.toArray()) {

			System.out.println(obj);

		}

		System.out.println("Copying functions of h1 to h2: ");

		h2 = (HashMap<String, Object>) h1.clone();

		System.out.println(h2);

	}

}

Output

   Access Specfiers List in java:

   default=No permission to access

   Protected=Less Restriction

   Private=Restricted fn

   Public=No restriction

   Copying functions of h1 to h2:

    Clone function:{default=No permission to access, Protected=Less Restriction, Public=No restriction, Private=Restricted fn}

BACK

Filed in: Java Examples

Comments are closed.