Professional Documents
Culture Documents
CS8392 - Oop - Unit - 3 - PPT - 3.4
CS8392 - Oop - Unit - 3 - PPT - 3.4
OBJECT ORIENTED
PROGRAMMING
VASANTHA KUMAR V,AP/CSE
Stream
A stream is a sequence of data.
Java application uses an output stream to write data to a destination; it may be a file, an
array, peripheral device or socket.
InputStream
Java application uses an input stream to read data from a source; it may be a file, an array,
peripheral device or socket.
Input Output Stream
Java IO Stream
Java defines two types of streams.
Byte Stream : It provides a convenient means for handling input and output of byte.
Character Stream : It provides a convenient means for handling input and output of
characters. Character stream uses Unicode and therefore can be internationalized.
BYTE STREAM vs CHARACTER
STREAM
BYTE STREAM vs CHARACTER
STREAM
Some important Byte stream classes
Stream class Description
BufferedInputStream Used for Buffered Input Stream.
BufferedOutputStream Used for Buffered Output Stream.
DataInputStream Contains method for reading java standard datatype
DataOutputStream An output stream that contain method for writing java standard data type
FileInputStream Input stream that reads from a file
FileOutputStream Output stream that write to a file.
InputStream Abstract class that describe stream input.
OutputStream Abstract class that describe stream output.
PrintStream Output Stream that contain print() and println() method
Some important Character stream classes
Stream class Description
import java.io.InputStreamReader;
import java.io.IOException;
class CharRead {
System.out.println(c);
}}
Reading Strings
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
class MyInput{
System.out.println(text);
}}
Read from a File
import java.io.BufferedReader; while ((str=br.readLine())!=null)
import java.io.IOException; {
import java.io.File; System.out.println(str);
import java.io.FileReader; }
class ReadTest{ br.close();
public static void main(String[] args)throws IOException{ }
try{ catch(IOException e) {
File fl = new File("C:/EEE/car.java"); e.printStackTrace();
BufferedReader br = new BufferedReader(new FileReader(fl)) ; } }}
String str;
Write into a File
import java.io.BufferedReader; FileWriter fw = new FileWriter(fl) ;
import java.io.IOException; fw.write(str);
import java.io.File; fw.close();
import java.io.FileWriter; }
class WriteTest catch (IOException e)
{ { e.printStackTrace(); }
public static void main(String[] args)throws IOException }}
{
try
{
File fl = new File("C:/EEE/new.txt");
String str="Write this string to my file";
The features of Java stream are
A stream is not a data structure instead it takes input from the Collections, Arrays or
I/O channels.
Streams don’t change the original data structure, they only provide the result as per
the pipelined methods.
Each intermediate operation is lazily executed and returns a stream as a result, hence
various intermediate operations can be pipelined.
Terminal operations mark the end of the stream and return the result.
Different Operations On Streams
Intermediate Operations:
map
The ‘map’ method is used to map each element to its corresponding result.
filter
The ‘filter’ method is used to eliminate elements based on a criteria.
sorted
The ‘sorted’ method is used to sort the stream.
Different Operations On Streams
Terminal Operations:
collect
The collect method is used to return the result of the intermediate operations performed on
the stream.
forEach
Stream has provided a new method ‘forEach’ to iterate each element of the stream.
reduce
The reduce method is used to reduce the elements of a stream to a single value.
EXAMPLE :
import java.util.*;
import java.util.stream.*;
class Demo
{
public static void main(String args[])
{
// create a list of integers
List<Integer> number = Arrays.asList(2,3,4,5);
// demonstration of map method
List<Integer> square = number.stream().map(x -> x*x).
collect(Collectors.toList());
System.out.println(square);
// create a list of String
List<String> names =
Arrays.asList("Reflection","Collection","Stream");
// demonstration of filter method
List<String> result = names.stream().filter(s->s.startsWith("S")).
collect(Collectors.toList());
System.out.println(result);
// demonstration of sorted method
List<String> show =
names.stream().sorted().collect(Collectors.toList());
System.out.println(show);
// create a list of integers
List<Integer> numbers = Arrays.asList(2,3,4,5,2);
// collect method returns a set
Set<Integer> squareSet =
numbers.stream().map(x->x*x).collect(Collectors.toSet());
System.out.println(squareSet);
// demonstration of forEach method
number.stream().map(x->x*x).forEach(y->System.out.println(y));
// demonstration of reduce method
int even =
number.stream().filter(x->x%2==0).reduce(0,(ans,i)-> ans+i);
System.out.println(even);
}
}
OUTPUT:
[4, 9, 16, 25]
[Stream]
[Collection, Reflection, Stream]
[16, 4, 9, 25]
4
9
16
25
6
THANKYOU