Java Collections JDK8 Features
Java Collections JDK8 Features
Java Collections JDK8 Features
(Simplified)
SRINIVAS GARAPATI
AMEERPET TECHNOLOGIES, NEAR SATYAM THEATRE, OPPOSITE HDFC BANK, AMEERPET, HYDERABAD
Contact for Online Classes: +91 – 911 955 6789 / 7306021113
Practice @ www.onlinejavacompiler.com
Contents
S No Topic Page Num
01 Introduction to Collection Framework 02
02 Wrapper classes 05
03 Generics 06
04 ArrayList 07
05 For-each loop 14
06 Iterator 16
07 ListIterator 17
08 List of Objects (Parameterized constructor approach) 18
09 List of Objects (POJO class approach) 25
10 ArrayList – Case Studies 28
11 ArrayList Operations – Menu driven approach 35
12 ArrayList Employee CRUD – Menu Driven 37
13 Vector 40
14 Stack 43
15 LinkedList 45
16 Set Interface 46
17 Map Interface 48
18 Comparator 55
19 Java8 Comparator 57
20 Java8 features 59
21 Static methods 61
22 Functional Interface 63
23 Lambda Expression 64
24 Method References 67
25 forEach() method 68
26 Stream API 70
27 Map() and Filter() 71
28 Collectors Class 73
29 Collections – Interview Questions 74
30 JDK8 – Interview Questions 77
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 1
Practice @ www.onlinejavacompiler.com
Collection Framework
Introduction:
Programming languages and Technologies are used to develop applications.
Applications are used in communication.
Applications store and process information.
Banking Application –
Store customers information and transactions information.
Customers use Banking application to communicate with Banking Employee.
Linear Data Structures: arrange the data sequentially in which elements are connected to its
previous and next adjacent.
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 2
Practice @ www.onlinejavacompiler.com
Non-Linear Data Structures: in which one element connected to multiple elements and the
elements arranged in two-dimensional or multi-dimensional format.
Define Collection?
Collection is a group of objects.
Examples, List, Set, Queue, Map etc.
Collection Hierarchy: The following diagram represents interfaces and classes available in java
Collection Framework
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 3
Practice @ www.onlinejavacompiler.com
2. Vector:
a. Accessing elements much faster.
b. Insertions and Deletions are slower – shifting elements takes time.
3. Stack:
a. Stack follows Last In First Out (LIFO) rule.
b. Inserting and removing elements from one end called TOP.
4. LinkedList:
a. Accessing element slower, nodes-based access.
b. Insertions and Deletions are faster – No shifting of elements.
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 4
Practice @ www.onlinejavacompiler.com
Wrapper classes:
Collection stores only objects (not primitive data).
Wrapper classes providing functionality to perform conversions like
o Primitive -> Object (Boxing)
o Object -> Primitive (Un boxing)
These conversions become automated since JDK5
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 5
Practice @ www.onlinejavacompiler.com
Generics
Generics:
As we know, collection only store objects.
Generics introduced in JDK5.
Generics are used to specify what type of objects allowed to store into Collection.
For Example,
Collection<Object> c = new Collection<Object>();
c.add(10);
Downcast to Integer:
Integer x = c.get(0);
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 6
Practice @ www.onlinejavacompiler.com
ArrayList Collection
ArrayList:
ArrayList is an ordered collection and allow duplicates.
ArrayList is index based.
Processing elements much faster (index based)
Insertions and Deletions are slower (shifting of elements takes time)
Methods:
Name Description
int size() Returns the number of elements in this list.
boolean add(E e) Appends the specified element to the end of this list
Object remove(int index) Removes the element at the specified position in this list
void clear() Removes all of the elements from this list
void add(int index, E element) Inserts element at the specified position in this list
Object get(int index) Returns the element at the specified position in this list
boolean isEmpty() Returns true if this list contains no elements
Object set(int index, E element) Replaces the element at the specified position in this list
with the specified element
boolean contains(Object o) Returns true if this list contains the specified element
int indexOf(Object o) Returns the index of the first occurrence of the specified
element, or -1 if this list does not contain the element.
Iterator<Object> iterator() Returns an iterator over the elements in this list
boolean addAll(Collection c) Appends all of the elements in the specified collection to
the end of this list
Object clone() Returns a shallow copy of this ArrayList instance
ListIterator listIterator(int index) Returns a list iterator over the elements in this list (in
proper sequence), starting at the specified position in
the list.
Object[] toArray() Returns an array containing all of the elements in this list
in proper sequence (from first to last element).
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 7
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 8
Practice @ www.onlinejavacompiler.com
Insert element into specified index: add(int index, E e) method is used to insert element into
specified index.
Instructions to code:
Create ArrayList with 5 elements 10, 20, 30, 40, 50
Read index to insert.
Check whether the index is present or not
If the index is present, then read the value and insert.
If the index is not present, display Error message.
import java.util.*;
class Code {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
System.out.println("List is : " + list);
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 9
Practice @ www.onlinejavacompiler.com
Program to remove all elements from the list: clear() method removes all elements from the
list.
Instructions to code:
Create list with 5 elements.
Display – List is not empty
Remove all elements using clear() method
Display – List is empty.
import java.util.*;
class Code
{
public static void main(String[] args){
List<Integer> list = new ArrayList<Integer>();
for(int i=1 ; i<=5 ; i++)
list.add(i);
list.clear();
System.out.println("List is : " + list);
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 10
Practice @ www.onlinejavacompiler.com
if(list.isEmpty())
System.out.println("List is empty");
else
System.out.println("List is not empty");
}
}
Program to remove index element: remove(int index) method removes element of specified
index.
Instructions to code:
Create list with elements
Read index value.
If the index is valid – remove the element and display list
If the index is not valid – display error message.
import java.util.*;
class Code {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
for(int i=1 ; i<=5 ; i++){
list.add(i);
}
System.out.println("List is : " + list);
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 11
Practice @ www.onlinejavacompiler.com
Program to check whether the list contains element or not: contains() method returns true if
the list has specified element.
import java.util.*;
class Code{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
for(int i=1 ; i<=5 ; i++)
list.add(i);
System.out.println("List is : " + list);
System.out.print("Enter element to check in list : ");
int ele = sc.nextInt();
if(list.contains(ele))
System.out.println("Yes element is present in list");
else
System.out.println("No such element in list");
}
}
Program display the index value of element: indexOf() method returns index of specified
element. It returns -1 if no such element in the list.
import java.util.*;
class Code {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
for(int i=1 ; i<=5 ; i++)
list.add(i);
System.out.println("List is : " + list);
System.out.print("Enter element to find index value : ");
int ele = sc.nextInt();
int index = list.indexOf(ele);
if(index!=-1)
System.out.println("Index value is : " + index);
else
System.out.println("No such element in list");
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 12
Practice @ www.onlinejavacompiler.com
Program to replace the existing value: set(int index, E e) method replace the index element
with specified element.
Instructions to code:
Create ArrayList with elements.
Read the element to replace
Check the element is present or not in the list using contains() method.
If the element is present,
o Read the new element to replace with.
If the element is not present,
o Display error message.
import java.util.*;
class Code {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
for(int i=1 ; i<=5 ; i++){
list.add(i);
}
System.out.println("List is : " + list);
System.out.print("Enter element to replace : ");
int x = sc.nextInt();
if(list.contains(x)) {
System.out.print("Enter new element : ");
int y = sc.nextInt();
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 13
Practice @ www.onlinejavacompiler.com
For-each loop:
It is also called enhanced for loop.
It is since JDK5
For-each loop provides easy syntax to process elements of Array or Collection.
Limitations:
For-each loop can process elements only in forward direction.
For-each loop can process elements one by one only.
Syntax:
for (datatype var : Array/Collection ) {
statements ;
}
System.out.println("List is : ");
for(Integer x : list)
System.out.println(x);
}
}
Display ArrayList element by element using for-loop: get(int index) method is used to
retrieve each element using its index.
import java.util.*;
class Code {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for(int i=1 ; i<=5 ; i++)
list.add(i*5);
System.out.println("List is : ");
for(int i=0 ; i<=list.size()-1 ; i++)
System.out.println(list.get(i));
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 14
Practice @ www.onlinejavacompiler.com
System.out.println("List is : ");
for(int i=list.size()-1 ; i>=0 ; i--)
System.out.println(list.get(i));
}
}
a1.addAll(a2);
System.out.println("a1 list after merge : " + a1);
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 15
Practice @ www.onlinejavacompiler.com
Iterator:
It is an interface.
Iterator providing methods to iterator any collection.
iterator() method returns Iterator object of any collection.
Methods:
1. boolean hasNext(): checks the next element is present or not to iterate.
2. Object next(): returns the next element of iterator object.
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 16
Practice @ www.onlinejavacompiler.com
ListIterator:
It is an interface
listIterator() method returns ListIterator object.
Using ListIterator, we can iterate elements,
o In Forward direction
o In Backward direction
o From specified index value
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 17
Practice @ www.onlinejavacompiler.com
Main.java:
Create 3 Employee objects and add to List
Display details using for-each loop
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 18
Practice @ www.onlinejavacompiler.com
import java.util.*;
class Main {
public static void main(String[] args) {
List<Employee> list = new ArrayList<Employee>();
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 19
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 20
Practice @ www.onlinejavacompiler.com
Main.java:
Collect values from Arrays to create Employee objects.
Display details using for-each loop
import java.util.*;
class Main {
public static void main(String[] args) {
int[] ids = {101, 102, 103, 104, 105};
String[] names = {"Amar", "Annie", "Harini", "Satya", "Jai"};
double[] salaries = {23000, 56000, 43000, 48000, 16000};
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 21
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 22
Practice @ www.onlinejavacompiler.com
Main.java: Read and Add employee details until end user quits.
import java.util.*;
class Main
{
public static void main(String[] args)
{
List<Employee> list = new ArrayList<Employee>();
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("Enter Emp details to add : ");
int id = sc.nextInt();
String name = sc.next();
double salary = sc.nextDouble();
Employee e = new Employee(id, name, salary);
list.add(e);
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 23
Practice @ www.onlinejavacompiler.com
Main.java: Read and Add employee details until end user quits using BufferedReader.
import java.util.*;
import java.io.*;
class Main {
public static void main(String[] args) throws Exception {
List<Employee> list = new ArrayList<Employee>();
BufferedReader br = null;
try{
br = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
System.out.println("Enter Emp details to add : ");
int id = Integer.parseInt(br.readLine());
String name = br.readLine();
double salary = Double.parseDouble(br.readLine());
Employee e = new Employee(id, name, salary);
list.add(e);
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 24
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 25
Practice @ www.onlinejavacompiler.com
Main.java:
Create List to store Employee objects.
Create objects from Arrays and add to List.
import java.util.*;
class Main
{
public static void main(String[] args) {
int[] ids = {101, 102, 103, 104};
String[] names = {"Amar", "Annie", "Harin", "Satya"};
double[] salaries = {35000, 45000, 40000, 38000};
while(true)
{
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 26
Practice @ www.onlinejavacompiler.com
list.add(e);
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 27
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 28
Practice @ www.onlinejavacompiler.com
Main.java:
import java.util.*;
class Main {
public static void main(String[] args) {
int[] ids = {101, 102, 103, 104, 105, 106};
String[] names = {"Amar", "Hareen", "Sathya", "Annie", "Raji", "Harsha"};
double[] salaries = {30000, 35000, 40000, 45000, 42000, 50000};
int[] depts = {20, 10, 20, 20, 30, 10};
String[] locations = {"Hyderabad", "Chennai", "Bangalore", "Hyderabad",
"Pune", "Bangalore"};
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 29
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 30
Practice @ www.onlinejavacompiler.com
if(e.getLocation().equals("Hyderabad"))
{
System.out.println(e.getId() + " ," + e.getName() + " , " + e.getSalary() + " , " +
e.getDept() + " , " + e.getLocation());
count++;
}
}
if(count==0)
{
System.out.println("No employee belongs to Hyderabad");
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 31
Practice @ www.onlinejavacompiler.com
}
}
if(count==0)
{
System.out.println("No employee records founds");
}
Count how many employees working in both Hyderabad and Bangalore locations:
int count=0;
for(Employee e : list)
{
String loc = e.getLocation();
if(loc.equals("Hyderabad") || loc.equals("Bangalore"))
{
count++;
}
}
System.out.println("Count is : " + count);
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 32
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 33
Practice @ www.onlinejavacompiler.com
{
System.out.println(e.getId() + " , " + e.getName() + " , " + e.getSalary() + " , " +
e.getDept() + " , " + e.getLocation());
count++;
}
}
if(count==0)
{
System.out.println("No employee found");
}
Display details whose salary below 40000 and not belongs to Hyderabad:
int count=0;
for(Employee e : list)
{
if(e.getSalary()<40000 && !(e.getLocation().equals("Hyderabad")))
{
System.out.println(e.getId() + " , " + e.getName() + " , " + e.getSalary() + " , " +
e.getDept() + " , " + e.getLocation());
count++;
}
}
if(count==0)
{
System.out.println("No employee found");
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 34
Practice @ www.onlinejavacompiler.com
Following program explains how to perform ArrayList operations such as Append, Insert,
Replace, Update, Remove, Sort, Reverse and Display:
import java.util.*;
class Main
{
public static void main(String[] args){
List<Integer> list = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("1.Append \n2.Insert \n3.Replace \n4.Remove
\n5.Display \n6.Sort \n7.Reverse \n8.Quit");
if(list.contains(ele)){
int index = list.indexOf(ele);
System.out.print("Enter new element : ");
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 35
Practice @ www.onlinejavacompiler.com
int x = sc.nextInt();
list.set(index, x);
System.out.println("Element replaced");
}
else
System.out.println("No such element in list");
}
else if(ch==4){
System.out.print("Enter element to remove : ");
int ele = sc.nextInt();
if(list.contains(ele)){
int index = list.indexOf(ele);
list.remove(index);
System.out.println("Element removed");
}
else
System.out.println("No such element to remove");
}
else if(ch==5){
if(list.isEmpty())
System.out.println("Empty list");
else
System.out.println("List is : " + list);
}
else if(ch==6){
Collections.sort(list);
System.out.println("List sorted");
}
else if(ch==7){
Collections.reverse(list);
System.out.println("List reversed");
}
else if(ch==8){
System.out.println("End");
System.exit(1);
}
else
System.out.println("Invalid choice");
}
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 36
Practice @ www.onlinejavacompiler.com
This program explains how to add employee details, display details of specific ID, remove
employee and update the details of employee:
import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
List<Employee> list = new ArrayList<Employee>();
while(true){
System.out.println("1.Add Record");
System.out.println("2.Display Record");
System.out.println("3.Display All");
System.out.println("4.Update Record");
System.out.println("5.Delete Record");
System.out.println("6.Exit");
if(ch==1){
System.out.println("Enter details :");
int id = sc.nextInt();
String name = sc.next();
double salary = sc.nextDouble();
boolean found=false;
for(Employee e : list){
if(e.id == id){
System.out.println("Name : " + e.name);
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 37
Practice @ www.onlinejavacompiler.com
boolean found=false;
for(Employee e : list){
if(e.id == id){
System.out.print("Enter sal to update: ");
double salary = sc.nextDouble();
e.salary = salary;
System.out.println("Record updated");
found = true;
break;
}
}
if(!found)
System.out.println("Invalid ID");
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 38
Practice @ www.onlinejavacompiler.com
}
else if(ch==5){
if(list.isEmpty())
{
System.out.println("Empty list");
}
else
{
System.out.print("Enter id : ");
int id = sc.nextInt();
boolean found=false;
for(Employee e : list)
{
if(e.id == id)
{
int index = list.indexOf(e);
list.remove(index);
System.out.println("Removed");
found = true;
break;
}
}
if(!found)
System.out.println("Invalid ID");
}
}
else if(ch==6){
System.out.println("End");
System.exit(1);
}
else
{
System.out.println("Invalid choice");
}
}
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 39
Practice @ www.onlinejavacompiler.com
Vector:
Vector implements List.
Vector allow duplicates and follow insertion order.
Vector is synchronized by default.
import java.util.*;
class Code
{
public static void main(String[] args)
{
Vector<Integer> v = new Vector<Integer>();
for (int i=1 ; i<=5 ; i++){
v.add(i*5);
}
System.out.println("Vector : " + v);
}
}
Enumeration:
Vector is legacy(old) class since first version of JDK.
Enumeration interface used to process vector element by element.
elements() method of Vector class returns Enumeration-interface.
Methods of Enumeration:
1. hasMoreElements(): is used to check the element is present or not in Enumeration
2. nextElement(): returns the next element in the enumeration.
import java.util.*;
class Code
{
public static void main(String[] args)
{
Vector<Integer> v = new Vector<Integer>();
for (int i=1 ; i<=5 ; i++){
v.add(i*5);
}
System.out.println("Vector : ");
Enumeration<Integer> en = v.elements();
while(en.hasMoreElements())
{
Integer x = en.nextElement();
System.out.println(x);
}
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 40
Practice @ www.onlinejavacompiler.com
ArrayList is not Synchronized: We get odd results when we try to add elements into ArrayList
from multiple threads.
import java.util.*;
class Test
{
static ArrayList<Integer> list = new ArrayList<Integer>();
}
class First extends Thread
{
public void run(){
for (int i=1 ; i<=100000 ; i++)
{
Test.list.add(i);
}
}
}
class Second extends Thread
{
public void run(){
for (int i=1 ; i<=100000 ; i++)
{
Test.list.add(i);
}
}
}
class Code
{
public static void main(String[] args) throws Exception {
First f = new First();
Second s = new Second();
f.start();
s.start();
f.join();
s.join();
System.out.println("List size is : " + Test.list.size());
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 41
Practice @ www.onlinejavacompiler.com
Vector is synchronized by default: Vector is thread safe, hence we get perfect results when we
try to add elements from multiple threads
import java.util.*;
class Test
{
static Vector<Integer> list = new Vector<Integer>();
}
class First extends Thread
{
public void run(){
for (int i=1 ; i<=100000 ; i++)
{
Test.list.add(i);
}
}
}
class Second extends Thread
{
public void run(){
for (int i=1 ; i<=100000 ; i++)
{
Test.list.add(i);
}
}
}
class Code
{
public static void main(String[] args) throws Exception
{
First f = new First();
Second s = new Second();
f.start();
s.start();
f.join();
s.join();
System.out.println("Vector size is : " + Test.list.size());
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 42
Practice @ www.onlinejavacompiler.com
Stack
Stack:
Stack is an extension of Vector class.
It follows LIFO – Last In First Out Rule
Methods are:
1. boolean empty() : Tests the stack is empty or not
2. Object peek(): returns the top element of stack but not remove
3. Object pop(): returns the top element of stack and removes
4. void push(Object e): push element on to the stack
import java.util.*;
class Code
{
public static void main(String[] args) throws Exception {
Stack<Integer> stk = new Stack<Integer>();
stk.push(10);
stk.push(20);
stk.push(30);
stk.push(40);
System.out.println("Stack is : " + stk);
stk.push(50);
stk.push(60);
System.out.println("Stack is : " + stk);
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 43
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 44
Practice @ www.onlinejavacompiler.com
Linked List
LinkedList:
LinkedList implements List
LinkedList allow duplicates and ordered collection.
Linked List store elements in the form of nodes and connect with links.
Methods are:
boolean add(E e) Appends the specified element to the end of this list.
void add(int index, E element) Inserts the specified element at the specified position
void addFirst(E e) Inserts the specified element at the beginning of this list.
void addLast(E e) Appends the specified element to the end of this list.
void clear() Removes all of the elements from this list.
boolean contains(Object o) Returns true if this list contains the specified element.
E get(int index) Returns the element at the specified position in this list.
E getFirst() Returns the first element in this list.
E getLast() Returns the last element in this list.
Iterator descendingIterator() Returns an iterator over the elements in this reverse.
int indexOf(Object o) Returns element index or else -1
ListIterator listIterator(int index) Create iterator from specified index.
E remove(int index) Removes the element at the specified position in this list.
E removeFirst() Removes and returns the first element from this list.
E removeLast() Removes and returns the last element from this list.
E set(int index, E element) Replace index element with specified element.
int size() Returns the number of elements in this list.
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 45
Practice @ www.onlinejavacompiler.com
Set Interface
Set:
Set doesn’t allow duplicates.
Set is not index based.
HashSet Methods are:
boolean add(E e) Adds the specified element to this set if it is not already present.
void clear() Removes all of the elements from this set.
Object clone() Returns a shallow copy: the elements themselves are not cloned.
boolean contains(Object o) Returns true if this set contains the specified element.
boolean isEmpty() Returns true if this set contains no elements.
Iterator<E> iterator() Returns an iterator over the elements in this set.
boolean remove(Object o) Removes the specified element from this set if it is present.
int size() Returns the number of elements in this set (its cardinality).
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 46
Practice @ www.onlinejavacompiler.com
set.add(30);
set.add(20);
set.add(10);
System.out.println("Set : " + set);
}
}
Output: 50, 40, 30, 20, 10
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 47
Practice @ www.onlinejavacompiler.com
Map Interface
Map:
Store values using keys
Map = {key=value, key=value, key=value ….}
Keys must be unique in Map
Values can be duplicated.
For example, book names(unique) with prices(duplicates)
o Books = {C=300.0 , C++=300.0, Java=350.0, Python=330.0};
Methods are:
Method Description
put(K key, V value) store value using key.
V get(Object key) return value of specified key.
boolean isEmpty() Returns true if this map contains key-values.
void clear() Removes all elements.
boolean containsKey(Object key) Returns true if map contains specified key.
Set<K> keySet() Returns a Set of keys contained in this map.
remove(Object key) Removes key-value of specified key.
replace(K key, V value) Replace the value of specified key with given value.
int size() Returns the number of key-values in map.
Collection<V> values() Returns Collection of values in this map.
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 48
Practice @ www.onlinejavacompiler.com
Implementations of Map:
1. HashMap: doesn’t maintain insertion order. Allows only one null key
2. LinkedHashMap: maintains insertion order. Allows only one null key
3. TreeMap: maintains sorted order of keys. It doesn’t allow null key.
4. Hashtable: It is call legacy class. It maintains sorted order. It doesn’t allow null key.
import java.util.*;
class Code {
public static void main(String[] args) {
//Map<Integer,String> map = new LinkedHashMap<Integer,String>();
Map<Integer,String> map = new TreeMap<Integer,String>();
map.put(50, "Fifty");
map.put(40, "Fourty");
map.put(30, "Thirty");
map.put(20, "Twenty");
map.put(10, "Ten");
System.out.println("Map : " + map);
}
}
Set<K> keySet():
We cannot iterate the map object either by using Iterator or using for-each loop.
First we need to collect all keys of map using keySet() method.
We iterate keys set and get values by specifying each key.
import java.util.*;
class Code {
public static void main(String[] args) throws Exception {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(10, "Ten");
map.put(20, "Twenty");
map.put(30, "Thirty");
map.put(40, "Fourty");
System.out.println("Map is : ");
Set<Integer> keys = map.keySet();
for(Integer key : keys){
String value = map.get(key);
System.out.println(key + " = " + value);
}
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 49
Practice @ www.onlinejavacompiler.com
System.out.println("Map is : ");
Set<String> keys = map.keySet();
Iterator<String> itr = keys.iterator();
while(itr.hasNext()) {
String key = itr.next();
Double value = map.get(key);
System.out.println(key + " = " + value);
}
}
}
if(ch==1) {
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 50
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 51
Practice @ www.onlinejavacompiler.com
else if(ch==5){
System.out.println("End");
System.exit(1);
}
else
System.out.println("Invalid choice");
}
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 52
Practice @ www.onlinejavacompiler.com
Main.java:
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map<Integer, Account> map = new HashMap<Integer, Account>();
while(true){
System.out.println("1. Add Account");
System.out.println("2. Update Location");
System.out.println("3. Display Account");
System.out.println("4. Remove Account");
System.out.println("5. Quit");
if(ch==1){
System.out.print("Enter Account Number : ");
Integer number = sc.nextInt();
if(map.containsKey(number)){
System.out.println("Account already exists");
}
else{
System.out.print("Enter Name :");
String name = sc.next();
System.out.print("Enter Balance :");
double balance = sc.nextDouble();
System.out.print("Enter Location :");
String location = sc.next();
map.put(number, acc);
System.out.println("Account added");
}
}
else if(ch==2){
System.out.print("Enter Account Number : ");
int number = sc.nextInt();
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 53
Practice @ www.onlinejavacompiler.com
if(map.containsKey(number)){
System.out.print("Enter location : ");
String location = sc.next();
Account acc = map.get(number);
acc.setLocation(location);
System.out.println("Location updated");
}
else
System.out.println("Error : Invalid acc-number");
}
else if(ch==3){
System.out.print("Enter Account Number : ");
int number = sc.nextInt();
if(map.containsKey(number)){
Account acc = map.get(number);
System.out.println("Details : " + acc.getName() + ", " +
acc.getBalance() + ", " + acc.getLocation());
}
else
System.out.println("Error : Invalid Account");
}
else if(ch==4){
System.out.print("Enter Account Number : ");
int number = sc.nextInt();
if(map.containsKey(number)){
map.remove(number);
System.out.println("Account removed");
}
else
System.out.println("Error : Invalid Account");
}
else if(ch==5){
System.out.println("End");
System.exit(1);
}
else
System.out.println("Invalid choice");
}
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 54
Practice @ www.onlinejavacompiler.com
Comparator interface
Comparator:
Comparator is used to order the objects of a user-defined class.
Comparator provides multiple sorting sequences hence we can sort the elements based
on different data members, for example, rollno, name, age or anything else.
Method Description
public int compare(Object o1, Object o2) Compares first and second object in the list.
public boolean equals(Object obj) Compares this object with specified object.
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 55
Practice @ www.onlinejavacompiler.com
Main.java:
import java.util.*;
import java.io.*;
class Main
{
public static void main(String args[]){
ArrayList<Student> list=new ArrayList<Student>();
list.add(new Student(101,"Vijay",23));
list.add(new Student(106,"Ajay",27));
list.add(new Student(105,"Jai",21));
list.add(new Student(103, "Amar", 13));
System.out.println("Sort by age");
Collections.sort(list,new AgeComparator());
System.out.println("After Sort : ");
for(Student st : list){
System.out.println(st.rollno+" , "+st.name+" , "+st.age);
}
}
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 56
Practice @ www.onlinejavacompiler.com
Main.java:
import java.util.*;
public class Code
{
public static void main(String args[])
{
ArrayList<Student> al=new ArrayList<Student>();
int nums[] = {101, 102, 103, 104};
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 57
Practice @ www.onlinejavacompiler.com
Comparator<Student> cm1=Comparator.comparing(Student::getName);
Collections.sort(al,cm1);
System.out.println("Sorting by Name");
for(Student st: al)
{
System.out.println(st.getRollno()+" , "+st.getName()+" , "+st.getAge());
}
Comparator<Student> cm2=Comparator.comparing(Student::getAge);
Collections.sort(al,cm2);
System.out.println("Sorting by Age");
for(Student st: al)
{
System.out.println(st.getRollno()+" , "+st.getName()+" , "+st.getAge());
}
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 58
Practice @ www.onlinejavacompiler.com
Java-8 Features
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 59
Practice @ www.onlinejavacompiler.com
Features are:
Static Methods in Interface (JDK7)
Default Methods in interface
Functional Interface
Lambda expression
Method references
forEach() method
Stream API
Parallel Streams
Predicates
Purpose of JDK-8:
Using java technology, application development became easy.
Billions of applications developed under java. Day by day the data increasing rapidly with
the use of these applications and data processing become complex.
JDK8 features are the solution to implement data processing techniques easily.
Data processing important – for quick results
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 60
Practice @ www.onlinejavacompiler.com
Note: The object address of implemented class assign to Interface type reference variable.
InterfaceName obj = new ImplementedClass();
interface Test
{
void m1(); // public abstract
}
class Demo implements Test {
public void m1() {
System.out.println("m1...");
}
}
class Main {
public static void main(String[] args) {
Test obj = new Demo(); // upcasting
obj.m1();
}
}
interface Code
{
int a = 10; // public static final
}
class Main {
public static void main(String[] args) {
System.out.println("a value : " + Code.a);
Code.a = 20 ; // Error : final variable cannot be modified
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 61
Practice @ www.onlinejavacompiler.com
Note: Static methods are used to define the common functionality of objects which are
implemented from that interface.
interface CreditCard {
String cartType = "VISA-Platinum";
static void benefits(){
System.out.println("Benefits on Flying, Dining and more");
}
}
Default Methods:
Defining a method with default keyword.
We can access Default methods through object reference.
Note: Default methods allow the interfaces to have methods with implementation without
affecting the classes that implement the interface.
interface Vehicle{
default String airBags(){
return "Two airbags";
}
default String alarmOn(){
return "at speed of 100";
}
int maxSpeed();
}
class Alto implements Vehicle{
public int maxSpeed(){
return 160;
}
}
class Swift implements Vehicle{
public int maxSpeed(){
return 220;
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 62
Practice @ www.onlinejavacompiler.com
Functional Interface:
Interface that accepts only one abstract method.
We must define with annotation @FunctionalInterface.
Functional Interface allow static and default methods.
@FunctionalInterface
interface Test
{
void m1();
void m2(); // Error :
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 63
Practice @ www.onlinejavacompiler.com
Lambda Expressions
2. Through Anonymous inner class: Defining a class without identity is called Anonymous
inner class. We always define anonymous class inside a method.
interface Test {
void fun();
}
class Main {
public static void main(String[] args) {
Test obj = new Test() {
public void fun() {
System.out.println("Anonymous fun");
}
};
obj.fun();
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 64
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 65
Practice @ www.onlinejavacompiler.com
@FunctionalInterface
interface Calc
{
void add(int x, int y);
}
class Main
{
public static void main(String[] args)
{
Calc obj = (x, y) -> System.out.println("Sum : " + (x+y));
obj.add(5,3);
obj.add(10,20);
}
}
Lambda expression with return values: Lambda expression automatically returns the value
which is evaluated in expression. We need to specify the return type in Functional Interface
specification.
@FunctionalInterface
interface Calc
{
int add(int x, int y);
}
class Main
{
public static void main(String[] args)
{
Calc obj = (x, y) -> x+y;
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 66
Practice @ www.onlinejavacompiler.com
Method references:
It is JDK8 feature.
It is used to refer any method of functional interface easily.
Any method definition can be assigned to functional interface method identity and
access the using its identity.
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 67
Practice @ www.onlinejavacompiler.com
forEach() method:
forEach() introduced in JDK8 to iterate the collection easily.
forEach() defined in both Iterable interface and in Stream interface.
forEach() method is a Default method.
default void forEach(Consumer<super T>action)
Note: forEach() method takes a single parameter which is a functional interface. So, you can
pass lambda expression or method reference as input.
Iterating List
Defining a List:
List<String> names = Arrays.asList("C", "Java", "Python");
Iterating Set
Defining a Set:
Set<String> uniqueNames = new HashSet<>(Arrays.asList("C", "C++", "Java"));
Iterating Map
Defining a Map:
Map<Integer, String> namesMap = new HashMap<>();
namesMap.put(1, "Java");
namesMap.put(2, "JDBC");
namesMap.put(3, "JSP");
Iterate map:
namesMap.forEach((key, value) -> System.out.println(key + " " + value));
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 68
Practice @ www.onlinejavacompiler.com
Iterate List:
import java.util.*;
import java.util.*;
class Main
{
public static void main(String[] args)
{
List<Integer> list = new ArrayList<>(Arrays.asList(10,20,30,40,50));
list.forEach(x->System.out.println(x));
}
}
Iterate Set:
import java.util.*;
import java.util.*;
class Main
{
public static void main(String[] args)
{
List<Integer> list = new ArrayList<>(Arrays.asList(10,20,30,40,50));
Set<Integer> set = new HashSet<>(list);
set.forEach(x->System.out.println(x));
}
}
Iterate Map:
import java.util.*;
import java.util.*;
class Main
{
public static void main(String[] args)
{
Map<Integer,String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "Servlets");
map.put(3, "JSP");
map.forEach((k, v)->System.out.println(k + " = " + v));
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 69
Practice @ www.onlinejavacompiler.com
Stream API
Stream API:
Stream is a flow of data.
Stream API providing pre-defined functionality by which we can filter the data easily.
We always create Streams to Collection objects and filter the data stored in collections.
Note: Stream is a not a data structure hence it will not hold any information. Stream will not
change the collection object. It just processes the elements of object by without modifying it.
Creating Stream to List: stream() method returns the stream of any collection object
Stream<Integer> st = list.stream();
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 70
Practice @ www.onlinejavacompiler.com
map():
It takes a lambda expression as its only argument, and uses it to change every individual
element in the stream.
Its return value is a new Stream object containing the changed elements.
Creating a Stream:
List<Integer> myList = new ArrayList<Integer>();
myList.add(1);
myList.add(5);
myList.add(8);
Stream<Integer> myStream = myList.stream();
filter():
filter() method takes lambda expression as input and return boolean value.
If filter() method returns true, then the element enter into resultant stream.
filter() method returns stream after filtering the data.
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 71
Practice @ www.onlinejavacompiler.com
Stream<String> st = list.stream();
Stream<String> res = st.filter(s->s.startsWith("S"));
res.forEach(System.out::println);
}
}
list.stream().filter(s->s.length()==4).forEach(System.out::println);
}
}
Program to display only even numbers in the list using stream api:
import java.util.*;
import java.util.stream.*;
class Main {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 9, 3, 7, 1, 4, 6};
List<Integer> list = new ArrayList<Integer>();
for(int x : arr){
list.add(x);
}
System.out.println("List is : " + list);
System.out.println("Even numbers list is : ");
list.stream().filter(s->s%2==0).forEach(System.out::println);
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 72
Practice @ www.onlinejavacompiler.com
Collectors Class
Collectors: Collectors is a final class. It provides methods to collect filtered elements into various
collections, and performing operations on collected data such as counting elements, reverse,
sort etc.
output: [1,4,9,16,25,36,36]
output: [1,4,9,16,25,36]
output: [3,4,5,6,6]
Counting elements:
List<Integer> integers = Arrays.asList(1,2,3,4,5,6,6);
Long collect = integers
.stream()
.filter(x -> x <4)
.collect(Collectors.counting());
Output: 3
Output: 1
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 73
Practice @ www.onlinejavacompiler.com
2. Define Array?
Array: Is a set of similar data elements
long[] accNums;
3. Define Object?
Object is a set of dis-similar data elements. For example, Student details,
Employee details, Customer details etc.
4. Define Collection?
Collection is set of Objects. Set of Student details, Employee details.
7. Define List:
List is ordered
List allow duplicates
List is index based
8. Define Set:
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 74
Practice @ www.onlinejavacompiler.com
9. Define Map:
Map store elements using keys
Keys must be unique
Values can be duplicated.
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 75
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 76
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 77
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 78
Practice @ www.onlinejavacompiler.com
Collectors class providing functionality to collect the processed data into List, Set
or Map through streaming.
19. How to collect sorted elements into list using stream api?
list.stream().sorted().collect(Collectors.toList());
20. Collect the sorted elements into List through streaming and display list?
list.stream().sorted().collect(Collectors.toList()).forEach(System.out::println);
23. How to display the elements greater than 5 in a List using filter() method?
list.stream().filter(n->n>5).forEach(System.out::println);
24. How to collect the elements greater than 5 in List using filter() method?
List<Integer> res = list.stream().filter(n->n>5).collect(Collectors.toList());
@FunctionalInterface
interface Predicate
{
public boolean test(E e);
}
27. How can we create Predicate that display only integers greater than 5?
Predicate<E> pr = n -> n>5;
28. How to filter list elements which are greater than 5 using predicate?
Predicate<Integer> pr = n -> n>5;
list.stream().filter(pr).forEach(System.out::println);
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 79
Practice @ www.onlinejavacompiler.com
29. How to filter list of strings starts with ‘A’ using predicate and parallel stream and please
guarantee about the order of elements?
Predicate<String> pr = s->s.startsWith(“A”);
List.stream().parallel().filter(pr).forEachOrdered(System.out::println);
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 80