Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use HashSet.contains() to check elements?

Status
Not open for further replies.

langnoi

IS-IT--Management
Mar 20, 2003
6
US
I'm new to programming and trying to use HashSet.contains() method to check for elements in one HashSet exist in another HashSet. I defined a boolean method, compareString, to check if all the elements in stringExpected exists in stringActual. For some reason, it always return 'FALSE', any ideas?

This is what I have:

String[] stringExpected {"a", "b", "c"};

String[] stringActual {"a", "b", "c"};

boolean compareString(StringI[] stringExpected, String[] stringActual)
{

HashSet sActual = new HashSet();
HashSet sExpected = new HashSet();

for (int i = 0; i < uris.length; ++i)
{
sExpected.add(stringExpected);
}
for (int i = 0; i < stringActual.length; ++i)
{
sActual.add(stringActual);
}

return sActual.contains(sExpected);
}
 
what is uris.length?

However - your test
Code:
return sActual.contains(sExpected);
tests, whether a HashSet contains another HashSet, but the first HashSet contains Strings - not HashSets.

Code:
return sActual.containsAll (sExpected);
might be what you're looking for.

don't visit my homepage:
 
Just another way:

Code:
String[] arrayA;
String[] arrayB;
boolean containSameElements = java.util.Arrays.equals(java.util.Arrays.sort(arrayA),java.util.Arrays.sort(arrayB));

Cheers,
Dian
 
Thanks Stefan and Diancecht for your help. I got it working now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top