How do I get the Intersection method to work?
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;
}
java
add a comment |
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;
}
java
Possible duplicate of How to calculate the intersection of two sets?
– nullpointer
Nov 23 '18 at 20:42
Possible duplicate of 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
add a comment |
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;
}
java
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;
}
java
java
edited Nov 23 '18 at 20:50
Henry
asked Nov 23 '18 at 20:40
HenryHenry
82
82
Possible duplicate of How to calculate the intersection of two sets?
– nullpointer
Nov 23 '18 at 20:42
Possible duplicate of 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
add a comment |
Possible duplicate of How to calculate the intersection of two sets?
– nullpointer
Nov 23 '18 at 20:42
Possible duplicate of 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
Possible duplicate of How to calculate the intersection of two sets?
– nullpointer
Nov 23 '18 at 20:42
Possible duplicate of How to calculate the intersection of two sets?
– nullpointer
Nov 23 '18 at 20:42
Possible duplicate of How to calculate the intersection of two sets?
– Krease
Nov 23 '18 at 20:45
Possible duplicate of 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
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
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
add a comment |
1 Answer
1
active
oldest
votes
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.
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 3 '18 at 20:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452714%2fhow-do-i-get-the-intersection-method-to-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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 3 '18 at 20:47
add a comment |
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.
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 3 '18 at 20:47
add a comment |
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.
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.
answered Nov 24 '18 at 0:23
bsaverinobsaverino
1947
1947
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 3 '18 at 20:47
add a comment |
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 3 '18 at 20:47
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
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
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 3 '18 at 20:47
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 3 '18 at 20:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452714%2fhow-do-i-get-the-intersection-method-to-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of How to calculate the intersection of two sets?
– nullpointer
Nov 23 '18 at 20:42
Possible duplicate of 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