1

I have a Object which contains an id and some attributes for that id.

public class Data { private String id; private String name; private String value; } 

Currently I have defined it in the Request Object as :

public class Request { private List<Data> dataList; } 

Is there going to make any difference with List vs Set if I know that I will ll probably never encounter the same id (content) in the request.

Overall for this usecase, which is better ? List vs Set ?

3
  • A List can be randomly accessed, has an order, and a Set doesn't necessarily do. You should also clearly define what you mean by "better", and describe more about your use case. Commented Aug 20, 2021 at 5:05
  • @Sweeper just that If I know there won't be any duplicate id and values - is that necessary for me to use Set ? Commented Aug 20, 2021 at 5:14
  • I would just note that the term "id" implies uniqueness in every context I've ever encountered. If an "id" wasn't unique, how could it "identify" something? Whether or not you want to use a List or a Set is a question of how the consumer of this data structure will process it and the tradeoffs between using a List or a Set. Commented Aug 20, 2021 at 5:49

2 Answers 2

7

Is there going to make any difference with List vs Set

List and Set have different semantics. A List is an ordered collection of items that allows duplicate entries. A Set is an unordered collection of items that allows no duplicates. (there are also some special variants that combine ordering and uniqueness like SortedSet and NavigableSet)

I know that I will ll probably never encounter the same id

The uniqueness of Set is by default ensured by your objects equals method. If you want your Data objects to be unique "by id" you have to implement custom equals and hashCode methods within your Data class that check for equal id.

Overall for this usecase, which is better?

I can't see any use case in your question.

If you need uniqueness take a Set. If you just need a collection of items take a List.

There are of course other use cases, too. For example, if you need to check whether your collection already contains an item or not. With a List you have to iterate over all elements and check for equality. When using for instance an HashSet it will just compute the hashCode of your object (with an equals check afterwards) and has the answer, which is much faster for large collections.

Sign up to request clarification or add additional context in comments.

2 Comments

"When using a Set it will just compute the hashCode of your object" that is true for HashSet, but not for Set generally.
@davmac Yeah true, that's why it's called HashSet. Other Set implementations use different techniques. Just wanted to make clear that Sets in general are optimized for fast contains, because contains is needed for the uniqueness, which is the main purpose of a Set.
1

To ensure that two objects are equal or not, Java uses equals() and hashCode() methods.

These are default method of any object. While putting an object in the set you may get wrong output because default implementation can give you same hash code for two objects.
Hence is it advisable to implement both equals and hashCode methods in custom classes.

Map and Set collection use hash code for storing the object.
However, List is an ordered collection and you can check whether same id exist or not in the objects before adding the object to the list. But again it would be a performance hit for big object pool.

1 Comment

Java only uses equals for compare two objects

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.