Found 9150 Articles for Object Oriented Programming

Convert array to HashSet in Java

Samual Sam
Updated on 25-Jun-2020 07:54:16

2K+ Views

Create an array and convert it to List −Integer[] arr = { 10, 15, 20, 10, 10, 10, 20, 30, 35, 40, 40}; List l = Arrays.asList(arr);Now, let us convert the above array to HashSet −Set s = new HashSet(l);The following is an example to convert array to HashSet −Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] argv) throws Exception {       Integer[] arr = { 10, 15, 20, 10, 10, 10, 20, 30, 35, 40, 40};       List l = Arrays.asList(arr);       Set s = new HashSet(l);       for (Iterator i = s.iterator(); i.hasNext();) {          Object ele = i.next();          System.out.println(ele);       }    } }Output35 20 40 10 30 15

Iterate over the elements of HashSet in Java

karthikeya Boyini
Updated on 25-Jun-2020 09:45:39

301 Views

Declare a HashSet and add elements −Set hs = new HashSet(); hs.add(20); hs.add(39); hs.add(67); hs.add(79);Now, iterate over the elements −for (Iterator i = hs.iterator(); i.hasNext();) {    Object ele = i.next();    System.out.println(ele); }The following is an example that iterate over the elements of HashSet −Example Live Demoimport java.util.HashSet; import java.util.Iterator; import java.util.Set; public class Demo {    public static void main(String[] argv) throws Exception {       Set hs = new HashSet();       hs.add(20);       hs.add(39);       hs.add(67);       hs.add(79);       System.out.println("Elements = ");       for (Iterator ... Read More

Remove specified element from HashSet in Java

Samual Sam
Updated on 25-Jun-2020 09:46:23

467 Views

To remove specified element from HashSet, use the remove() method.First, declare a HashSet and add elements −Set hs = new HashSet(); hs.add(20); hs.add(39); hs.add(67); hs.add(79); hs.add(81); hs.add(87); hs.add(88);Let’s say you need to remove element 39. For that, use the remove() method −hs.remove(39);The following is an example to remove specified element from HashSet −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       Set hs = new HashSet();       hs.add(20);       hs.add(39);       hs.add(67);       hs.add(79);       hs.add(81);       hs.add(87);     ... Read More

Iterate through elements of HashSet in Java

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

396 Views

Create a HashSet and add elements to it −Set hs = new HashSet(); hs.add(20); hs.add(39); hs.add(67); hs.add(79); hs.add(81); hs.add(87);Try the below given code to iterate through the elements −Iterator i = hs.iterator(); while (i.hasNext()) System.out.println(i.next());To iterate through the elements of HashSet, try the following code −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       Set hs = new HashSet();       hs.add(20);       hs.add(39);       hs.add(67);       hs.add(79);       hs.add(81);       hs.add(87);       hs.add(88);       System.out.println("Elements = ... Read More

Get size of HashSet in Java

Samual Sam
Updated on 25-Jun-2020 09:48:14

354 Views

To get the size of HashSet, use the size() method. Let us create a HashSet and add elements −Set hs = new HashSet(); hs.add(15); hs.add(71); hs.add(82); hs.add(89); hs.add(91);Let us now use size() and get the size of HashSet −hs.size()The following is an example to get the size of HashSet −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       Set hs = new HashSet();       hs.add(15);       hs.add(71);       hs.add(82);       hs.add(89);       hs.add(91);       System.out.println("Elements = "+hs);       ... Read More

Copy all elements of Java HashSet to an Object Array

karthikeya Boyini
Updated on 25-Jun-2020 09:49:16

325 Views

Declare a HashSet and add elements −Set hs = new HashSet(); hs.add(15); hs.add(71); hs.add(82); hs.add(89); hs.add(91); hs.add(93); hs.add(97); hs.add(99);To copy all the elements, use the toArray() method −Object[] obArr = hs.toArray();The following is an example to copy all elements to HashSet to an object array −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       Set hs = new HashSet();       hs.add(15);       hs.add(71);       hs.add(82);       hs.add(89);       hs.add(91);       hs.add(93);       hs.add(97);       hs.add(99);   ... Read More

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

534 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

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

Advertisements