Found 33676 Articles for Programming

Remove all elements from a HashSet in Java

Samual Sam
Updated on 25-Jun-2020 09:50:28

2K+ Views

To remove all the elements from HashSet, use the clear() method.Create a HashSet and initialize elements −String strArr[] = { "P", "Q", "R", "S" }; Set s = new HashSet(Arrays.asList(strArr));Now, to remove all the above elements −s.clear()The following is an example to remove all elements from a HashSet −Example Live Demoimport java.util.Arrays; import java.util.HashSet; import java.util.Set; public class Demo {    public static void main(String[] a) {       String strArr[] = { "P", "Q", "R", "S" };       Set s = new HashSet(Arrays.asList(strArr));       strArr = new String[] { "R", "S", "T", "U" };   ... Read More

Add all the elements from a collection to the HashSet in Java

Aishwarya Naglot
Updated on 21-Jul-2025 15:24:12

539 Views

HashSet in Java implements the Set interface, which belongs to the Java Collections Framework and is a part of the java.util package. It stores unique elements, which means we cannot store any duplicate (appearing more than once) values in a HashSet. The given task is to add all the elements of another collection to a HashSet object in Java. For example, if we have a collection ArrayList with elements [1, 3, 4, 5, 5]. Our task is to add all these elements to a HashSet, maybe to remove duplicates from the list, or for any other reason. Adding Elements from ... Read More

Java Program to remove all elements from a set in Java

Shriansh Kumar
Updated on 01-Aug-2024 11:42:49

2K+ Views

We are given a Set that contains multiple elements and our task is to write a Java program to remove elements from the Set. Here, the Set is an Sub Interface of the Java Collection Interface which defines a mathematical set. Removing Set Elements in Java To remove elements from a given set in Java, use the following ways − Using clear() method Using removeAll() method Using Iterator and remove() Using Java clear() method With the help of clear() method, we can remove all elements ... Read More

Get the union of two sets in Java

karthikeya Boyini
Updated on 28-Jan-2025 14:55:40

6K+ Views

The union of two sets combines all the unique elements from both sets into one. In Java we can achieve this using the HashSet class, which ensures no duplicate elements. This article will demonstrate two approaches to finding the union of two sets.  Different Approaches  Following are the two different approaches to getting the union of two sets in Java − Using the addAll() Method Using Streams Using the addAll() Method The simplest way to find the union of two sets is by using the addAll() method of the HashSet class. This ... Read More

Copy all elements of ArrayList to an Object Array in Java

Samual Sam
Updated on 25-Jun-2020 09:55:58

3K+ Views

All the elements of an ArrayList can be copied into an Object Array using the method java.util.ArrayList.toArray(). This method does not have any parameters and it returns an Object Array that contains all the elements of the ArrayList copied in the correct order.A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] args) {       List aList = new ArrayList();       aList.add("Nathan");       aList.add("John");       aList.add("Susan");       aList.add("Betty");       aList.add("Peter");       Object[] objArr ... Read More

Convert an ArrayList to an Array with zero length Array in Java

karthikeya Boyini
Updated on 25-Jun-2020 09:59:33

230 Views

An ArrayList can be converted into an Array using the java.util.ArrayList.toArray() method. This method takes a single parameter i.e. the array of the required type into which the ArrayList elements are stored and it returns an Array that contains all the elements of the ArrayList in the correct order.A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] args) {       List aList = new ArrayList();       aList.add("James");       aList.add("Harry");       aList.add("Susan");       aList.add("Emma");       ... Read More

Get the intersection of two sets in Java

Samual Sam
Updated on 29-Jun-2020 08:07:56

15K+ Views

To get the intersection of two sets, use the retainAll() method. Here are out two set −First set −HashSet set1 = new HashSet (); set1.add("Mat"); set1.add("Sat"); set1.add("Cat");Second set −HashSet set2 = new HashSet (); set2.add("Mat"); set2.add("Cat"); set2.add("Fat"); set2.add("Hat");Get the intersection −set1.retainAll(set2);The following is an example to get the intersection of two sets −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       HashSet set1 = new HashSet ();       HashSet set2 = new HashSet ();       set1.add("Mat");       set1.add("Sat");       set1.add("Cat"); ... Read More

Get the location of an element in Java ArrayList

Alshifa Hasnain
Updated on 11-Mar-2025 19:33:17

2K+ Views

In this article, we will learn to get the location of an element in Java. In Java, an ArrayList is used for storing and manipulating dynamic collections of elements. When working with an ArrayList, you might need to find the index (position) of a specific element. What is the indexOf()? The location of an element in an ArrayList can be obtained using the method java.util.ArrayList.indexOf(). This method returns the index of the first occurrence of the element that is specified. If the element is unavailable in the ArrayList, then this method returns -1. Syntax int index = arrayList.indexOf(element); Finding the ... Read More

Get the asymmetric difference of two sets in Java

karthikeya Boyini
Updated on 25-Jun-2020 10:26:24

696 Views

Use removeAll() method to get the asymmetric difference of two sets.First set −HashSet set1 = new HashSet (); set1.add("Mat"); set1.add("Sat"); set1.add("Cat");Second set −HashSet set2 = new HashSet (); set2.add("Mat");To get the asymmetric difference −set1.removeAll(set2);The following is an example that displays how to get the asymmetric difference between two sets −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       HashSet set1 = new HashSet ();       HashSet set2 = new HashSet ();       set1.add("Mat");       set1.add("Sat");       set1.add("Cat");       ... Read More

Java IdentityHashMap containsValue() method

Samual Sam
Updated on 25-Jun-2020 10:27:13

87 Views

Use the containsValue() method to check for the existence of a value. First, create a IdentityHashMap −Map m = new IdentityHashMap();Add some elements −m.put("1", 100); m.put("2", 200); m.put("3", 300); m.put("4", 150); m.put("5", 110); m.put("6", 50); m.put("7", 90); m.put("8", 250); m.put("9", 350); m.put("10", 450);Now check for the existence of a value −m.containsValue(100))The following is an example to implement containsValue() method −Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] args) {       Map m = new IdentityHashMap();       m.put("1", 100);       m.put("2", 200);       m.put("3", 300);       m.put("4", ... Read More

Advertisements