• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Loop through ArrayList and equals duplicate data

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All :-)

I need some help on a simple comparison.
I've an arrayList which input is a txt file and there is an attribute called telephone number for the object Person.
I want to compare each telephone number with the rest of the list to see if there are duplicates.
I have created the following code:


It now prints every phone number because the "i" is when comparing the same.
How can I loopt through myList.get(i).getPhoneNumber() ?

 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use an enhanced for() loop.
You can use a Set to see what phone numbers have come before.

 
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go through this part of the Java™ Tutorials, and this part. You will find there is a kind off Set which maintains insertion order. You will also find there is an ArrayList constructor which can take your Set. See if you can get this sort of thing to work:-
 
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey's method can be shortened  slightly. The method 'set.add(element)' returns a boolean, indicating whether the add was successful or not. So this is possible:
 
Mike Dalton
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all. I'll try it out tomorrow and post the outcome.
 
Mike Dalton
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've added the implementation Carey (will refactor later) if I want to print also the phone number which it conflict with how can I do that?
Example I've in my list the values: 33, 34, 35, 36, 37, 34, 34, 29, 40

Now it will add the 2 times number 34 that they are not unique and it adds the first 34 to the unique list. If I want also to show that the first 34 conflicts with the 2nd and 3rd 34. How can I achieve that?
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hasn't Carey's suggestion already shown you how to show duplicates? You can of course handle the duplicates differently, even adding them to a List or as “V”s in a Map. You can use the index where you found the duplicate as the “K”.
 
Mike Dalton
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it will show 33, 34, 35, 36, 37, 29, 40 in the Set and 34, 34 will be printed.
I want to remove the first 34 from the Set. Your suggestion is to use a Map?
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Dalton wrote:Yes it will show 33, 34, 35, 36, 37, 29, 40 in the Set and 34, 34 will be printed.
I want to remove the first 34 from the Set. Your suggestion is to use a Map?

I don't understand why you'd delete the first one, isn't that the one you'd want to keep and delete the other 2? You would certainly only want to keep one copy of any that are duplicates, but which one, and why? A Set would only contain one instance per phone number. Why would you delete that from the Set?
 
Mike Dalton
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand what you mean but I want to challenge myself to perfectly understand this.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's hard to help you without a complete set of requirements.

Have you read the tutorial on Set ?
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Dalton wrote:. . . I want to remove the first 34 from the Set. . . .  

Iterate the original List backwards.
 
Mike Dalton
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:It's hard to help you without a complete set of requirements.

Have you read the tutorial on Set ?


Yes I have read it.

The requirements are all phone numbers should be unique.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know that a Set can only contain one "34". Adding additional "34"s does not actually add them to the set if there's already one there.
 
Mike Dalton
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I understand that.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Dalton wrote:The requirements are all phone numbers should be unique.



If that's all then putting them into the Set is all you need to do. Earlier in the thread you were trying to do other things, like reporting phone numbers which weren't unique.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not necessarily. The requirement could also be to make a list of only the unique numbers. In tnat case the 'removeAll' method could prove handy. Or my favorite would be to make a frequencymap, where the keys are the numbers, and the values the frequency of those numbers.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:The requirement could also be to make a list of only the unique numbers.



That's true. But Carey just asked the OP for a complete list of requirements and the OP didn't mention making a list as a requirement. Nor was there any requirement about what to do if there were non-unique phone numbers.

So I reiterate Carey's suggestion that a complete list of requirements would be helpful.
 
Mike Dalton
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The requirements are:
-all phone numbers should be unique
-At the end display both the phone numbers with the name of the person and the address of each of the failed records
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a way to make a Map where the key is a phone number and value is a List of Person objects that have that phone number. If the phone number is unique the size of the list would be 1. If it is > 1 then the phone number is not unique. You can decide what you want to do with the list.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe OP wants a list of failed to add records only:

 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With some code to print non-unique phone numbers I get this:
34, Joe
34, Zoo
34, Hank

19, Mary
19, Betty

So the question becomes is "Joe" ok because he got there first and "Zoo" and "Hank" are not ok because they came later? Or do you want to apply some other business logic to all three, not knowing which of the three is the correct one? If it is the former then your solution is simpler.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like a job for groupingBy().
 
Mike Dalton
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I implemented a > 1 counter and it works perfectly.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic