Containers in java

Source: Internet
Author: User

We usually use arrays to store things. array access is really convenient, but the size of the array is a limit. Every time you store things, you must determine whether it is out of the range. It is too inflexible, And the types in the array must be of the same type. If int Is All int, ah, to put a string type, you must re-create the new array. Of course, it is really not good to put different types of things in one piece, but whatever the case, it is inconvenient and inflexible .. To be flexible, you need to find a java container: A series of class instances provided by java API, used to store objects in programs. You don't have to worry about the capacity of the containers provided by java. You don't have to worry about adding a type. You can store a dog and a person, people stay with dogs, get along well with each other, enjoy themselves, and enjoy yourself well. It's quite suitable for my taste of life .. The container structure diagram provided in java is as follows. You only need to master this diagram. As for the method or something, we can check the api documentation at that time. This figure shows that the data objects in the Set are not ordered and cannot be repeated. Therefore, it is normal that the order of the items printed by each person is different. The data objects in the List are ordered and can be repeated. The Map interface defines the method for storing Key-value pairs. The Key is equivalent to the directory of a book, and the value is equivalent to the real content. Therefore, Key keys cannot be repeated. If there are duplicates, the subsequent content replaces the previous content. The difference between a linked list and an ArrayList is that the underlying data structure of the linked list is in the form of a linked list, and the memory space of the linked list is in the form of a hash. The underlying data structure of ArrayList is an array, and the memory space of the array is continuous. The read Speed of ArrayList is fast. If you want to read a certain memory space, such as the last one, you can directly use the first plus offset. The reading speed of the shortlist is slow. It is found by one node next and one node next. However. The modification speed of ArrayList is slow. If a value in the array is deleted, the subsequent values move forward one by one. If a value in the array is added, the subsequent values move backward one by one. The modification speed of the linked list is fast. If you delete or add a node in the linked list, you can directly change the pointer. Although any type of objects can be loaded in the above containers, the efficiency is not high. Why? Because the objects stored in the container are completely treated as objects, even if the specific Dog is stored, the specific Person is stored. Then, when reading data, you need to transform the Object into the corresponding Person or Dog. So, let's put the same object at the end, so there is no need for transformation, there is no need to reduce efficiency, is it using arrays? The array stores the same object. Of course not. The array capacity can be limited. What is the need for a container like java that can put the same object like an array? Haha, that is, a wildcard that combines the advantages of both .. Generic, of course, there is no limit on the capacity, and the operation is convenient. Simply add the object and remove it directly. Stores objects of the same type. For example, this container is completely a Dog class, and you can fetch it directly at that time, instead of judging whether it is a Dog class, you do not need to convert it into a Dog class, because the premise has been declared that this is installed with Dog, List <Dog> c = new ArrayList <Dog> (); do not need to install Person, otherwise the consequences will be borne by yourself .. Then I packed all the good-looking dogs into it. Now I want to see how I installed them? This involves an interface Iterator and a method iterator. This iterator method is implemented by all classes that implement the Collection interface. The Iterator object is called an Iterator for convenient traversal. In Iterator: hasNext: determines whether there is another one. Next: indicates the next object. Oh, so convenient !!!!! For (Iterator <Dog> it = c2.iterator (); it. hasNext ();) {Dog dog = it. next (); System. out. println (dog);} I have read all the dogs, but I want to see which one is the most handsome and which one has the largest age? What should I do? Haha, this involves an interface Comparable. To sort data, you only need to implement the Comparable interface and rewrite the compareTo method. Well, the java container only needs to master the above figure, as well as the Iterator interface and Comparable interface. If you do not understand the method, you can find help in time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.