0% found this document useful (0 votes)
15 views2 pages

Lists Sets

The document explains the differences between Lists and Sets in Java, highlighting that Lists are ordered collections allowing duplicates, while Sets are unordered collections of unique elements. It describes common List implementations like ArrayList, LinkedList, and Vector, and Set implementations like HashSet, LinkedHashSet, and TreeSet. Additionally, it provides guidance on when to use Lists versus Sets based on the need for order and uniqueness.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Lists Sets

The document explains the differences between Lists and Sets in Java, highlighting that Lists are ordered collections allowing duplicates, while Sets are unordered collections of unique elements. It describes common List implementations like ArrayList, LinkedList, and Vector, and Set implementations like HashSet, LinkedHashSet, and TreeSet. Additionally, it provides guidance on when to use Lists versus Sets based on the need for order and uniqueness.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Lists:

A List is an ordered collection of elements that allows duplicates. This means


that elements can be added to a list in any order and multiple times. The order
of the elements in the list is preserved and each element can be accessed by its
index.
The list interface in Java provides several classes that implement it.
Some of the commonly used classes are ArrayList, LinkedList, and Vector.
ArrayList: An ArrayList is a dynamic array that can grow or shrink as needed. It
provides fast access to elements using their index. However, inserting or
removing elements from the middle of the list can be slow, as all the elements
after the insertion point have to be shifted.
LinkedList: A LinkedList is a linked list implementation of the List interface. It
provides efficient insertion and removal of elements from the middle of the
list, but accessing elements by index can be slower than with an ArrayList.
Vector: A Vector is similar to an ArrayList, but it is synchronized. This means
that it is thread-safe but slower than an ArrayList.

Sets
A Set is an unordered collection of unique elements. This means that no
duplicates are allowed, and the elements can be added to the set in any order.
The Set interface in Java provides several classes that implement it. Some of
the commonly used classes are HashSet, LinkedHashSet, and TreeSet.
HashSet: A HashSet stores elements using a hash table. It provides constant-
time performance for basic operations like add, remove, and contains,
assuming that the hash function distributes the elements evenly among the
buckets.
LinkedHashSet: A LinkedHashSet is similar to a HashSet, but it maintains the
insertion order of the elements.

1|Page
java-collections Sudhakara Rao Chillara
TreeSet: A TreeSet stores elements in a sorted tree structure. It provides
guaranteed log(n) time cost for the basic operations like add, remove, and
contains, where n is the size of the set.
When to Use Lists and Sets
Lists are suitable when you need to store a collection of elements in a specific
order and allow duplicates.
For example, if you are creating a shopping list, you would want to maintain
the order of the items and allow the same item to be added multiple times.
Sets are suitable when you need to store a collection of unique elements and
order is not important.
For example, if you are creating a set of unique email addresses, you would not
want to store duplicates and order is not important.
In some cases, you may need to use both lists and sets in your application. For
example, if you are creating a playlist, you may want to store the songs in a list
to maintain their order, but you may also want to store the unique artists in a
set to avoid duplicates.

2|Page
java-collections Sudhakara Rao Chillara

You might also like