Found 2620 Articles for Java

What happens when we try to add a duplicate key into a HashMap object in java?

Maruthi Krishna
Updated on 15-Oct-2019 10:32:33

3K+ Views

The HashMap is a class that implements the Map interface. It is based on the Hash table. It allows null values and null keys.You can store key-value pairs in the HashMap object. Once you do so you can retrieve the values of the respective keys but, the values we use for keys should be uniqueDuplicate valuesThe put command associates the value with the specified key. i.e. if we add a key-value pair where the key exists already, this method replaces the existing value of the key with the new value, Example Live Demoimport java.util.HashMap; import java.util.Iterator; import java.util.Map; public class DuplicatesInHashMap ... Read More

How to insert an object in an ArrayList at a specific position in java?

Maruthi Krishna
Updated on 15-Oct-2019 10:29:34

759 Views

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

How to search for a string in an ArrayList in java?

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

19K+ Views

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

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

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

716 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

918 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

355 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

3K+ 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

36K+ 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

650 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

Advertisements