The conversion between list set map and array is analyzed in detail

  • 2020-04-01 02:17:50
  • OfStack

1. Turn the set list
Set Set = new HashSet(new ArrayList());  

2. Turn the set list
List List = new ArrayList(new HashSet());

3. Turn the array into a list
List stooges = Arrays. AsList ("Larry", "Moe", "Curly");
or
String[] arr = {"1", "2"};
List the List = Arrays. AsList (arr);

4. Convert array to set
Int [] a = {1, 2, 3};
Set Set = new HashSet(arrays.aslist (a));

5. Map related operations.
Map Map = new HashMap();
The map. The put (" 1 ", "a");
The map. The put (' 2 ', 'b');
The map. The put (' 3 ', 'c');
System. The out. Println (map);
// output all values
System. The out. Println (map) keySet ());
// output all keys
System. The out. Println (map) values ());
// converts the value of the map to List
List List = new ArrayList(map.values());
System. Out. Println (list);
// converts the value of map to Set
Set Set = new HashSet(map.values());
System. The out. Println (set);

6. Turn the list array
List the List = Arrays. AsList (" a ", "b");
String[] arr = (String[]) list.toarray (new String[list.size()));
System. The out. Println (Arrays. ToString (arr));


Related articles: