Collections framework in java

13
Collections FrameWork ArrayDqueue Sorting & Reverse Techniques

Transcript of Collections framework in java

Collections FrameWorkArrayDqueue

Sorting & Reverse Techniques

Collections Framework

• The Java Collections Framework is a collection of interfaces and classeswhich helps in storing and processing the data efficiently

• This framework has several useful classes which have tons of useful functions which makes a programmer task super easy.

List• A List is an ordered Collection (sometimes called a sequence)

• Lists may contain duplicate elements.

• Elements can be inserted or accessed by their position in the list, using a zero-based index.

ArrayList

• ArrayList is a resizable-array implementation of the List interface.

• It implements all optional list operations, and permits all elements, including null.

• this class provides methods to manipulate the size of the array that is used internally to store the list

• Example program for ArrayList

LinkedList• LinkedList is a doubly-linked list implementation of

the List and Deque interfaces

• The LinkedList class extends AbstractSequentialList and implements the List interface

• It provides a linked-list data structure.

• Example of LinkedList

Difference between ArrayList and LinkedList

• Since Array is an index based data-structure searching or getting element from Array with index is pretty fast. Array provides O(1) performance for get(index) method but remove is costly in ArrayList as you need to rearrange all elements. On the Other hand LinkedList doesn't provideRandom or index based access and you need to iterate over linked list to retrieve any element which is of order O(n).

• LinkedList has more memory overhead than ArrayList because in ArrayListeach index only holds actual object (data) but in case of LinkedList each node holds both data and address of next and previous node.

Set Interface

• A Set is a Collection that cannot containduplicate elements

• .There are three main implementations of Set interface: HashSet, TreeSet, LinkedHashSet

HashSet• HashSet extends AbstractSet and implements the Set interface.

• It creates a collection that uses a hash table for storage.

• A hash table stores information by using a mechanism called hashing.

• In hashing, the informational content of a key is used to determine a unique value, called its hash code.

• The hash code is then used as the index at which the data associated with the key is stored

• Keypoints of HashSet

• HashSet doesn’t maintain any order

• HashSet doesn’t allow duplicates

• HashSet allows null values however if you insert more than one nulls it would still return only one null value

• Example Program

Tree Set Class• TreeSet is similar to HashSet except that it

sorts the elements in the ascending order

• TreeSet does not allows null element

• Example Program

LinkedHashSet• LinkedHashSet is also an implementation of

Set interface

• LinkedHashSet maintains the insertion order. Elements gets sorted in the same sequence in which they have been added to the Set.

• Example program

ArrayDeque Class• java.util.ArrayDeque implements a double-ended queue, which allows

efficient insertion and deletion at both ends.

• ArrayDeque is a resizable array implementation of the Deque interface

• It will perform faster than stack when used as stack and faster than linkedlist when used access by multiple threads

• Null elements are prohibited.

• Example program

Sortingimport java.util.ArrayList;import java.util.Collections;public class Sorting_Array_List {

public static void main(String[] args) {ArrayList<Integer> Integer_sort=new ArrayList<Integer>();In`teger_sort.add(1); Integer_sort.add(0);Integer_sort.add(8); Integer_sort.add(3);//the output will show unsorted integersSystem.out.println("Before sorting"+Integer_sort);//now sorting the integersCollections.sort(Integer_sort);System.out.println("After sorting"+Integer_sort);//we can access this collection with iterator also

}

}####output####Before sorting[1, 0, 8, 3]After sorting[0, 1, 3, 8]

Reverseimport java.util.ArrayList;import java.util.Collections;public class Reverse_ArrayList {

public static void main(String[] args) {ArrayList<Integer> Integer_sort=new ArrayList<Integer>();Integer_sort.add(1);Integer_sort.add(2);Integer_sort.add(3);Integer_sort.add(4);//the output will show sorted integersSystem.out.println("Before reverse"+Integer_sort);//now reverse the integersCollections.reverse(Integer_sort);System.out.println("After sorting"+Integer_sort);//we can access this collection with iterator also

}

}####output####Before reverse[1, 2, 3, 4]After sorting[4, 3, 2, 1]