In this article, we will understand how to check if a Set is a subset of another set. A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are not allowed. Following are the ways to check if a set is a subset of another set: Using containsAll() Method Using Stream API Using containsAll() Method In Java, the set interface has a method called containsAll() that checks ... Read More
The ArrayList is a resizable array, which is part of java.util package. The difference between a built-in array and an ArrayList in Java is that the size of an array cannot be modified. In this article, we will understand how to pass an ArrayList as a function argument. Following are the ways to pass an ArrayList as a function argument: Using Function Argument Using Return Type Using Function Argument We can pass an ArrayList as a function argument in Java. The function can take an ArrayList as a parameter and perform operations ... Read More
In this article, we will learn how to convert the local time to GMT (Greenwich Mean Time) in Java. We need this conversion when we want to standardize time across different time zones or when working with systems that require GMT. We will cover the following methods to convert local time to GMT: Using ZonedDateTime class Using Calendar Class Using Date Class Using ZonedDateTime Class ZonedDateTime class was introduced in Java 8, which is used for converting local time to GMT. We can use the ... Read More
List Interface is a collection in Java that is used for storing elements in a sequence. It also allows us to save duplicates as well as null values. In this article, we will learn how to empty a list in Java. Following are the ways to empty a list in Java: Using clear() Method Using removeAll() Method Using clear() Method The clear() method of the List interface is used to remove all elements from the list. It removes all the elements but returns nothing. Following is the syntax of the ... Read More
A Set is a Collection that cannot contain duplicate elements (same as the mathematical set abstraction). The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. Finding the difference between two sets means finding the elements that are not similar in both sets. For example, we have set A = {1, 2, 3} and set B = {2, 3, 4}. The difference between these two sets will be {1, 4} because 1 is not in set B and 4 is not in set A. In this article, we will learn how to ... Read More
HashMap is a part of the Java Collections Framework, and it is used for storing key-value pairs. In this article, we will see how to update the value of a HashMap using a key. We will use following methods to update the value of a HashMap: Using put() Method Using compute() Method Using replace() Method Using put() Method The put() method is used to add a key-value pair to the HashMap. If the key already exists, it will update the value that is stored with that ... Read More
What is a LinkedList? A Linked list is a sequence of data structures where each node contains two parts as follows - Data: The value or information stored in the node. Next: A reference (or pointer) to the next node in the sequence. Detecting a Loop in a LinkedList A loop or cycle means that the last node of a linked list is connected back to one of the nodes earlier in the list, creating a cycle. Or, the next pointer of a node points to itself. The following are the ways ... Read More
A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. In this example, we will see how to iterate over a Set in Java using the following methods: Using forEach() Method Using Stream API Using forEach() Method The forEach() method from the Set is used for iterating over each element of the set. We can use this method to perform an action for each element in ... Read More
ArrayList class in Java is a part of the Java Collections Framework and is used for storing a dynamically sized collection of elements. It is similar to an array, but it can grow and shrink in size according to the requirements of the program.Removing Duplicate Elements from an ArrayList In an ArrayList, we can store duplicate elements. In this article, we will see how to remove duplicate elements from an ArrayList in Java. The following are the methods we will use to remove duplicate elements from an ArrayList: Using removeIf() Method ... Read More
In this article, we will learn how to insert an element at a specific index in a Java list. For example, we have a fruit list by certain order and we want to insert a new fruit at a specific index in the list. Following are the ways to insert an element at a specific index in a Java list: Using add() Method Using ListIterator Using Stream API Using add() Method The add() method of the the list interface helps us to insert an element at ... Read More