Java Stream Classes and ArrayList Methods

Posted by Anonymous and classified in Computers

Written on in English with a size of 2.78 KB

What is a Stream and its Classes in Java?

Introduction
A Stream is a sequence of data that flows between a program and an input/output device. Streams are used to perform input and output operations in Java. Java provides stream classes in the java.io package for reading and writing data. Streams simplify file handling and data communication.

Types of Streams

1. Input Stream

Used to read data from a source such as a file, keyboard, or network. Data flows from the source to the program.

2. Output Stream

Used to write data to a destination such as a file, screen, or network. Data flows from the program to the destination.

Various Stream Classes in Java

1. FileInputStream

Used to read data from a file. It reads data in byte format and is suitable for reading both text and binary files.

2. FileOutputStream

Used to write data to a file. It writes data in byte format and is used for creating and updating files.

3. BufferedInputStream

Reads data through a buffer. This improves the speed of input operations and reduces the number of file access operations.

4. BufferedOutputStream

Writes data through a buffer. This improves output performance and reduces the number of write operations.

Demonstrate 3 ArrayList Collection Methods

Introduction
ArrayList is a class in the Java Collection Framework available in the java.util package. It is used to store a dynamic collection of elements. An ArrayList can grow and shrink automatically as elements are added or removed. It maintains insertion order and allows duplicate elements.

Common ArrayList Methods

1. add() Method

The add() method is used to insert an element into the ArrayList. By default, the element is added at the end of the list. It helps in dynamically increasing the size of the collection.

Purpose: To add new elements to the ArrayList.

2. remove() Method

The remove() method is used to delete an element from the ArrayList. Elements can be removed using their index position or value. It reduces the size of the ArrayList automatically.

Purpose: To remove unwanted elements from the collection.

3. get() Method

The get() method is used to retrieve an element from a specified index. It allows accessing stored elements easily. Index numbering starts from 0.

Purpose: To access elements stored in the ArrayList.

Other Common ArrayList Methods

4. size()

The size() method returns the total number of elements present in the ArrayList.

Related entries: