Java Unit 10
Java Unit 10
Collection classes/interfaces are data structures that can hold a variable number of
objects of different types. They are part of the Java Collections Framework.
Some common collection classes/interfaces in Java include:
ArrayList: an ordered list that allows duplicates
LinkedList: a linked list that allows duplicates
HashSet: a set that does not allow duplicates
TreeSet: a set that stores elements in sorted order
HashMap: a map that stores key-value pairs
To use a collection class/interface, you first need to create an instance of it using the
"new" keyword and its constructor.
Map/List/Set Implementations:
Map Implementations:
Collection Classes:
1.Array List
ArrayList is an implementation of the List interface in Java, which allows for the
storage of elements in an ordered sequence and provides indexed access to
those elements.
ArrayList is implemented as a resizable array, which means that elements can be
added or removed dynamically as needed.
To use ArrayList in Java, you must first import the java.util.ArrayList package.
Here are some common methods used when working with ArrayList:
add(element): Adds an element to the end of the ArrayList.
add(index, element): Adds an element to the ArrayList at the specified index, shifting
all subsequent elements to the right.
get(index): Returns the element at the specified index.
set(index, element): Replaces the element at the specified index with the specified
element.
remove(index): Removes the element at the specified index, shifting all subsequent
elements to the left.
size(): Returns the number of elements in the ArrayList.
clear(): Removes all elements from the ArrayList.
2.Linked List
LinkedList is an implementation of the List interface in Java, which allows for the
storage of elements in an ordered sequence and provides indexed access to
those elements.
LinkedList is implemented as a doubly-linked list, which means that each element in
the list has a reference to both the previous and next elements in the list.
To use LinkedList in Java, you must first import the java.util.LinkedList package.
Here are some common methods used when working with LinkedList:
add(element): Adds an element to the end of the LinkedList.
addFirst(element): Adds an element to the beginning of the LinkedList.
addLast(element): Adds an element to the end of the LinkedList (same as
add(element)).
get(index): Returns the element at the specified index.
set(index, element): Replaces the element at the specified index with the specified
element.
remove(index): Removes the element at the specified index.
removeFirst(): Removes the first element of the LinkedList.
removeLast(): Removes the last element of the LinkedList.
size(): Returns the number of elements in the LinkedList.
clear(): Removes all elements from the LinkedList.
3.Hash Set
HashSet is an implementation of the Set interface in Java, which allows for the
storage of elements in an unordered collection and does not allow duplicates.
HashSet uses a hash table for storage and retrieval of elements.
To use HashSet in Java, you must first import the java.util.HashSet package.
Here are some common methods used when working with HashSet:
add(element): Adds an element to the HashSet.
remove(element): Removes the specified element from the HashSet.
contains(element): Returns true if the HashSet contains the specified element, false
otherwise.
size(): Returns the number of elements in the HashSet.
clear(): Removes all elements from the HashSet.
4.Tree Set
TreeSet is an implementation of the SortedSet interface in Java, which allows for the
storage of elements in a sorted collection and does not allow duplicates.
TreeSet uses a binary search tree for storage and retrieval of elements, which
ensures that the elements are always in sorted order.
To use TreeSet in Java, you must first import the java.util.TreeSet package.
Here are some common methods used when working with TreeSet:
add(element): Adds an element to the TreeSet.
remove(element): Removes the specified element from the TreeSet.
contains(element): Returns true if the TreeSet contains the specified element, false
otherwise.
size(): Returns the number of elements in the TreeSet.
clear(): Removes all elements from the TreeSet.
first(): Returns the first (lowest) element in the TreeSet.
last(): Returns the last (highest) element in the TreeSet.
Comparator:
In Java, the Comparator interface is used to define a custom ordering of objects.
Comparator provides a way to sort objects based on criteria other than their natural
ordering, which is defined by the Comparable interface.
Comparator has two methods: `compare()` and `equals()`.
The `compare()` method compares two objects and returns an integer that indicates
their relative order. If the first object is "less than" the second object, a negative
number is returned. If the first object is "greater than" the second object, a
positive number is returned. If the two objects are "equal", zero is returned.
The `equals()` method is used to check if two comparators are equal.