How to convert ArrayList to HashSet and Set to Array in Java examples



In this tutorial we will learn How to convert ArrayList to HashSet and how to convert Set to Array in Java with program and examples



In projects you must have face many scenarios where you need to convert  ArrayList to HashSet  and  HashSet to ArrayList in Java. In this tutorial there are lots of example which can help you sort out the issue in java.




But, before doing these conversions you must know Similarity and Differences between List vs Set in java.


How to convert  ArrayList to HashSet >
  • Program/Example 1.1 to convert ArrayList to HashSet in java using constructor
  • Program/Example 1.2 to convert ArrayList to HashSet in java using addAll method of HashSet
  • Program/Example 1.3 to convert ArrayList to HashSet in java using GUAVA - google collection API
  • Program/Example 1.4 to convert ArrayList to HashSet in java 8

How to convert  HashSet to ArrayList in Java >
  • Program/Example 2.1 to convert HashSet to String ArrayList  in java using constructor
  • Program/Example 2.2 to convert ArrayList to HashSet in java using addAll method of ArrayList
  • Program/Example 2.3 to convert ArrayList to HashSet in java using GUAVA - google collection API
  • Program/Example 2.4 to convert ArrayList to HashSet in java 8

How to convert  ArrayList to HashSet >


  • Program/Example 1.1 to convert ArrayList to HashSet in java using constructor>
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ConvertArrayListToHashSetExample1 {
public static void main(String[] args) {
     List<Integer> arrayList = new ArrayList<Integer>();
     arrayList.add(1);
     arrayList.add(2);
     arrayList.add(1);
     //convert ArrayList to HashSet by
     //passing ArrayList in constructor of HashSet
     Set<Integer> hashSet = new HashSet<Integer>(arrayList);
     // Display set
     System.out.println("hashSet = "+hashSet);
}
}
/*OUTPUT
hashSet = [1, 2]
*/


  • Program/Example 1.2 to convert ArrayList to HashSet in java using addAll method of HashSet>
package convertListToSet;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ConvertArrayListToHashSetExample2 {
public static void main(String[] args) {
     List<Integer> arrayList = new ArrayList<Integer>();
     arrayList.add(1);
     arrayList.add(2);
     arrayList.add(1);
     //convert ArrayList to HashSet by
     //passing ArrayList in addAll method of HashSet
     Set<Integer> hashSet = new HashSet<Integer>();
     hashSet.addAll(arrayList);
     // Display set
     System.out.println("hashSet = "+hashSet);
}
}
/*OUTPUT
hashSet = [1, 2]
*/


  • Program/Example 1.3 to convert ArrayList to HashSet in java using GUAVA - google collection API >
package convertListToSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
public class ConvertArrayListToHashSetExample3_GoogleGuava {
public static void main(String[] args) {
     Set<Integer> hashSet = new HashSet<Integer>();
     List<Integer> arrayList = Lists.newArrayList(1, 2);
     hashSet = Sets.newHashSet(arrayList);
     // Display set
     System.out.println("hashSet = " + hashSet);
}
}
/*OUTPUT
hashSet = [1, 2]
*/
Jar used in above program = google-collections.jar


  • Program/Example 1.4 to convert ArrayList to HashSet in java 8
package convertListToSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class ConvertArrayListToHashSetExample4_inJava8 {
public static void main(String[] args) {
     List<Integer> arrayList = new ArrayList<Integer>();
     arrayList.add(1);
     arrayList.add(2);
     arrayList.add(3);
     //convert ArrayList to HashSet by
     //passing ArrayList in addAll method of HashSet
    
     Set<Object> hashSet = arrayList.stream().collect(Collectors.toSet());
    
     // Display set
     System.out.println("hashSet = "+hashSet);
}
}
/*OUTPUT
hashSet = [1, 2, 3]
*/

So far in this tutorial we learned How to convert ArrayList to HashSet. Now, we will learn how to convert  Set to Array in Java with program and examples.

How to convert  HashSet to ArrayList in Java >


  • Program/Example 2.1 to convert HashSet to String ArrayList  in java using constructor>
package convertSetToList;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ConvertHashSetToArrayListExample1 {
public static void main(String[] args) {
     Set<Integer> set = new HashSet<Integer>();
     set.add(1);
     set.add(2);
     //convert HashSet to ArrayList
     //by passing set in constructor of ArrayList
     List<Integer> arrayList = new ArrayList<Integer>(set);
    
     System.out.println("arrayList = "+arrayList);
}
}
/*OUTPUT
arrayList = [1, 2]
*/


  • Program/Example 2.2 to convert ArrayList to HashSet in java using addAll method of ArrayList>
package convertSetToList;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ConvertHashSetToArrayListExample2 {
public static void main(String[] args) {
     Set<Integer> set = new HashSet<Integer>();
     set.add(1);
     set.add(2);
     List<Integer> arrayList = new ArrayList<Integer>();
     //convert HashSet to ArrayList
     //by passing set in addAll method of ArrayList
     arrayList.addAll(set);
    
     System.out.println("arrayList = "+arrayList);
}
}
/*OUTPUT
arrayList = [1, 2]
*/


  • Program/Example 2.3 to convert ArrayList to HashSet in java using GUAVA - google collection API >
package convertSetToList;
import java.util.List;
import java.util.Set;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
public class ConvertHashSetToArrayListExample3_GoogleGuava {
public static void main(String[] args) {
     Set<Integer> hashSet = Sets.newHashSet(1, 2);
     List<Integer> arrayList = Lists.newArrayList(hashSet);
     System.out.println("arrayList = " + arrayList);
}
}
/*OUTPUT
arrayList = [1, 2]
*/


Jar used in above program = google-collections.jar


  • Program/Example 2.4 to convert ArrayList to HashSet in java 8
package convertSetToList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class ConvertHashSetToArrayListExample4_inJava8 {
public static void main(String[] args) {
     Set<Integer> set = new HashSet<Integer>();
     set.add(1);
     set.add(2);
     set.add(3);
     List<Object> arrayList = set.stream().collect(Collectors.toList());
    
     System.out.println("arrayList = "+arrayList);
}
}
/*OUTPUT
arrayList = [1, 2, 3]
*/

So, In this tutorial we learned How to convert ArrayList to HashSet and how to convert Set to Array in Java with program and examples.

Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>


Collection - List, Set and Map all properties in tabular form



List hierarchy in java - Detailed - ArrayList, LinkedList, vector, CopyOnWriteArrayList classes



Set hierarchy in java - Detailed - HashSet, CopyOnWriteArraySet, LinkedHashSet, TreeSet, ConcurrentSkipListSet, EnumSet classes



Collection in java

Collection - List, Set and Map all properties in tabular form



No comments:

Post a Comment