Java implements methods for converting between lists collections and arrays

  • 2020-04-01 03:44:23
  • OfStack

This article illustrates a Java method for converting between lists, collections, and arrays. Share with you for your reference. The specific implementation method is as follows:

package test;  
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.HashSet; 
import java.util.List; 
import java.util.Set; 
public class Test2 { 
    public static void main(String[] args) { 
        List list = new ArrayList(); 
        list.add("a"); 
        list.add("b"); 
        list.add("c"); 
        list.add("d"); 
        // list.add(1);// Will produce java.lang.ArrayStoreException abnormal  
        //You can turn a list into an array & PI if the data types in the list are the same. < br / >         Object[] array = list.toArray(); 
        System.out.println(" from list The length of the converted object array is: " + array.length); 
        //Casting is required when converting to an array of another type, and the toArray method is used with an argument to the object array, & PI; < br / >         //The contents of the list are put into the parameter array. When the length of the parameter array is less than the number of elements in the list, the length of the array is automatically expanded to fit the length of the list & PI. < br / >         String[] array1 = (String[]) list.toArray(new String[0]); 
        System.out.println(" from list The length of the converted string array is: " + array1.length); 
        //Allocates an array of strings with a length equal to the length of the list & PI; < br / >         String[] array2 = (String[]) list.toArray(new String[list.size()]); 
        System.out.println(" from list The length of the converted string array is: " + array2.length); 
        list.clear(); 
        //Converts the array to list  < br / >         for (int i = 0; i < array.length; i++) { 
            list.add(array[i]); 
        } 
        System.out.println(" Convert the array to list The number of elements is: " + list.size()); 
        list.clear(); 
        //Use the asList method & PI of Arrays directly; < br / >         list = Arrays.asList(array); 
        System.out.println(" Convert the array to list The number of elements is: " + list.size()); 
        Set set = new HashSet(); 
        set.add("a"); 
        set.add("b"); 
        //Converts set to array & PI; < br / >         array = set.toArray(); 
        array1 = (String[]) set.toArray(new String[0]); 
        array2 = (String[]) set.toArray(new String[set.size()]); 
        System.out.println(" from Set The length of the converted object array is: " + array.length); 
        System.out.println(" from Set The length of the converted string array is: " + array2.length); 
        //Array to Set  < br / >         //Once the array is converted to a List, the List is used to construct set&cake; < br / >         set = new HashSet(Arrays.asList(array)); 
        System.out.println(" Convert the array to Set The number of elements is: " + list.size()); 
        //Empty the Set and then convert the array to the full list. < br / >         set.clear(); 
        set.addAll(Arrays.asList(array1)); 
        System.out.println(" Convert the array to Set The number of elements is: " + list.size()); 
    } 
}

I hope this article has been helpful to your Java programming.


Related articles: