BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to Convert ArrayList to HashSet in Java

Last Updated: September 23, 2022 by Chaitanya Singh | Filed Under: java

There are several ways to convert ArrayList to HashSet. In this guide, we will discuss the following ways with examples:

  • Passing ArrayList to constructor of HashSet class
  • Iterating ArrayList and adding elements to HashSet using HashSet.add()
  • Using HashSet.addAll() method
  • Using java 8 stream

1. Passing ArrayList to constructor of HashSet class

One of the constructor of HashSet accepts a collection as an argument. Here, we have passed the ArrayList that we have created to the HashSet constructor. This copies all the elements of ArrayList to HashSet.

import java.util.HashSet;
import java.util.ArrayList;
class JavaExample {
  public static void main(String[] args)
  {
    ArrayList<String> arrList = new ArrayList<>();
    arrList.add("C");
    arrList.add("C++");
    arrList.add("Java");
    arrList.add("Python");
    System.out.println("ArrayList Elements: "+arrList);

    // Passing the arrList to HashSet constructor
    // This will copy all the elements of ArrayList to HashSet
    HashSet<String> hSet = new HashSet<>(arrList);

    System.out.println("HashSet Elements: ");
    // Displaying HashSet elements
    for (String s : hSet) {
      System.out.println(s);
    }
  }
}

Output:

Convert ArrayList to HashSet Example Output

2. Iterating ArrayList and adding elements to HashSet using HashSet.add()

Iterating the ArrayList using forEach() method and adding the element to the corresponding HashSet on every iteration.

import java.util.HashSet;
import java.util.ArrayList;
class JavaExample {
  public static void main(String[] args)
  {
    ArrayList<String> arrList = new ArrayList<>();
    arrList.add("C");
    arrList.add("C++");
    arrList.add("Java");
    arrList.add("Python");

    // Creating an empty HashSet
    HashSet<String> hSet = new HashSet<>();

    // Adding each element of ArrayList element to HashSet
    arrList.forEach(item -> { hSet.add(item); });

    System.out.println("HashSet Elements: ");
    // Displaying HashSet elements
    for (String s : hSet) {
      System.out.println(s);
    }
  }
}

Output:

HashSet Elements: 
Java
C++
C
Python

3. Using HashSet.addAll() method

Here, we are using the addAll() method of HashSet class to convert ArrayList to HashSet. In this example, we have an ArrayList of Integer type and we are adding its elements to HashSet of Integer type. In the previous examples, we have seen the conversion of String type ArrayList to HashSet. This time we are converting Integer type ArrayList so notice all the places where we have made changes from String to Integer in the program.

import java.util.HashSet;
import java.util.ArrayList;
class JavaExample {
  public static void main(String[] args)
  {
    ArrayList<Integer> arrList = new ArrayList<>();
    arrList.add(11);
    arrList.add(22);
    arrList.add(33);
    arrList.add(44);

    // Creating an empty HashSet
    HashSet<Integer> hSet = new HashSet<>();

    // Adding all elements of ArrayList in one go
    // The addAll() method copies all elements of a collection
    // to HashSet.
    hSet.addAll(arrList);

    System.out.println("HashSet Elements: ");
    // Displaying HashSet elements
    for (int num : hSet) {
      System.out.println(num);
    }
  }
}

Output:

HashSet Elements: 
33
22
11
44

4. Using java 8 stream

Java 8 introduced the concept of Streams. Here, we are using stream to convert ArrayList to HashSet.

import java.util.ArrayList;
import java.util.HashSet;
import java.util.stream.*;
class JavaExample {
  public static void main(String[] args)
  {
    ArrayList<String> arrList = new ArrayList<>();
    arrList.add("AA");
    arrList.add("BB");
    arrList.add("CC");
    arrList.add("DD");

    // using java 8 stream to convert the ArrayList to
    // HashSet. Notice the typecast HashSet<String>
    HashSet<String> hSet
            = (HashSet<String>)arrList.stream().collect(
                    Collectors.toSet());

    System.out.println("HashSet Elements: ");
    // Displaying HashSet elements
    for (String s : hSet) {
      System.out.println(s);
    }
  }
}

Output:

HashSet Elements: 
AA
BB
CC
DD
Main Article: Collections in Java

Top Related Articles:

  1. LinkedHashMap in Java
  2. How to loop HashMap in java
  3. Add Multiple Items to an ArrayList in Java
  4. How to get the last element of Arraylist?
  5. Java 9 – @SafeVarargs Annotation (with examples)

Tags: Collections, Java-ArrayList, Java-HashSet

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap