The popular cognition of Java collection classes

To be frank, Java provides a complete set of collection classes (also known as container classes) to manage a group of objects with variable length (i.e. collection elements). The common types include list, set, queue and map. From my personal programming experience, the implementation class ArrayList of list and the implementation class HashMap of map are used the most frequently, and other implementation classes can only catch up with it.

List, set and queue are all sub interfaces of collection, but each has its own advantages. List saves elements in the order of insertion. Set does not have duplicate elements. Queue usually (but not necessarily) sorts each element in FIFO (first in first out).

The biggest difference between map and collection is that it is a group of "key value pairs", which can quickly find values through keys; Collection has no key, so you need to find the value according to some rules (the value here is the element).

How to use collection classes? Examples are as follows:

 list = ();        list.add( map = ();        map.put(

Generally, ArrayList is our first choice when selecting list. Don't ask me why, just like you ask me why 1 + 1 = 2? I can't answer. Some people will ask, if my application operation prefers to insert and delete rather than random access, do I still choose ArrayList? I know that LinkedList is more efficient in dealing with inserts and deletions, while ArrayList is more suitable for random access. Then my answer is that you already know the answer, so make your own choice. The application of list often involves some classical sorting algorithms. We might as well revisit bubble sorting. The rules of bubble sorting are: if there are n numbers, they are out of order; Start from the first number and compare it with the number n-1 after it. If it is smaller than yourself, exchange positions; Starting from the second number and comparing with the following n-2 numbers, it is also found that if there is a number smaller than itself, it will exchange positions; Until the end of n-1. Look at the program listing 1-1: List = ();         Collections. addAll(list,  temp) {                    list.set(j, list.get(i));                    list.set(i, temp);                 }            }        }        System. out. println(list); The}} collections class is a tool set for operating collection. It provides many static methods that can operate and return collection, which is very easy to use. Collections. The addall () method can add all the specified elements to the specified collection, and can specify the elements to be added separately, just like collections addAll(list,2,1,5); So; Or specify the element to be added as an array; This method behaves like list The behavior of addall (arrays. Aslist (elements)) is the same, but in most implementations, this method may run much faster. list. Set (int index, e element) is used to specify the element to replace the element at the specified position in this list. It is very convenient.

HashMap is the most common map implementation and is widely used; Support null key and null value, which is the first choice for most access scenarios using key value; For example, when querying a database, HashMap is often used to flexibly bind column names and column values (some columns of a table can be selected instead of all columns). New key value pairs can also be stored in the values of HashMap, as shown in the following diagram.

Considering such a scenario, Wang Er is now a big writer, not just a programmer who can write code. One day, he met several enthusiastic readers at the door of the toilet. In order to show their respect for Wang Er, the readers felt it necessary to let Wang Er solve the problem first. PriorityQueue is a priority queue (priority means priority), which can be applied to this scenario. The code example is as follows: {queue = (); meta "> @ override meta" >@Since override # PriorityQueue needs to arrange priorities, it must have certain rules, and the arranged objects must implement the comparable interface. It is suggested to debug while learning. You will find the change of queue very interesting; The corresponding CompareTo will be executed every time add or poll is taken out. (I'm very ashamed that I don't understand the internal sorting, so I'll continue to learn later.) the common function functions of PriorityQueue are as follows: method name function description delete) delete) recommended reading:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>