Difference between Collectors.List and Collecetors.Set

48 Views Asked by At

I'm wondering what is the difference between collections.set and collections.list. In my current situtation, I'm trying to stream through a set of participants and map them by the id and just collect the email of it.

And I'm wondering now what should i use?

1

There are 1 best solutions below

0
Mr. Irrelevant On

Set: Java Set is a collection construct that, by definition, contains unique elements — that is, no duplicates. The Java Set collection can only contain objects, and it is not an ordered list, which means it does not care about the order of the elements. Because the set is an interface, you cannot instantiate it directly.

List: The list is an ordered collection, also known as a sequence. Because the list is organized, you have complete control over where list items are placed in a list. One thing to note here is that the Java list collection can only contain objects.

As you see a good indicator is, do you need the participants to be in a specific order?

source: https://www.vibrantpublishers.com/blogs/blogs-on-programming/java-collections-list-set-queue-map