0% found this document useful (0 votes)
6 views8 pages

Collections Diagrams

Uploaded by

220701299
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Collections Diagrams

Uploaded by

220701299
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Framework:

- Provide a set of interfaces and classes to implement various data structures and algorithms.
- Manipulation operations: storing data, searching, sorting, insertion, deletion, and updating of data.

# Arrays Collection

Arrays are fixed in size that is once


we create an array we cannot Collection are growable in nature that is based on our requirement. We
1
increased or decreased based on can increase or decrease of size.
our requirement.

Write in memory Arrays are not


2 Write in memory collection are recommended to use.
recommended to use.

With respect to performance Arrays


3 With respect to performance collection are not recommended to use.
are recommended to use.

Arrays can hold only homogeneous


4 Collection can hold both homogeneous and heterogeneous elements.
data types elements.

Every collection class is implemented based on some standard data


There is no underlying data structure
structure, and hence, ready-made method support is available for
5 for arrays and hence ready made
every requirement. As performance is crucial, we can use these
method support is not available.
methods directly, and we are not responsible for implementing them.

Arrays can hold both object and Collection can hold only object types but not primitive datatypes such
6
primitive data type . as int, long, short, etc.
ArrayList:

- Resizable/dynamic array & Part of java.util package

 iterator() - returns iterator object that can be used to sequentially access elements of lists

 add() - adds an element to a list


 addAll() - adds all elements of one list to another
 remove() - removes an element from the list
 removeAll() - removes all the elements from the list
 clear() - removes all the elements from the list (more efficient than removeAll())

 get() - helps to randomly access elements from lists


 set() - changes elements of lists
 size() - returns the length of lists
 toArray() - converts a list into an array

 contains() - returns true if a list contains specified element


 indexOf() - Searches a specified element in an arraylist and returns the index of the element.
- push & pop/peek

// ArrayList implementation of List


List<String> list1 = new ArrayList<>();

// LinkedList implementation of List


List<String> list2 = new LinkedList<>();

// Vector implementation of List


List<String> list3 = new Vector<>();

// Stack implementation of List


List<String> list3 = new Stack<>();
 hashCode() - returns a hash code value (address of the element in the set)

Set Operations
The Java Set interface allows us to perform basic mathematical set operations like union, intersection, and subset.
 Union - to get the union of two sets x and y, we can use x.addAll(y)
 Intersection - to get the intersection of two sets x and y, we can use x.retainAll(y)
 Subset - to check if x is a subset of y, we can use y.containsAll(x)

Queue:

 add() - Inserts the specified element into the queue. If the task is successful, add() returns true, if not it throws an exception.
 offer() - Inserts the specified element into the queue. If the task is successful, offer() returns true, if not it returns false.
 element() - Returns the head of the queue. Throws an exception if the queue is empty.
 peek() - Returns the head of the queue. Returns null if the queue is empty.
 remove() - Returns and removes the head of the queue. Throws an exception if the queue is empty.
 poll() - Returns and removes the head of the queue. Returns null if the queue is empty.

Deque:

 removeFirst() - returns and removes the first element from the array deque (equivalent to remove())
 removeLast() - returns and removes the last element from the array deque
 pollFirst() - returns and removes the first element of the array deque (equivalent to poll()
 pollLast() - returns and removes the last element of the array deque

Sorted Set:

 comparator() - returns a comparator that can be used to order elements in the set
 first() - returns the first element of the set
 last() - returns the last element of the set
 headSet(element) - returns all the elements of the set before the specified element
 tailSet(element) - returns all the elements of the set after the specified element including the specified element
 subSet(element1, element2) - returns all the elements between the element1 and element2 including element1

You might also like