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

Unit-4 - OOPS With Java

aktu notes of oops with java unit 4

Uploaded by

iamvansh22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
13 views8 pages

Unit-4 - OOPS With Java

aktu notes of oops with java unit 4

Uploaded by

iamvansh22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Unit-4 java collection framework

java Collection:-

java Collection means a single unit of objects. Java Collection framework provides many interfaces
(Set, List, Queue, Deque) and classes (ArrayList , Vector, LinkedList, PriorityQueue, HashSet,
LinkedHashSet, TreeSet).

It provides readymade architecture.

o It represents a set of classes and interfaces.


o It is optional.

the java.util package contains all the classes and interfaces for the Collection framework.

Iterator interface
The Iterable interface is the root interface for all the collection classes. The Collection
interface extends the Iterable interface and therefore all the subclasses of Collection interface
also implement the Iterable interface.

It contains only one abstract method. i.e.,

Iterator interface provides the facility of iterating the elements in a forward direction only.
Methods of Iterator interface
There are only three methods in the Iterator interface. They are:

List Interface
List interface is the child interface of Collection interface. It inhibits a list type data structure
in which we can store the ordered collection of objects. It can have duplicate values.

List interface is implemented by the classes ArrayList, LinkedList, Vector, and Stack.

List <data-type> list1= new ArrayList();


List <data-type> list2 = new LinkedList();
List <data-type> list3 = new Vector();
List <data-type> list4 = new Stack();

Array List:-
The Array List class implements the List interface. It uses a dynamic array to store the
duplicate element of different data types.

import java.util.*;
class class1
{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>();//Creating arraylist
list.add("Ravi");//Adding object in arraylist
list.add("Vijay");
list.add("Ravi");
list.add("Ajay");
//Traversing list through Iterator
Iterator itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next()); } } }

LinkedList:-

LinkedList implements the Collection interface. It uses a doubly linked list


internally to store the elements. It can store the duplicate elements. It maintains
the insertion order and is not synchronized. In LinkedList, the manipulation is fast
because no shifting is required.

Example:-
import java.util.*;
public class class2 {
public static void main(String args[]){
LinkedList<String> al=new LinkedList<String>();
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator<String> itr=al.iterator();
while (itr.hasNext()){
System.out.println(itr.next ());
} }}

Vector:-

Vector uses a dynamic array to store the data elements. It is similar to ArrayList.
However, it is synchronized and contains many methods that are not the part of
Collection framework.

import java.util.*;
public class class1{
public static void main(String args[]){
Vector<String> v=new Vector<String>();
v.add("Ayush");
v.add("Amit");
v.add("Ashish");
v.add("Garima");
Iterator<String> itr=v.iterator();
while(itr.hasNext()){
System.out.println(itr.next()); } }
}

Stack:-
The stack is the subclass of Vector. It implements the last-in-first-out data structure, i.e.,
Stack. The stack contains all of the methods of Vector class and also provides its methods
like boolean push(), boolean peek(), boolean push(object o), which defines its properties.

import java.util.*;
public class class2{
public static void main(String args[]){
Stack<String> stack = new Stack<String>();
stack.push("Ayush");
stack.push("Garvit");
stack.push("Amit");
stack.push("Ashish");
stack.push("Garima");
stack.pop();
Iterator<String> itr=stack.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
} } }

Queue Interface:-

Queue interface maintains the first-in-first-out order. It can be defined as an ordered


list that is used to hold the elements which are about to be processed. There are various
classes like PriorityQueue, Deque, and ArrayDeque which implements the Queue
interface.

Queue<String> q1 = new PriorityQueue();


Queue<String> q2 = new ArrayDeque();

Set Interface:-

Set Interface in Java is present in java.util package. It extends the Collection interface. It
represents the unordered set of elements which doesn't allow us to store the duplicate items.
We can store at most one null value in Set. Set is implemented by HashSet, LinkedHashSet,
and TreeSet.

Set<data-type> s1 = new HashSet<data-type>();


Set<data-type> s2 = new LinkedHashSet<data-type>();
Set<data-type> s3 = new TreeSet<data-type>();

HashSet:-
HashSet class implements Set Interface. It represents the collection that uses a
hash table for storage. Hashing is used to store the elements in the HashSet. It
contains unique items.
import java.util.*;
public class TestJavaCollection7{
public static void main(String args[]){
//Creating HashSet and adding elements
HashSet<String> set=new HashSet<String>();
set.add("Ravi");
set.add("Vijay");
set.add("Ravi");
set.add("Ajay");
//Traversing elements
Iterator<String> itr=set.iterator();
while(itr.hasNext()){
System.out.println(itr.next()); } }
}

