Collections Java
Collections Java
==============================================================
1.Wrapper class
- used to convert primitive into object (autoboxing)
and object into primitive (unboxing)
========================================================================
Collections Framework-
-operation includes-
1.insertion
2.searching
3.sorting
4.delete
5.manipulation
//List - interface
//ArrayList - child class of List interface
ArrayList<Integer> arrList = new ArrayList<>();
List<Integer> listArr = new ArrayList<>();
-----------------------------------------------------------------------------------
Framework means-
Iterable Interafece :
Iterator<T> iterator();
T -> Generic
===================================================================================
======
List
- List is a interface
- List is child interface of collection interface.
-We can store data in a list collection in order
-It can have duplicate values
-we can save null elements also in a list
*Ordered data
*Duplicate also allowed.
===================================================================================
================
Arraylist
Questions :-
for(String aa :nameArrayList) {
System.out.println(aa);
}
System.out.println("==============================");
System.out.println("===========================");
nameArrayList.remove(3);
for(String aa :nameArrayList) {
System.out.println(aa);
}
System.out.println("===========================");
System.out.println(nameArrayList.get(3));
System.out.println("===========================");
System.out.println(nameArrayList.contains("Mahesh"));
System.out.println("===========================");
Collections.sort(nameArrayList);
for(String aa :nameArrayList) {
System.out.println(aa);
}
System.out.println("===========================");
Collections.sort(nameArrayList,Collections.reverseOrder());
for(String aa :nameArrayList) {
System.out.println(aa);
}
==============================================================================
LinkedList :
- Linkedlist is faster than arraylist as it uses doubly linked list internally.
- Manipulation of data in linkedlist is faster than arraylist as no bit shifting is
required in memory
-linked list is better choice for manipulating the data.
-Linkedlist is a linear data structure where elements are not stored in contiguous
location.
-There is no default capacity for linked list as it does not allocate memory to the
element before the elements are added to the list.
Example:
List<Integer> linkedList = new LinkedList<>();
linkedList.add(1);
linkedList.add(1);
linkedList.add(4);
linkedList.add(6);
stringLinkedList.add("abc");
stringLinkedList.add("pqr");
stringLinkedList.add("xyz");
stringLinkedList.add("str");
==============================================================================
Vector :
- It is also same like dynamic array which grows in size as per required.
- We can store n number of elements in vector
- vector is synchronized.
- it is deprecated that means it contains legacy methods which are not part of
collections framework now.
Synatax:
List<Integer> lst = new Vector<>();
==================================================================================
Stack:
Example ->
Stack<String> stk = new Stack<>();
stk.push("Shubahm");
stk.push("Akshay");
stk.push("Nilesh");
stk.push("Mahesh");
System.out.println(stk.pop());
System.out.println(stk.pop());
System.out.println(stk.pop());
System.out.println(stk.pop());
===================================================================================
Set
- Set is an interface.
- No duplicate allowed
- unordered
-
Hashset -
- Hashset is class which are used to create collection that uses a hash table for
the storing purpose.
- hashset stores the element by using one mechanish called as " hashing ".
- hashset contains unique elements only.
- Hashset allows to store null values.
- Hashset insert the value in the table according to hashcode of that perticular
element.
- hashset is best approach to search the element
- hashsets initial capicity is 16.
syntax
Set<String> nameSet = new HashSet<>();
System.out.println(fruitSet.contains("Bananas"));
fruitSet.clear();
System.out.println("=================================");
System.out.println(fruitSet.isEmpty());
}
===================================================================================
=======
LinkedHashSet -
syntax:
Set<String> nameSet = new LinkedHashSet<>();
example:
Set<String> carName = new LinkedHashSet<>();
carName.add("Nexon");
carName.add("Kia");
carName.add("MG-Hector");
carName.add("Maruti800");
carName.add("Rapid");
carName.add("Swift");
carName.add("Vento");
carName.add("Vento");
==============================================================
TreeSet -
Example :
System.out.println(countryName);
System.out.println("===================================");
Iterator itr =countryName.descendingIterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
System.out.println("===================================");
System.out.println(countryName.descendingSet());
}
=========================================================
for(Integer aa : nums) {
System.out.println(aa);
}
===========================================================
===================================================================================
===================================
Queue
Priority Queue-
-A priority queue is used when the object are supposed to be performed on the basic
of priority.
-As Queue performs FIFO but sometimes there will be a need where we have to proceed
our object as per priority
then priority queue comes into existence.
-elements of priority queue is ordered according to their natural ordering or by a
comparator provided by queue.
-element with maximum ASCII value will have highest priority.
-null not allowed
-initial capacity is 11
Example
System.out.println(names.peek());
System.out.println(names.poll());
System.out.println(names.poll());
System.out.println(names.peek());
}
==============================================
System.out.println(nums.peek());
System.out.println(nums.poll());
System.out.println(nums.peek());
}
System.out.println("==============================");
dequeObj.removeLast();
Iterator<String> itr2 = dequeObj.iterator();
while(itr2.hasNext()) {
System.out.println(itr2.next());
===================================================================================
====
Array :
}
===============================================================
**********************
for(Integer i:numList) {
System.out.println(i);
}
Collections.sort(numList);
System.out.println("After sorting....");
for(Integer i:numList) {
System.out.println(i);
}
System.out.println("Max value--------------");
System.out.println(Collections.max(numList));
System.out.println("Min value--------------");
System.out.println(Collections.min(numList));
}
===================================================================================
===
package com.syntel.program.oops;
import java.util.Objects;
public Flowers() {
@Override
public int hashCode() {
return Objects.hash(getName());
}
@Override
public boolean equals(Object obj) {
// if(this == obj) return true;
// if(!(obj instanceof Flowers)) return false;
Flowers that =(Flowers)obj;
return this.getName().equals(that.getName());
}
}
-------------------------------------------------------------------
for(Flowers fl :flowerSet) {
System.out.println(fl.getId()+" "+fl.getName()+" "+fl.getPrice());
}
==========================================================================
ArrayList Example with predefined methods............
--------------------------------------------------------------------------
System.out.println(fruitList.size());
System.out.println(fruitList.contains("Pinapple"));
System.out.println(fruitList.isEmpty());
Collections.sort(fruitList);
System.out.println("After sorting...");
for(String a : fruitList) {
System.out.println(a);
}
Collections.sort(fruitList,Collections.reverseOrder());
System.out.println("After reverse sorting...");
for(String a : fruitList) {
System.out.println(a);
}
fruitList.clear();
System.out.println(fruitList.isEmpty());
}
===========================================================================
Collections -
Collection -