Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

set inclusion in Java

843789Aug 10 2010 — edited Aug 10 2010
Can anyone show me how to write a contains method to show set inclusion. I have two HashSets s1 and s2 with elements in them I have an iterator to show the elements in each set. The next stage is to write a method that will show that s2 is included in s1. I have been told to use a contains method, any help to get me started would be very much appreciated. This is what I have so far.

import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeSet;


public class Specification {


public static void main(String[] args) {

// Create two sets.
HashSet s1 = new HashSet();
s1.add("r1");
s1.add("r2");
s1.add("r3");

HashSet s2 = new HashSet();
s2.add("r1");
s2.add("r2");

//use iterator to retrieve elements from set S1

Iterator it = s1.iterator();

while(it.hasNext())
System.out.println(it.next());
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 7 2010
Added on Aug 10 2010
4 comments
358 views