Difference between array Array and collection:
(1) The array is of fixed size, and the same array can only store data of the same type (base type/reference type)
(2) The JAVA collection can store a set of data with an unfixed number of operations. (3) If you don’t know how many objects you need during the program and need to automatically amplify the capacity when there is insufficient space, you need to use the container library, and array is not applicable.
Contact: Use the corresponding toArray() and Arrays.asList() methods to recall the conversion.
The difference between List and ArrayList
1. List is an interface, and the List feature is orderly, which ensures that elements are saved in a certain order.
ArrayList is its implementation class, a List implemented using an array.
Map is an interface, and the Map feature is to find objects based on an object.
HashMap is its implementation class. HashMap's Map is implemented by hash table, which uses the hashcode of the object (hashcode() is an Object method) for quick hash search. (For hash search, you can refer to <<Data Structure>>)
2. Generally speaking, if it is not necessary, the recommended code only deals with List and Map interfaces.
For example: Listlist=newArrayList();
The reason for this is that list is equivalent to a generic implementation. If you want to change the type of list, you only need:
Listlist=newLinkedList();//LinkedList is also the implementation class of List and is also the brother class of ArrayList
In this way, there is no need to modify other codes, which is the elegance of interface programming.
Another example is that in the class method, the following statement is:
privatevoiddoMyAction(Listlist){}
In this way, this method can handle all classes that implement List interface and implement generic functions to a certain extent.
3. If you feel that the performance of ArrayList and HashMap cannot meet your needs during development, you can customize your custom classes by implementing List, Map (or Collection).
List, method of converting Set to array
There are two forms of the toArray function, one has no parameters and the other has parameters. Note that in the form with parameters, the size of the array must be specified.
Program code:
public void convertCollectionToArray() {List list = new ArrayList();Object[] objectArray1 = list.toArray();String[] array1 = list.toArray(new String[list.size()]);Set set = new HashSet();Object[] objectArray2 = set.toArray();String[] array2 = set.toArray(new String[set.size()]);}In turn, the array is converted to List, Set.
Integer[] numbers = {7, 7, 8, 9, 10, 8, 8, 9, 6, 5, 4}; // To convert an array into a Set first we convert it to a List. Next // with the list we create a HashSet and pass the list as the constructor. List list = Arrays.asList(numbers); Set set = new HashSet(list);Note: This cannot be done directly with an int[] array, because the parameters of the asList() method must be an object. Int[] should be converted into Integer[] first. The same is true for other primitive types arrays, which must be converted into the corresponding wrapper type array first.
int[] numbers = {7, 7, 8, 9, 10, 8, 8, 9, 6, 5, 4}; int size = numbers.length; Integer[] array = new Integer[size]; for (int i = 0; i < numbers.length; i++) { Integer integer = numbers[i]; array[i] = integer; } List list = Arrays.asList(array);Summarize
The above is the entire content of this article about the introduction of the difference between Java collections and arrays and the examples of mutual conversion. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
" Example of method of inputting arrays and outputting in reverse order on Java console "
" Expand code examples of Java arrays "
" Detailed explanation of Java array basics "
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!