Java 3 RD
Java 3 RD
Java 3 RD
class CseFinal {
public static void main(String [] args) {
system.out.println(“Cse,Final”);
}}
S Class Description
N
1 BufferedInputStream This class provides methods to read bytes from the buffer.
2 ByteArrayInputStream This class provides methods to read bytes from the byte array.
3 DataInputStream This class provides methods to read Java primitive data types.
4 FileInputStream This class provides methods to read bytes from a file.
5 FilterInputStream This class contains methods to read bytes from the other input streams,
which are used as the primary source of data.
7 PipedInputStream This class provides methods to read from a piped output stream to
which the piped input stream must be connected.
Character stream:
Character stream is used to read and write a single character of data. All the character stream classes are
derived from base abstract classes Reader and Writer.
Reader Class
Writer Class
S Class Description
N
1. BufferedReader This class provides methods to read characters from the buffer.
2. CharArrayReader This class provides methods to read characters from the char array.
3. FileReader This class provides methods to read characters from the file.
4. FilterReader This class provides methods to read characters from the underlying character input
stream.
6 PipedReader This class provides methods to read characters from the connected piped output
stream.
DATA INPUT STREAM AND DATA OUTPUT STREAM TO ACCESS PRIMITIVE DATA TYPES
DATA INPUT STREAM
Data input stream is used to read primitive data types from an input source.
*Constructor of data input stream is data input (input stream in).
*Methods of data input stream (Public string read Line() throws IO Exception
Sample program:
import java.io.DataInputStream;
import java.io.FileInputStream;
class IOTest
{
public void raedFile()
{
try
{
FileInputSttream object
FileInputStream fis=new FileInputStream(“F:\\new folder\\data1.txt”);
DataInputStream dis=new DataInputStream(fis);
int I;
while((i=dis.read())!=-1)
{
system.out.print((char)!);
}}
catch (Exception e)
{
e.printStack Trace();
}}}
Public class DataInputStreamExample
{
Public static vaoid main(String args[])
{
IOTest obj=new IO Test();
Obj.readFile();
}}
COLLECTIONS
A collection is framework provided by JAVA. a collection sometimes called as container is simply an object
that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate and
communicate aggregate data. This framework provides many interfaces and their implemented classes in
order to store group of objects in single entity.
What is a framework in Java
It provides readymade architecture.
It represents a set of classes and interfaces.
It is optional.
What is Collection framework
The Collection framework represents a unified architecture for storing and manipulating a group of
objects. It has:
Interfaces and its implementations, i.e., classes
Algorithm
Hierarchy of collection framework
Array List
Java ArrayList class uses a dynamic array for storing the elements. It is like an array, but there is no size
limit. We can add or remove elements anytime. The ArrayList in Java can have the duplicate elements
also.
Program:
import java.util.*;
public class ArrayListExample1{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>(); //Creating arraylist
list.add("Mango"); //Adding object in arraylist
list.add("Apple");
list.add("Banana");
list.add("Grapes");
System.out.println(list);
}
}
Linked List:
Java Linked List class uses a doubly linked list to store the elements. It provides a linked-list data
structure.
Program:
import java.util.*;
public class LinkedList1{
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());
}
}
}
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
information content of a key is used to determine a unique value, called its “hash code”. The transformation
of the key into its hash code is performed automatically.
Program:
import java.util.*;
class HashSet1
{
public static void main(String args[])
{
HashSet<String> set=new HashSet();
set.add("One");
set.add("Two");
set.add("Three");
set.add("Four");
set.add("Five");
Iterator<String> i=set.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
Hash table:
Hash table was a part of the original java.util and is a concrete implementation of a dictionary. It is an
integration of collection framework.
Program:
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");
for(Map.Entry m:hm.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
MAP INTERFACE
Map interface maps unique keys to values. A key is an object that you use to retrieve a value at a later date.
Given a key and a value you can store the value in a map object. After the value is stored you can retrieve it
by using its key.
Program:
import java.util.*;
class MapExample2{
public static void main(String args[]){
Map<Integer,String> map=new HashMap<Integer,String>();
map.put(100,"Amit");
map.put(101,"Vijay");
map.put(102,"Rahul");
for(Map.Entry m:map.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
Hash map:
The HashMap class uses a hashtable to implement the Map interface. This allows the execution time of basic
operations, such as get() and put(), to remain constant even for large sets.
Programm:
import java.util.*;
public class HashMapExample1{
public static void main(String args[]){
HashMap<Integer,String> map=new HashMap<Integer,String>();//Creating HashMap
map.put(1,"Mango"); //Put elements in Map
map.put(2,"Apple");
map.put(3,"Banana");
map.put(4,"Grapes");
System.out.println("Iterating Hashmap...");
for(Map.Entry m : map.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
} }
ENUMSET:
EnumSet is a specialized implementation of the Set interface for enumeration types. It implements the Set
interface and extends the AbstractSet. Enumset class is a member of the java collections framework and it is
not synchronized. Enumset is much faster than HashSet.
Program:
import java.util.*;
enum days {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
public class EnumSetExample {
public static void main(String[] args) {
Set<days> set = EnumSet.of(days.TUESDAY, days.WEDNESDAY);
// Traversing elements
Iterator<days> iter = set.iterator();
while (iter.hasNext())
System.out.println(iter.next());
}
}
ENUM MAP:
An EnumMap is a specialized implementation of the Map interface for enumeration types. It implements the
Map interface and extends AbstractMap in java. EnumMap is much faster than HashMap.
Program:
import java.util.*;
public class EnumMapExample {
// create an enum
public enum Days {
Monday, Tuesday, Wednesday, Thursday
};
public static void main(String[] args) {
EnumMap<Days, String> map = new EnumMap<Days, String>(Days.class);
map.put(Days.Monday, "1");
map.put(Days.Tuesday, "2");
map.put(Days.Wednesday, "3");
map.put(Days.Thursday, "4");
// print the map
for(Map.Entry m:map.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
EnumMap is not an abstract class therefore, we EnumSet is an abstract class therefore, we can not
can create an instance of this class. create the instance of this class.