views:

274

answers:

4

I read data from a text file, so there may be

John
Mary
John
Leeds

and I need to get 3 elements in the ArrayList, cause there are only 3 unique values in the file.

I can use the hidden HashTable and add information to it, then simply copy its data into the List. Are there other solutions?

+1  A: 

You can check list.contains() before adding.

if(!list.contains(value)) {
    list.add(value);
}

I guessed it would be obvious! However, adding items to a HashSet and then creating a list from this set would be more efficient.

Chandru
I suppose it uses the "equals" method?
EugeneP
Yes it does use equals() on the the string internally.
Chandru
Yep. Thanks. In my case that would be preferable, 'cause the code will be kept clean. But I guess the HashTable algorithm is faster than contains method, unless they use a HashTable internally.
EugeneP
+8  A: 

Why do you need to store it in a List? Do you actually require the data to be ordered or support index-based look-ups?

I would suggest storing the data in a Set. If ordering is unimportant you should use HashSet. However, if you wish to preserve ordering you could use LinkedHashSet.

Adamski
i was about to say that +1
Rakesh Juyal
Why? Cause Lists are used most often and it are easy to work with. I think if you write code for other developers it's better to return a List type than any other type.
EugeneP
Not necessarily. The only case where you'd need a list is if you want random indexed access to items in the collections. Otherwise it would be preferrable to go with plain HashSet or LinkedHashSet.
Chandru
In my opinion the code is cleaner when you use indice. You have the control over "where you are" inside the data structure. It may come from my experience with C language.
EugeneP
@EugeneP: Don't write C code in Java. In every language, it is better to take advantage of its own features.
MAK
@EugeneP: I disagree that it's better to return a List than any other type. It's better to return the appropriate data structure. If uniqueness is important but ordering isn't then return a Set. If neither are important then return a Collection. If you only wish to iterate over the elements then return an Iterable. The more decoupled the calling code is from the internal implementation the easier it is to make changes or substitute in a different implementation.
Adamski
@Mak Agree with you. Yet no one will remove for(;;) loop from Java. Remember where Java comes from? If you have a number of ways how to iterate over a structure, it's up to you to pick one of those.
EugeneP
@EugeneP: I guess your point is that there is no reason to use something just to conform with dogma. I don't disagree. But in this case, a `List` is both unnecessary and less efficient (O(n^2) vs O(n)). Unless you actually need to preserve the order of the `String`s, using a `List` is really a worse choice in practice. That is not the case for `for(;;)`. It may be that using a `List` comes more naturally to you because C does not really have any `Set` like data structure, but it is really not the natural thing to use.
MAK
@Mak All right. I'm picking up this answer as the accepted.
EugeneP
+1  A: 

Use a set instead of a list. Take a look at here: Java Collections Tutorials and specifically about Sets here: Java Sets Tutorial

In a nutshell, sets contain one of something. Perfect :)

Tom Duckering
+2  A: 

If you have a List containing duplicates, and you want a List without, you could do:

List<String> newList = new ArrayList<String>(new HashSet<String>(oldList));

That is, wrap the old list into a set to remove duplicates and wrap that set in a list again.

Fabian Steeg
@Fabian Steeg. Yep. Thanks. Good tip.
EugeneP