Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

Checking if values are the same

Thread view: 
francan00@yahoo.com - 28 Oct 2007 19:16 GMT
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.
Are Nybakk - 28 Oct 2007 19:31 GMT
> I currently have two String objects I check to find out if they are
> the same value:
[quoted text clipped - 12 lines]
>
> Please advise.

Couldn't you just iterate through a String array? Or add the Strings to
two collections and check equality that way?
Are Nybakk - 28 Oct 2007 23:25 GMT
>> I currently have two String objects I check to find out if they are
>> the same value:
[quoted text clipped - 15 lines]
> 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.
Lew - 28 Oct 2007 19:36 GMT
> I currently have two String objects I check to find out if they are
> the same value:
[quoted text clipped - 10 lines]
> 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());
}

Signature

Lew

francan00@yahoo.com - 28 Oct 2007 21:12 GMT
> franca...@yahoo.com wrote:
> > I currently have two String objects I check to find out if they are
[quoted text clipped - 42 lines]
> --
> Lew

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());

}
Patricia Shanahan - 28 Oct 2007 21:18 GMT
...
> How would this be without Generics?
>
[quoted text clipped - 4 lines]
>
> }

Just delete all instances of "<T>".

Patricia
Daniel Pitts - 28 Oct 2007 20:26 GMT
> I currently have two String objects I check to find out if they are
> the same value:
[quoted text clipped - 12 lines]
>
> 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.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Roedy Green - 28 Oct 2007 23:58 GMT
>Now I need to check 10 objects.  How would I check 10 objects to find
>out if any of them have the same value?

You want an array of Strings.  You compare each of them against the
first one.  See http://mindprod.com/jgloss/array.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.