Saturday, 4 July 2020

How to Convert a HashSet to an ArrayList in java programming languages ?

How to Convert a HashSet to an ArrayList in java programming languages ?



simple you can see this example of source code and learn how to convert HashSet to an ArraList.

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class HashSetToArrayList {

  public static void main(String[] args) {
  
    Set<String> hashset = new HashSet<String>();
    hashset.add("A");
    hashset.add("B");
    hashset.add("C");
    List<String> list = new ArrayList<String>(hashset);
    System.out.println(list.toString());
    
  }
}


you can see an other example how use Hash set  convert in an ArraList in java programming languages

This code example shows how to convert ArrayList to HashSet:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ArrayListToHashSet {

  public static void main(String[] args) {
    List<String> list = new ArrayList<String>();
    list.add(null);
    list.add("A"); 
    list.add("B");
    Set<String> hashset = new HashSet<String>(list);
    list = new ArrayList<String>(hashset);
    System.out.println(list.toString());
  }
}

The above code example also can be used to remove duplicate items from an ArrayList.


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ArrayListToHashSet {

public static void main(String[] args) {
List list = new ArrayList();
list.add(null);
list.add("A");
list.add("A");
list.add("A");
list.add("A");
list.add("B");
list.add("B");
list.add("B");
list.add("C");
Set hashset = new HashSet(list);
list = new ArrayList(hashset);
System.out.println(list.toString());
}

}

0 comments:

Post a Comment