- Key things to Note:
- It has Iterator interface in it
- forEach method
- It also has Spliterator interface
- which is combination of splitting + iterator
- it provides tryAdvance() method which is equivalent to both next() and hasNext() from iterator
- it can't be used on Map data structure
- Key things to Note:
- hasNext() => checks whether element exists next in the sequence or not
- next() => returns the element and moves iterator/ cursor point to next element
- remove() => removes last element returned by iterator
- forEachRemaining() => Performs the given action for each remaining element until all elements have been processed or the action throws an exception.
- Key things to Note:
- SIZED – if it’s capable of returning an exact numer of elements with the estimateSize() method
- SORTED – if it’s iterating through a sorted source
- SUBSIZED – if we split the instance using a trySplit() method and obtain Spliterators that are SIZED as well
- CONCURRENT – if source can be safely modified concurrently
- DISTINCT – if for each pair of encountered elements x, y, !x.equals(y)
- IMMUTABLE – if elements held by source can’t be structurally modified
- NONNULL – if source holds nulls or not
- ORDERED – if iterating over an ordered sequence
- Key things to Note:
- size() - size of the collection
- isEmpty() - check empty or not
- contains() - check if contains element
- (Iterator) iterator() -> implements hasNext(), next(), remove(), forEachRemaining()
- toArray() - convert collection to array
- add() - add element to collection
- remove() - remove element from collection
- containsAll() - check if some collection is present
- addAll() - add one collection to other
- removeAll() - remove the passed collection elements from collection
- removeIf() - checks condition and removes element
- retainAll() - keep common elements from passed collection
- clear() - clear the collection
- equals() - check if one object equals to other
- hashCode() - returns hashCode for each object
- (Spliterator) spliterator() -> implements trySplit(), tryAdvance(), forEachRemaining()..
- (java.util.stream.Stream) stream() -> stream object to perform stream operations
- parallelStream() -> similar to stream object
Compiledfrom"List.java"publicinterfacejava.util.List<E> extendsjava.util.Collection<E> {
publicabstractintsize();
publicabstractbooleanisEmpty();
publicabstractbooleancontains(java.lang.Object);
publicabstractjava.util.Iterator<E> iterator();
publicabstractjava.lang.Object[] toArray();
publicabstract <T> T[] toArray(T[]);
publicabstractbooleanadd(E);
publicabstractbooleanremove(java.lang.Object);
publicabstractbooleancontainsAll(java.util.Collection<?>);
publicabstractbooleanaddAll(java.util.Collection<? extendsE>);
publicabstractbooleanaddAll(int, java.util.Collection<? extendsE>);
publicabstractbooleanremoveAll(java.util.Collection<?>);
publicabstractbooleanretainAll(java.util.Collection<?>);
publicvoidreplaceAll(java.util.function.UnaryOperator<E>);
publicvoidsort(java.util.Comparator<? superE>);
publicabstractvoidclear();
publicabstractbooleanequals(java.lang.Object);
publicabstractinthashCode();
publicabstractEget(int);
publicabstractEset(int, E);
publicabstractvoidadd(int, E);
publicabstractEremove(int);
publicabstractintindexOf(java.lang.Object);
publicabstractintlastIndexOf(java.lang.Object);
publicabstractjava.util.ListIterator<E> listIterator();
publicabstractjava.util.ListIterator<E> listIterator(int);
publicabstractjava.util.List<E> subList(int, int);
publicjava.util.Spliterator<E> spliterator();
publicstatic <E> java.util.List<E> of();
publicstatic <E> java.util.List<E> of(E);
publicstatic <E> java.util.List<E> of(E, E);
publicstatic <E> java.util.List<E> of(E, E, E);
publicstatic <E> java.util.List<E> of(E, E, E, E);
publicstatic <E> java.util.List<E> of(E, E, E, E, E);
publicstatic <E> java.util.List<E> of(E, E, E, E, E, E);
publicstatic <E> java.util.List<E> of(E, E, E, E, E, E, E);
publicstatic <E> java.util.List<E> of(E, E, E, E, E, E, E, E);
publicstatic <E> java.util.List<E> of(E, E, E, E, E, E, E, E, E);
publicstatic <E> java.util.List<E> of(E, E, E, E, E, E, E, E, E, E);
publicstatic <E> java.util.List<E> of(E...);
publicstatic <E> java.util.List<E> copyOf(java.util.Collection<? extendsE>);
}
- Key things to Note:
- extra methods apart from Collection Interface are
- addAll(index, collection) - adds all the collection at given index
- replaceAll() - replaces all the elements with the given or specified function using apply operator
- Collections.sort() - sort elements of list accordingly optionally comparator can be passed
- get(index) - get element at given index
- set(index, val) - set element at given index
- add(index, ele) - add at index
- indexOf(val) - get the index of given val
- remove(index) - remove element using index
- lastIndexOf(val) - last occurence index
- listIterator(), listIterator(int) - starts iterator and iterator at specific index
- subList(startIndex, endIndex)
- of() - combine muliple objects to umodifiablelist
- copyOf() - create copyOf umodifiablelist
- Key things to Note:
- [FIFO] -> add at back, remove at front
- add/offer - both adds but add throws exception like IllegalStateException, NPE, ClassCast, IllegalArgument whereas offer returns false if operation failed
- remove/poll - remove throws NoSuchElementException if queue is empty but poll returns null
- element/peek - element() throws NSE exception if queue is empty, but peek() returns null value
Set interface
>> javap java.util.Set;
Compiledfrom"Set.java"publicinterfacejava.util.Set<E> extendsjava.util.Collection<E> {
publicabstractintsize();
publicabstractbooleanisEmpty();
publicabstractbooleancontains(java.lang.Object);
publicabstractjava.util.Iterator<E> iterator();
publicabstractjava.lang.Object[] toArray();
publicabstract <T> T[] toArray(T[]);
publicabstractbooleanadd(E);
publicabstractbooleanremove(java.lang.Object);
publicabstractbooleancontainsAll(java.util.Collection<?>);
publicabstractbooleanaddAll(java.util.Collection<? extendsE>);
publicabstractbooleanretainAll(java.util.Collection<?>);
publicabstractbooleanremoveAll(java.util.Collection<?>);
publicabstractvoidclear();
publicabstractbooleanequals(java.lang.Object);
publicabstractinthashCode();
publicjava.util.Spliterator<E> spliterator();
publicstatic <E> java.util.Set<E> of();
publicstatic <E> java.util.Set<E> of(E);
publicstatic <E> java.util.Set<E> of(E, E);
publicstatic <E> java.util.Set<E> of(E, E, E);
publicstatic <E> java.util.Set<E> of(E, E, E, E);
publicstatic <E> java.util.Set<E> of(E, E, E, E, E);
publicstatic <E> java.util.Set<E> of(E, E, E, E, E, E);
publicstatic <E> java.util.Set<E> of(E, E, E, E, E, E, E);
publicstatic <E> java.util.Set<E> of(E, E, E, E, E, E, E, E);
publicstatic <E> java.util.Set<E> of(E, E, E, E, E, E, E, E, E);
publicstatic <E> java.util.Set<E> of(E, E, E, E, E, E, E, E, E, E);
publicstatic <E> java.util.Set<E> of(E...);
publicstatic <E> java.util.Set<E> copyOf(java.util.Collection<? extendsE>);
}
- Key things to note:
- it has same methods as list
- List is an ordered collection of elements which allows duplicates
- Set is an unordered collection of non-duplicate elements
- Key things to Note:
- ArrayList(int size) - how much initial capacity to be allotted
- trimToSize() - after elements are filled, remaining capacity can be removed by this method
- ensureCapacity(int) - ArrayList resizes automatically, to make sure of good performance at once we can ensure with minimum capacity there by decreasing arraylist resizing frequency
- removeRange(int,int) - protected method can't be used, instead subList(int,int).clear()
- Key things to Note [interface extended from Queue]
- [DEQUE - aka Double ended Queue] -> can insert and remove at both ends
- addFirst()/offerFist() - same operation but addFirst throws exception similar to queue
- addLast()/offerLast() - adds element at the last, difference is similar to queue
- removeFirst()/pollFirst() - removes first element from deque
- removeLast()/pollLast() - removes last element from deque
- getFirst()/peekFirst() - gets the first element
- getLast()/peekLast() - gets the last element
- removeFirstOccurence() - removes first occurence of element
- removeLastOccurence() - removes last Occurence of element
- add()/offer()
- remove()/poll()
- element()/peek()
- push() - push element to the deque
- pop() - pops element from the deque
- remove(object)
- descendingIterator() - iterates over elements in deque in reverse Order
- Key things to Note:
- Stack[LIFO] extends Vector
- push(ele) -> push element to top
- pop(ele) -> pop element at top
- peek() -> gets the top element
- empty() -> checks if stack collections is empty
- search(obj) -> searches for object in stack
- Key things to Note:
- PriorityQueue(), PriorityQueue(int initialCapacity), PriorityQueue(Comparator),PriorityQueue(int initialCapacity, Comparator), PriorityQueue(Collection), PriorityQueue(PriorityQueue), PriorityQueue(SortedSet) -> initializers of the class
- add()/Offer()
- peek()
- remove()/poll()
- removeEq() -> checks for equality and removes [ refer differnce between equals() and equality(==)]
- removeAt(index)
- Key things to Note:
- HashSet is for non-duplicate elements collection without maintaining any order
- add()/ remove()/ clear()
- default initialCapacity - 16, loadFactor- 0.75
- The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased.
- HashSet(int initialCapacity, float loadFactor)
- Key things to Note:
- make note of word it is Sorted. So, all operations are method relates to sorted structure
- subset(E minElement, E maxElement)
- first() - minimum or first element
- last() - maximum or last element
- Comparator() - we can pass our own comparator like new TreeSet<>(){new Comparator<>())
- headSet(element) - returns elements less than passed element
- tailSet(element) - returns elements greater than passed element
- Key things to Note:
- ceiling(ele) - returns the least element that is greater than or equal to the given element
- floor() -returns the greatest element that is less than or equal to the given element
- descendingSet() - returns reverse order of Set
- higher() / lower() - return the least ele that is strictly greater than given ele/ returns the greates ele that is strictly less than given ele
- subSet(minEle, boolean fromInclusive, maxEle, boolean toInclusive)
- headSet(ele, boolean toInclusive)
- tailSet(ele, boolean fromInclusive)
About
Java Collection Framework - my Tutorials for Revision