LinkedHashSet:-

LinkedHashSet class represents the LinkedList implementation of Set Interface. It extends the
HashSet class and implements Set interface. Like HashSet, It also contains unique elements.
It maintains the insertion order and permits null elements.

Example :-
import java.util.*;
public class TestJavaCollection8{
public static void main(String args[]){
LinkedHashSet<String> set=new LinkedHashSet<String>();
set.add("Ravi");
set.add("Vijay");
set.add("Ravi");
set.add("Ajay");
Iterator<String> itr=set.iterator();
while(itr.hasNext()){
System.out.println(itr.next()); } }
}

SortedSet Interface:-

SortedSet is the alternate of Set interface that provides a total ordering on its elements. The
elements of the SortedSet are arranged in the increasing (ascending) order. The SortedSet
provides the additional methods that inhibit the natural ordering of the elements.

SortedSet<data-type> set = new TreeSet();

TreeSet:-

Java TreeSet class implements the Set interface that uses a tree for storage. Like HashSet,
TreeSet also contains unique elements. However, the access and retrieval time of TreeSet is
quite fast. The elements in TreeSet stored in ascending order.

import java.util.*;
public class TestJavaCollection9{
public static void main(String args[]){
//Creating and adding elements
TreeSet<String> set=new TreeSet<String>();
set.add("Ravi");
set.add("Vijay");
set.add("Ravi");
set.add("Ajay");
//traversing elements
Iterator<String> itr=set.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
} } }

Java Map Interface:-


A map contains values on the basis of key, i.e. key and value pair. Each key and value pair is
known as an entry. A Map contains unique keys.

there are two interfaces for implementing Map in java: Map and SortedMap, and three
classes: HashMap, LinkedHashMap, and TreeMap.

A Map doesn't allow duplicate keys, but you can have duplicate values. HashMap and
LinkedHashMap allow null keys and values, but TreeMap doesn't allow any null key or
value.

HashMap:-HashMap is the implementation of Map, but it doesn't maintain any order.

LinkedHashMap:-LinkedHashMap is the implementation of Map. It inherits HashMap


class. It maintains insertion order
.
TreeMap:-TreeMap is the implementation of Map and SortedMap. It maintains ascending
order.
Java Hashtable class:-

Java Hashtable class implements a hashtable, which maps keys to values. It inherits
Dictionary class and implements the Map interface.

o A Hash table is an array of a list. Each list is known as a bucket. The position of the
bucket is identified by calling the hash code() method. A Hash table contains values
based on the key.
o Java Hash table class contains unique elements.
o Java Hash table class doesn't allow null key or value.
o Java Hash table class is synchronized.
o The initial default capacity of Hash table class is 11 whereas load Factor is 0.75.

public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>,

Cloneable, Serializable.

Hashtable

class Parameters

K: It is the type of keys maintained by this map.


V: It is the type of mapped values.
Example:-
import java.util.*;
class Hashtable1{
public static void main(String args[]){
Hashtable<Integer,String> hm=new Hashtable<Integer,String>();
hm.put(100,"Amit");
hm.put(102,"Ravi");
hm.put(101,"Vijay");
hm.put(103,"Rahul");
hm.forEach(System.out::println);
// for(Map.Entry m:hm.entrySet()){
// System.out.println(m.getKey()+" "+m.getValue());
}
}

Java Comparable interface

Comparator interface:-

Java Comparator interface is used to order the objects of a user-defined class.


This interface is found in java.util package and contains 2 methods
compare(Object obj1,Object obj2) and equals(Object element).

It provides multiple sorting sequences, i.e., you can sort the elements on the basis
of any data member, for example, rollno, name, age or anything else.

Java 8 Comparator interface

Java 8 Comparator interface is a functional interface that contains only one


abstract method. Now, we can use the Comparator interface as the assignment
target for a lambda expression or method reference.

The properties object contains key and value pair both as a string. The java.util.Properties
class is the subclass of Hash table.

It can be used to get property value based on the property key. The Properties class provides
methods to get data from the properties file and store data into the properties file. Moreover,
it can be used to get the properties of a system.

Advantage of the properties file

Recompilation is not required if the information is changed from a properties file: If any
information is changed from the properties file, you don't need to recompile the java class. It
is used to store information which is to be changed frequently.

Example :-

import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args)throws Exception{ FileReader reader=new F
ileReader("db.properties");

Properties p=new Properties();


p.load(reader);

System.out.println(p.getProperty("user"));
System.out.println(p.getProperty("password"));
}
}

You might also like