Collections framework in java

yugandharvadlamudi 1,300 views 13 slides Aug 29, 2016
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

Collection in java,ArrayDeque,Sorting,Reverse order


Slide Content

Collections FrameWork ArrayDqueue Sorting & Reverse Techniques

Collections Framework   The  Java Collections Framework  is a collection of interfaces and classes which 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 L inkedList doesn't provide Random 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 ArrayList each 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 contain duplicate 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 linked list when used access by multiple threads Null elements are prohibited . Example program

Sorting import 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 integers System. out.println ("Before sorting"+ Integer_sort ); //now sorting the integers Collections. 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]

Reverse import 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 integers System. out.println ("Before reverse"+ Integer_sort ); //now reverse the integers Collections. 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]