Checking if values are the same

F

francan00

I currently have two String objects I check to find out if they are
the same value:

String str1 = "red";
String str2 = "yellow";
if (str1.equals(str2)){
System.out.println("Equal");
}
else{
System.out.println("Not equal");
}


Now I need to check 10 objects. How would I check 10 objects to find
out if any of them have the same value?

Please advise.
 
A

Are Nybakk

I currently have two String objects I check to find out if they are
the same value:

String str1 = "red";
String str2 = "yellow";
if (str1.equals(str2)){
System.out.println("Equal");
}
else{
System.out.println("Not equal");
}


Now I need to check 10 objects. How would I check 10 objects to find
out if any of them have the same value?

Please advise.

Couldn't you just iterate through a String array? Or add the Strings to
two collections and check equality that way?
 
L

Lew

I currently have two String objects I check to find out if they are
the same value:

String str1 = "red";
String str2 = "yellow";
if (str1.equals(str2)){
System.out.println("Equal");
}
else{
System.out.println("Not equal");
}

Now I need to check 10 objects. How would I check 10 objects to find
out if any of them have the same value?

Variations on:

Collection <Thing> mightHaveDupes = getSomeCollection();
Collection <Thing> noDupes = new WhateverCollection <Thing> ();
// we'll get back to which Collection implementation to use

for ( Thing thing : mightHaveDupes )
{
if ( noDupes.contains( thing ))
{
log( "Thing {"+ thing +"} is duplicated." );
}
else
{
noDupes.add( thing );
}
}

If the copy is a Set, then duplication is avoided automatically:

public <T> boolean hasDupes( Collection<T> mayHave )
{
Set<T> copy = new HashSet<T> ( mayHave );
return (copy.size() < mayHave.size());
}
 
D

Daniel Pitts

I currently have two String objects I check to find out if they are
the same value:

String str1 = "red";
String str2 = "yellow";
if (str1.equals(str2)){
System.out.println("Equal");
}
else{
System.out.println("Not equal");
}


Now I need to check 10 objects. How would I check 10 objects to find
out if any of them have the same value?

Please advise.

The easiest way would probably be to add them to a Set of some sort
(HashSet probably). Set.add() will return false if that value was
already in the set.
 
F

francan00

Variations on:

Collection <Thing> mightHaveDupes = getSomeCollection();
Collection <Thing> noDupes = new WhateverCollection <Thing> ();
// we'll get back to which Collection implementation to use

for ( Thing thing : mightHaveDupes )
{
if ( noDupes.contains( thing ))
{
log( "Thing {"+ thing +"} is duplicated." );
}
else
{
noDupes.add( thing );
}

}

If the copy is a Set, then duplication is avoided automatically:

public <T> boolean hasDupes( Collection<T> mayHave )
{
Set<T> copy = new HashSet<T> ( mayHave );
return (copy.size() < mayHave.size());

}

Thanks, I dont have Generics in my Java 1.4 environment.

How would this be without Generics?

public <T> boolean hasDupes( Collection<T> mayHave )
{
Set<T> copy = new HashSet<T> ( mayHave );
return (copy.size() < mayHave.size());

}
 
P

Patricia Shanahan

How would this be without Generics?

public <T> boolean hasDupes( Collection<T> mayHave )
{
Set<T> copy = new HashSet<T> ( mayHave );
return (copy.size() < mayHave.size());

}

Just delete all instances of "<T>".

Patricia
 
A

Are Nybakk

Are said:
Couldn't you just iterate through a String array? Or add the Strings to
two collections and check equality that way?

I think I misunderstood somewhat - should be one collection and you can
check if it contains your string.

Lew's solution, however, is quite elegant.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top