Results 1 to 1 of 1
-
Printing the union and instersection of two sets
Java Code:import java.util.*; /** Show the union and instersection of two sets. */ public class SetStuff { public static void main(String[] args) { // Create two sets. Set s1 = new HashSet(); s1.add("Ian Darwin"); s1.add("Bill Dooley"); s1.add("Jesse James"); Set s2 = new HashSet(); s2.add("Ian Darwin"); s2.add("Doolin' Dalton"); Set union = new TreeSet(s1); union.addAll(s2); // now contains the union print("union", union); Set intersect = new TreeSet(s1); intersect.retainAll(s2); print("intersection", intersect); } protected static void print(String label, Collection c) { System.out.println("--------------" + label + "--------------"); Iterator it = c.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
Problem after Printing GUI.
By coldblood22 in forum AWT / SwingReplies: 1Last Post: 04-05-2008, 02:43 PM -
Printing (no dialog)
By Java Tip in forum Java TipReplies: 0Last Post: 02-04-2008, 09:36 AM -
No duplicates allowed in Sets
By Java Tip in forum Java TipReplies: 0Last Post: 01-21-2008, 04:33 PM -
Java Collection Framework (Sets)
By Java Tutorial in forum Java TutorialReplies: 0Last Post: 12-07-2007, 07:50 PM -
how does the remove method work for sets and hashsets
By haridharna in forum Advanced JavaReplies: 4Last Post: 08-06-2007, 12:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks