Hopefully another simple question that shouldn't be difficult, but I'm new to Java and struggling. I have a set setElements
in a class classA
, which is a set of anther class' objects (classB
), that will always only contain two elements. What I need to do is prepare a method that takes an argument, then checks if either element of setElements
is equal to that argument. If one element is equal, the method returns the other element from the set.
I'm just really struggling to get the functionality out and I'm fairly certain it's due to my mediocre Java skills :)
This is the code I have now:
Set<classB> setElements = new HashSet<classB>();
public classA(classB x, class B y) {
setElements.add(x);
setElements.add(y);
\\method to return the other element if one element is equal to argument
public classB otherElement(classB argument){
for (classB x:setElements){
if (x==argument){
return \\other element in setElements} } }
Please help!