Found 2616 Articles for Java

Difference between ArrayList.clear() and ArrayList.removeAll() in java?

Maruthi Krishna
Updated on 15-Oct-2019 10:21:26

770 Views

The ArrayList class in Java is a Resizable-array implementation of the List interface. It allows null values.The clear() method this class removes all the elements from the current List object.Example Live Demoimport java.util.ArrayList; public class ClearExample {    public static void main(String[] args){       //Instantiating an ArrayList object       ArrayList list = new ArrayList();       list.add("JavaFX");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       list.add("Impala");       System.out.println("Contents of the Array List: "+list);       //Removing the sub list       list.clear();       ... Read More

Difference between next() and hasNext() in java collections?

Maruthi Krishna
Updated on 15-Oct-2019 10:18:38

988 Views

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

How to remove the redundant elements from an ArrayList object in java?

Maruthi Krishna
Updated on 15-Oct-2019 10:15:45

376 Views

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

Difference between peek(), poll() and remove() method of Queue interface in java?

Maruthi Krishna
Updated on 06-May-2022 07:40:42

11K+ Views

This represents a collection that is indented to hold data before processing. It is an arrangement of the type First-In-First-Out (FIFO). The first element put in the queue is the first element taken out from it.The peek() methodThe peek() method returns the object at the top of the current queue, without removing it. If the queue is empty this method returns null.Example Live Demoimport java.util.Iterator; import java.util.LinkedList; import java.util.Queue; public class QueueExample {    public static void main(String args[]) {       Queue queue = new LinkedList();       queue.add("Java");       queue.add("JavaFX");       queue.add("OpenCV");   ... Read More

How to compress a file in Java?

Maruthi Krishna
Updated on 15-Oct-2019 10:06:59

4K+ Views

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

How to convert a JSON to Java Object using the Jackson library in Java?

raja
Updated on 07-Sep-2023 00:47:49

37K+ Views

The ObjectMapper class is the most important class in the Jackson library. We can convert a JSON to Java Object using the readValue() method of ObjectMapper class, this method deserializes a JSON content from given JSON content String.Syntaxpublic readValue(String content, JavaType valueType) throws IOException, JsonParseException, JsonMappingExceptionExampleimport java.io.*; import java.util.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; public class JSONToJavaObjectTest {    public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException {       Employee emp1 = new Employee();       emp1.setFirstName("Raja");       emp1.setLastName("Ramesh");       emp1.setId(115);       emp1.getTechnologies().add("Java");       emp1.getTechnologies().add("Selenium");       emp1.getTechnologies().add("Spark"); ... Read More

Difference between the list() and listFiles() methods in Java

Maruthi Krishna
Updated on 15-Oct-2019 08:09:11

690 Views

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

Temporary files in Java

Maruthi Krishna
Updated on 15-Oct-2019 08:04:06

3K+ Views

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

Reading data from keyboard using console class in Java

Maruthi Krishna
Updated on 15-Oct-2019 07:58:15

518 Views

The Console class is used to write/read data from the console (keyboard/screen) devices. It provides a readLine() method which reads a line from the key-board. You can get an object of the Console class using the console() method.Note − If you try to execute this program in a non-interactive environment like IDE it doesn’t work.ExampleFollowing Java program reads data from user using the Console class. Live Demoimport java.io.BufferedReader; import java.io.Console; import java.io.IOException; import java.io.InputStreamReader; class Student {    String name;    int age;    float percent;    boolean isLocal;    char grade;    Student(String name, int age, float percent, boolean isLocal, ... Read More

Character streams in Java

Maruthi Krishna
Updated on 15-Oct-2019 07:53:34

6K+ Views

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

Advertisements