File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Iterating over a list - Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Iterating over a list - Java" Watch "Iterating over a list - Java" New topic

Iterating over a list - Java

Grishma Dube
Ranch Hand

Joined: Jul 01, 2003
Posts: 275

I have got a list of accounts. There can be more than one record of one single account in the list.

Out of this list i need to prepare a list, which will have only one record of an account. In shotr i want to remove duplicate record.

Please give me some idea, how should i iterate the list.

Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Let's say that your list is defined as :

Use the following code :

Good luck ...

Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

You shouldn't iterate the list. This filters out the duplicates for you:That's assuming you have an equals() method that returns true if two accounts are the same. And if you want to preserve the sequence of the original list then use a LinkedHashset.
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
I learnt from this topic as well

Thanks ...
I agree. Here's the link:
subject: Iterating over a list - Java
It's not a secret anymore!