Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   How to check variables for uniqueness ? (http://www.velocityreviews.com/forums/t389576-how-to-check-variables-for-uniqueness.html)

krislioe@gmail.com 12-21-2006 05:03 AM

How to check variables for uniqueness ?
 
Hi all,

I have eight variables : var1, var2... var 8. All types String.
How to check that each variables has unique values ?

Thank you for your help,
xtanto


Andrew Thompson 12-21-2006 05:08 AM

Re: How to check variables for uniqueness ?
 
krisl...@gmail.com wrote:
....
> I have eight variables : var1, var2... var 8. All types String.
> How to check that each variables has unique values ?


One way would be to create a Map, iterate the
var's and if not present in the map, add the value
as a key, else return false.

Andrew T.


Patricia Shanahan 12-21-2006 06:18 AM

Re: How to check variables for uniqueness ?
 
Andrew Thompson wrote:
> krisl...@gmail.com wrote:
> ...
>> I have eight variables : var1, var2... var 8. All types String.
>> How to check that each variables has unique values ?

>
> One way would be to create a Map, iterate the
> var's and if not present in the map, add the value
> as a key, else return false.
>
> Andrew T.
>


Any particular reason for Map, rather than Set?

Note that the result of a Set add call is true if, and only if, the
value is not already in the Set.

Patricia

Andrew Thompson 12-21-2006 06:25 AM

Re: How to check variables for uniqueness ?
 
Patricia Shanahan wrote:
> Andrew Thompson wrote:
> > krisl...@gmail.com wrote:
> > ...
> >> I have eight variables : var1, var2... var 8. All types String.
> >> How to check that each variables has unique values ?

> >
> > One way would be to create a Map, iterate the
> > var's and if not present in the map, add the value
> > as a key, else return false.

....
> Any particular reason for Map, rather than Set?


You mean besides, 'lack of enough consultation
of the relevant docs.'? ;-)

> Note that the result of a Set add call is true if, and only if, the
> value is not already in the Set.


A Set sounds the go - it is just right for this task.

Andrew T.


John Ersatznom 12-21-2006 06:33 AM

Re: How to check variables for uniqueness ?
 
Andrew Thompson wrote:
> Patricia Shanahan wrote:
>
>>Andrew Thompson wrote:
>>
>>>krisl...@gmail.com wrote:
>>>...
>>>
>>>>I have eight variables : var1, var2... var 8. All types String.
>>>>How to check that each variables has unique values ?
>>>
>>>One way would be to create a Map, iterate the
>>>var's and if not present in the map, add the value
>>>as a key, else return false.

>
> ...
>
>>Any particular reason for Map, rather than Set?

>
>
> You mean besides, 'lack of enough consultation
> of the relevant docs.'? ;-)
>
>
>>Note that the result of a Set add call is true if, and only if, the
>>value is not already in the Set.

>
>
> A Set sounds the go - it is just right for this task.


HashSet<String> foo = new HashSet<String>();
foo.add(var1);
foo.add(var2);
foo.add(var3);
foo.add(var4);
foo.add(var5);
foo.add(var6);
foo.add(var7);
foo.add(var8);
if (foo.size() < 8)
duplicateExists();
else
duplicateDoesNotExist();

If you actually need to identify the specific duplicate pairs, you need
to compare them one by one -- 1 with all the others, 2 with all the
higher-numbered ones, and so on up to 7 and 8, using equals().

If you want case insensitivity, use e.g.

foo.add(var3.toLowerCase());

or equalsIgnoreCase().

Patricia Shanahan 12-21-2006 11:30 AM

Re: How to check variables for uniqueness ?
 
John Ersatznom wrote:
> Andrew Thompson wrote:
>> Patricia Shanahan wrote:
>>
>>> Andrew Thompson wrote:
>>>
>>>> krisl...@gmail.com wrote:
>>>> ...
>>>>
>>>>> I have eight variables : var1, var2... var 8. All types String.
>>>>> How to check that each variables has unique values ?
>>>>
>>>> One way would be to create a Map, iterate the
>>>> var's and if not present in the map, add the value
>>>> as a key, else return false.

>>
>> ...
>>
>>> Any particular reason for Map, rather than Set?

>>
>>
>> You mean besides, 'lack of enough consultation
>> of the relevant docs.'? ;-)
>>
>>
>>> Note that the result of a Set add call is true if, and only if, the
>>> value is not already in the Set.

>>
>>
>> A Set sounds the go - it is just right for this task.

>
> HashSet<String> foo = new HashSet<String>();
> foo.add(var1);
> foo.add(var2);
> foo.add(var3);
> foo.add(var4);
> foo.add(var5);
> foo.add(var6);
> foo.add(var7);
> foo.add(var8);
> if (foo.size() < 8)
> duplicateExists();
> else
> duplicateDoesNotExist();
>
> If you actually need to identify the specific duplicate pairs, you need
> to compare them one by one -- 1 with all the others, 2 with all the
> higher-numbered ones, and so on up to 7 and 8, using equals().


To save repititious writing, I'm going to assume the strings are in an
array. The equivalent of your code would be:

HashSet<String> foo = new HashSet<String>();
for(String v:vars){
foo.add(v);
}
if (foo.size() < vars.length)
duplicateExists();
else
duplicateDoesNotExist();

You can simplify finding specific duplicates by checking the foo.add
results:

HashSet<String> foo = new HashSet<String>();
for(int i=0; i<vars.length; i++){
if(!foo.add(vars[i]){
for(int j=0; j<i; j++){
if(vars[i].equals(vars[j])){
reportDuplicate(i,j);
}
}
}
}

A true result from foo.add means the string was actually added to the
set, so it has no duplicate with a lower index.

Patricia

Ed Kirwan 12-21-2006 12:25 PM

Re: How to check variables for uniqueness ?
 
Patricia Shanahan wrote:

>
> You can simplify finding specific duplicates by checking the foo.add
> results:
>
> HashSet<String> foo = new HashSet<String>();
> for(int i=0; i<vars.length; i++){
> if(!foo.add(vars[i]){
> for(int j=0; j<i; j++){
> if(vars[i].equals(vars[j])){
> reportDuplicate(i,j);
> }
> }
> }
> }
>
> A true result from foo.add means the string was actually added to the
> set, so it has no duplicate with a lower index.
>
> Patricia


Perhaps using a List would obviate the need for the nest loop?

List list = new ArrayList();
for (int i = 0, n = vars.length; i < n; i++) {
int duplicateIndex = list.indexOf(vars[i]);
if (duplicateIndex != -1) {
reportDuplicate(i, duplicateIndex);
} else {
list.add(vars[i]);
}
}

..ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition.

Download Fractality, free Java code analyzer:
http://www.EdmundKirwan.com/servlet/...c-page130.html

Remon van Vliet 12-21-2006 01:28 PM

Re: How to check variables for uniqueness ?
 

> Perhaps using a List would obviate the need for the nest loop?
>
> List list = new ArrayList();
> for (int i = 0, n = vars.length; i < n; i++) {
> int duplicateIndex = list.indexOf(vars[i]);
> if (duplicateIndex != -1) {
> reportDuplicate(i, duplicateIndex);
> } else {
> list.add(vars[i]);
> }
> }
>
> .ed


The nested loop is only needed to allow reporting of a specific duplicate
pair. I cannot think of many practical examples where that is required
rather than simply reporting that the element to be added is a duplicate. If
it is required then I'd say you're right, using a List does result is
slightly more readable code.

That said, if the collection must not contain duplicate elements then at
least from a design and correctness perspective you should use a Set. I'd
personally do so even if that decision would result in a few extra lines of
code here and there.

Remon



Oliver Wong 12-21-2006 09:09 PM

Re: How to check variables for uniqueness ?
 
"John Ersatznom" <j.ersatz@nowhere.invalid> wrote in message
news:emd9s6$cns$1@aioe.org...
>
> If you want case insensitivity, use e.g.
>
> foo.add(var3.toLowerCase());


This might not actually work, because of the fickleness of certain human
languages.

>
> or equalsIgnoreCase().


Yeah, I'd essentially wrap the String in a custom class which overrides
equals to call equalsIgnoreCase, and give that to the Set.

- Oliver



Hemal Pandya 12-22-2006 05:45 AM

Re: How to check variables for uniqueness ?
 

Ed Kirwan wrote:
> Patricia Shanahan wrote:

[...]
> Perhaps using a List would obviate the need for the nest loop?


It will, but will be a lot more expensive. Use can use a
Map<String,Integer> to both avoid nested loop and report indexes. Yes,
it will take more memory.

[....]



All times are GMT. The time now is 02:04 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.