1:38 pm - Saturday March 25, 2017

HashSet example program for boolean retainAll(Collection c)

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
/**

 *@author:candidjava.com
 *@description:Hashset example program for boolean retainAll(Collection c)

 */

import java.util.HashSet;

public class HashSetRetainAll {

	public static void main(String[] args) {

		HashSet hs1 = new HashSet(); // Create two hashset
		HashSet hs2 = new HashSet();

		hs1.add("Isabella");
		hs1.add("Angelina");
		hs1.add("Pille"); // Adding some elements to hs1
		hs1.add("Hazem");

		hs2.add("Isabella"); // Add some elements to hs2
		hs2.add("Angelina");
		hs2.add("Bianca");

		System.out.println("The hs1 collection before: " + hs1); // Display the
																	// two
																	// collections
																	// before:
		System.out.println("The hs2 collection before: " + hs2);

		hs1.retainAll(hs2); // Delete the elements hs1 that are not in hs2

		// Display the collection after:
		// Display the two collections before retaining all:
		System.out.println("The hs1 collection after: " + hs1);
		System.out.println("The hs2 collection after: " + hs2);
	}
}

Output

The hs1 collection before: [Isabella, Hazem, Pille, Angelina]
The hs2 collection before: [Isabella, Bianca, Angelina]
The hs1 collection after: [Isabella, Angelina]
The hs2 collection after: [Isabella, Bianca, Angelina]

BACK

Filed in: Z-A All Java Codes

Comments are closed.