The add() method of the ArrayList class helps you to add elements to an array list. It has two variants −add(E e) − This method accepts an object/elements as a parameter and adds the given element at the end of the list.public void add(int index, E element) − This method accepts an element and an integer value representing the position at which we need to insert it and inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).Therefore ... Read More
The contains a () method of the String class accepts Sting value as a parameter, verifies whether the current String object contains the specified string and returns true if it does (else false).Therefore, to for a string in an ArrayList −Get the array list.Using the for-each loop get each element of the ArrayList object.Verify whether each element in the array list contains the required string.If so print the elements.Example Live Demoimport java.util.ArrayList; import java.util.Iterator; public class FindingString{ public static void main(String[] args){ ArrayList list = new ArrayList(); //Instantiating an ArrayList object ... Read More
Java provides Iterator and ListIterator classes to retrieve the elements of the collection objects.The hasNext() methodThe hasNext() method of these interfaces returns true if the collection object has the next element else it returns false.Example Live Demoimport java.util.ArrayList; import java.util.Iterator; public class hasNextExample{ public static void main(String[] args){ ArrayList list = new ArrayList(); //Instantiating an ArrayList object list.add("JavaFX"); list.add("Java"); Iterator it = list.iterator(); System.out.println(it.hasNext()); it.next(); System.out.println(it.hasNext()); it.next(); System.out.println(it.hasNext()); ... Read More
The interface set does not allow duplicate elements. The add() method of this interface accepts elements and adds to the Set object, if the addition is successful it returns true if you try to add an existing element using this method, the addition operations fails to return false.Therefore, to remove redundant elements of an ArrayList object −Get/create the required ArrayList.Create an empty set object.Try to add all the elements of the ArrayList object to set objectives.Clear the contents of the ArrayList using the clear() method.Now, using the addAll() method add the contents of the set object to the ArrayList again.Example Live ... Read More
The DeflaterOutputStream class of Java is used to compress the given data and stream it out to the destination.The write() method of this class accepts the data (in integer and byte format), compresses it and, writes it to the destination of the current DeflaterOutputStream object. To compress a file using this method &Minus;Create a FileInputStream object, by passing the path of the file to be compressed in String format, as a parameter to its constructor.Create a FileOutputStream object, by passing the path of the output file, in String format, as a parameter to its constructor.Create a DeflaterOutputStream object, by passing ... Read More
You can get a part of a Java array in between two specified indexes in various ways.By Copying contents:One way to do so is to create an empty array and copy the contents of the original array from the start index to the endIndex.Example Live Demoimport java.util.Arrays; public class SlicingAnArray { public static int[] sliceArray(int array[], int startIndex, int endIndex ){ int size = endIndex-startIndex; int part[] = new int[size]; //Copying the contents of the array for(int i=0; iarray[i]); part = stream.toArray(); //Copying the contents of the array for(int i=0; i
The class named File of the java.io package represents a file or directory (path names) in the system. To get the list of all the existing files in a directory this class provides the list() and ListFiles() methods.The main difference between them is thatThe list() method returns the names of all files in the given directory in the form of a String array.The ListFiles() method returns the objects (File) of the files in the given directory, in the form of an array of type File.i.e. If you just need the names of the files within a particular directory you can ... Read More
In certain scenarios such as unit testing, or for some application logics you might need to create temporary files.Creating a temporary fileThe File class in Java provides a method with name createTempFile(). This method accepts two String variables representing the prefix (starting name) and suffix(extension) of the temp file and a File object representing the directory (abstract path) at which you need to create the file.ExampleFollowing Java example creates a temporary file named exampleTempFile5387153267019244721.txt in the path D:/SampleDirectoryimport java.io.File; import java.io.IOException; public class TempararyFiles { public static void main(String args[]) throws IOException { String prefix = ... Read More
Character Streams − These handle data in 16 bit Unicode. Using these you can read and write text data only.The Reader and Writer classes (abstract) are the super classes of all the character stream classes: classes that are used to read/write character streams. Following are the character array stream classes provided by Java −ReaderWriterBufferedReaderBufferedWriterCharacterArrayReaderCharacterArrayWriterStringReaderStringWriterFileReaderFileWriterInputStreamReaderInputStreamWriterFileReaderFileWriterExampleThe following Java program reads data from a particular file using FileReader and writes it to another, using FileWriter.import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class IOStreamsExample { public static void main(String args[]) throws IOException { //Creating FileReader object ... Read More
These handle data in bytes (8 bits) i.e., the byte stream classes read/write data of 8 bits. Using these you can store characters, videos, audios, images etc.The InputStream and OutputStream classes (abstract) are the super classes of all the input/output stream classes: classes that are used to read/write a stream of bytes. Following are the byte array stream classes provided by Java −InputStreamOutputStreamFIleInputStreamFileOutputStreamByteArrayInputStreamByteArrayOutputStreamObjectInputStreamObjectOutputStreamPipedInputStreamPipedOutputStreamFilteredInputStreamFilteredOutputStreamBufferedInputStreamBufferedOutputStreamDataInputStreamDataOutputStreamExampleFollowing Java program reads data from a particular file using FileInputStream and writes it to another, using FileOutputStream.import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class IOStreamsExample { public static void main(String args[]) throws IOException { ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP