0

So I have this method, it not complete because cannot get it work with the set that I have. The set that have has {0,1,2,7,8,9,10}. I tried with an if/else, but it gave me nullpointer exception error. Should I put HashSet objects into an array and then compare the objects? Please provide any insight.

This is customer intersection method, where I have been provided HashSet.java file which contains, the these following methods. 1 - add method 2 - contains method 3 - remove method

  public HashSet Intersect(HashSet s1)// only 1 & 2 should be printed
  {                
    HashSet intersect = new HashSet(buckets.length); 

    Iterator iter = this.iterator();   

    while(iter.hasNext())
    {
        intersect.add(iter.next());
    }

    Iterator iter1 = s1.iterator();
    while(intersect.contains(iter1.next()))
    {
        intersect.remove(iter.next());
    }

    return intersect;
  }
Henry
  • 13
  • 5
  • Possible duplicate of [How to calculate the intersection of two sets?](https://stackoverflow.com/questions/8882097/how-to-calculate-the-intersection-of-two-sets) – Naman Nov 23 '18 at 20:42
  • Possible duplicate of [How to calculate the intersection of two sets?](https://stackoverflow.com/questions/8882097/how-to-calculate-the-intersection-of-two-sets) – Krease Nov 23 '18 at 20:45
  • I'm using custom prepared HashSet.java. I can't use any available java.utils to solve the problem other than using the given HashSet problem. I managed somehow with some help on Stack to get the Union method and difference method to work, but intersection is so elusive. I hope someone I could help. – Henry Nov 23 '18 at 20:52
  • Iterate over your hashset, use contains to check whether the hashset passed as parameter contains the current element. If so, add that element to the intersection hashset you'll return. – Aaron Nov 23 '18 at 20:58

1 Answers1

0

For your NPE issue you should first read about iterators. Indeed you must always check if there is a next element via .hasNext() (if true then you may safely call .next()). Secondly and tightly related your logic is invalid and problems happen quickly in second loop as you call iter.next() while you 've already fully iterated over iter. So the next element is obviously null. This is certainly not what you intended to code.

bsaverino
  • 1,221
  • 9
  • 14
  • I worked it out but I might wrong values. So something is not right in the code. This is how it looks like in the main method. 'code' HashSet setI = setA.Intersect(setB); Iterator iterI = setI.iterator(); System.out.print("\nThe Intersection is: { "); while(iterI.hasNext()) { System.out.print(iterI.next() + " "); } System.out.print("}"); – Henry Nov 25 '18 at 22:21
  • This is my intersection method, 'code' public HashSet Intersect(HashSet s1)// only 1 & 2 should be printed { HashSet intersect = new HashSet(buckets.length); Iterator iter1 = this.iterator(); while(iter1.hasNext()){ if(s1.contains(iter1.next())) { intersect.add((iter1.next())); } } return intersect; } – Henry Nov 25 '18 at 22:42
  • Sorry but my original answer should be meaningful enough (other comments mught help also). Indeed I see an issue in your second comment and it's still related to the way you use Iterators. You check iter1.next() against s1 and then add iter1.next() to intersect! Calling .next() twice never gives the same element (the iterator moves to the next element!) while hasNext() just checks if there is another element. Just assign iter1.next() to a temporary/scope variable if you plan to re-use it. – bsaverino Dec 03 '18 at 20:47