An introduction to the difference between Java collections and arrays and an instance of conversion between them

  • 2020-12-16 05:56:21
  • OfStack

Array Array and collection differences:

(1) Arrays are fixed in size, and the same array can only hold data of type 1 (base type/reference type).

(2) The JAVA set can store and operate a set of data with an unfixed number. (3) If the program does not know how many objects are needed and needs to automatically expand the capacity when the space is insufficient, it needs to use the container class library, array is not applicable.

Connections: The transformations can be recalled using the corresponding toArray() and Arrays.asList () methods.

The difference between List and ArrayList

1.List is the interface, and the List feature is ordered, ensuring that the elements are saved in the order specified by 1.

ArrayList is its implementation class, which is an List implemented with arrays.

Map is the interface, and the Map feature is to find objects based on 1 object.

HashMap is its implementation class, and HashMap implements Map with hash table, which uses the object's hashcode(hashcode() is Object's method) for fast hash lookup.(For hash lookup, see < < The data structure > > )

2.1 In general, the recommended code deals only with the List,Map interfaces if it is not necessary.

Such as: Listlist = newArrayList ();

The reason for this is that list is equivalent to a generic implementation. To change the type of list, you simply need to:

Listlist=newLinkedList(); //LinkedList is also the implementation class of List and a cousin of ArrayList

This way, you don't need to change the rest of the code, which is the elegance of interface programming.

Another example is in the method of the class, as follows:

privatevoiddoMyAction(Listlist){}

This way the method can handle all classes that implement the List interface,1 to some extent implementing generic functions.

3. If the performance of ArrayList and HashMap does not meet your needs, you can customize your custom classes by implementing List,Map(or Collection).

List, Set converted to an array method

The toArray function comes in two forms, one with no arguments and one with arguments. Note that the size of the array should be specified in the form with arguments.

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: You cannot do this directly with the int[] array, because the arguments to the asList() method must be objects. You should first convert int[] to Integer[]. The same is true for other primitive arrays, which must first be converted to the corresponding wrapper array.


 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);

conclusion

That's the end of this article's introduction to the differences between Java collections and arrays, as well as conversion instances, and I hope you found it helpful. Those who are interested can continue to see this site:

Java Method Instance of Console Input Array and Output in Reverse Order

Sample Java Array Expansion Code

java Array Basics

If there is any deficiency, please let me know. Thank you for your support!


Related articles: