CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2010
    Posts
    3

    Using Contains method in HashSet

    I'm working on a project in Java, I am new to java and haven't used it much before so any help would be appreciated. I have two HashSets with elements in them. I am trying to write a method to return a boolean stating if one set contains all of the elements from another set


    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");
    s2.add("r3");
    s2.add("r4");

    //check if s2 contains s1
    public static <T> boolean isSubset(HashSet<T> s2, HashSet<T> s1); {
    return s2.containsAll(s1);


    I need help to rectify my contains method. Can someone tell me what's wrong with it and how I can fix it. I get errors on the static and boolean sections. I am using Eclipse as my platform.

    Thanks

  2. #2
    Join Date
    Apr 2007
    Posts
    442

    Re: Using Contains method in HashSet

    Besides the tons of simple errors, such as missing curlies {}, misplaced ";" sings, it works. However, your implementation does not give anything extra to what already is there, as you are aware containsAll(...) exists in AbstractCollection as it is. Why not just use that?

    However, remove the ";" from the end of your isSubset(...) method, insert the missing curlies and you have a working piece of code. In cases such as this, spend effort in learning to read the error messages you get. IDEs such as Eclipse do give you fair bit of warning and information as to whats going amiss.

  3. #3
    Join Date
    Aug 2010
    Posts
    3

    Re: Using Contains method in HashSet

    Sorry for being such a pain, I'm still getting used to all of the Java syntax, could you copy and paste what you changed so i can compare it to my code and see where the syntax errors are?

    I'm doing some basic functions and going to be adding more methods as I go along, the idea is to have a number of consistent sets displayed in a graph format at the end.


    Sorry

    Thanks

  4. #4
    Join Date
    Apr 2007
    Posts
    442

    Re: Using Contains method in HashSet

    Code:
    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");
    s2.add("r3");
    s2.add("r4");
    System.out.println(isSubset(s2, s1));
    }
    
    //check if s2 contains s1
    public static <T> boolean isSubset(HashSet<T> s2, HashSet<T> s1) {
    return s2.containsAll(s1);
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured