The method array, list and set interconversion

Here summarize methods Array, List and Set of into each other.

Array into List

Into the List Array using Arrays.asList () method.

String[] arr= new String[]{"yanggb1", "yanggb2", "yanggb3"};
List list = Arrays.asList(arr);

list.add("yanggb4"); // not ok
list.remove("yanggb2"); // not ok

It is noted that, the object returns Arrays.asList () method is an internal class Arrays, and does not implement the modification method set. So List after the conversion and can not add new elements or remove elements, error.

In fact, Arrays.asList () method is embodied in an adapter mode, but the conversion interface, and background data remains array. This means that, if converted on the list of elements to modify, revise the results will be reflected in the original array, because in fact the original array operations.

Array turn Set

The transfer Array Set Set is set using the constructor.

String[] arr = new String[]{"yanggb1", "yanggb2", "yanggb3"};
Set<String> set = new HashSet<>(Arrays.asList(arr));

set.add(
"yanggb4"); // ok set.remove("yanggb2"); // ok

After the conversion to Set collection can add elements or remove elements, because it is a call to the constructor HashSet new out.

Another point to note is that the operation of the converted Set collection does not affect the original Array.

System.out.println(set); // [yanggb3, yanggb1, yanggb4]
System.out.println(arr[2]); // yanggb3

Can also be seen, conversion out of this way of data relatively Set the original is in reverse order.

List turn Set

Set List will turn again using the constructor Set collection.

String[] arr = new String[]{"yanggb1", "yanggb2", "yanggb3"};
List<String> list = Arrays.asList(arr);
Set<String> set = new HashSet(list);

Set turn List

The Set List can also be transferred using the List constructor.

String[] arr = new String[]{"yanggb1", "yanggb2", "yanggb3"};
Set<String> set = new HashSet<>(Arrays.asList(arr));
List<String> list = new ArrayList<>(set);

List or turn Array Set

List or Array Set turn can be used Collection.toArray () method.

String[] arr = new String[]{"yanggb1", "yanggb2", "yanggb3"};
List<String> list = Arrays.asList(arr);
Set<String> set = new HashSet(list);
String[] arr1 = list.toArray(new String[list.size()]);
String[] arr2 = set.toArray(new String[set.size()]);

Here we must note that the collection of call Collection.toArray (T [] t) method which requires a specific type of the specified elements, otherwise invalid type conversion case (direct compilation error) occurs, this time need only be forcibly converted.

String[] arr = new String[]{"yanggb1", "yanggb2", "yanggb3"};
List list = Arrays.asList(arr);
Set set = new HashSet(list);
String[] arr1 = (String[])list.toArray();
String[] arr2 = (String[])set.toArray();

Also note that the use of Collection.toArray () converts out of the array is an array of new, it will not affect the original array.

 

"Some people obviously look very friendly, but always alone."

Guess you like

Origin www.cnblogs.com/yanggb/p/11765021.html