An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static. Just like classes you can extend one interface from another using the extends keyword as shown below:interface ArithmeticCalculations { public abstract int addition(int a, int b); public abstract int subtraction(int a, int b); } interface MathCalculations extends ArithmeticCalculations { public abstract double squareRoot(int a); public abstract double powerOf(int a, int b); }In the same way you can extend multiple interfaces from an interface using the extends keyword, by separating the interfaces using comma (, ) ... Read More
When you have created an array of a particular data type with fixed size and populate it if you store a value other than its datatype an ArrayStoreException is thrown at the run time.ExampleIn the following Java program, we are creating an Integer array and trying to store a double value in it. Live Demoimport java.util.Arrays; public class ArrayStoreExceptionExample { public static void main(String args[]) { Number integerArray[] = new Integer[3]; integerArray[0] = 12548; integerArray[1] = 36987; integerArray[2] = 555.50; integerArray[3] = 12548; ... Read More
When you are working with collection objects, while one thread is iterating over a particular collection object, if you try to add or remove elements from it, a ConcurrentModificationException will be thrown.Not only that, If you are iterating a collection object, add or remove elements to it and try to iterate its contents again it is considered that you are trying to access the collection object using multiple threads and ConcurrentModificationException is thrown.Example Live Demoimport java.util.ArrayList; import java.util.Iterator; public class OccurenceOfElements { public static void main(String args[]) { ArrayList list = new ArrayList(); ... Read More
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