Java Collection Class (top)

Source: Internet
Author: User
Tags array length

What are the data storage methods in Java?
    • The basic data type (1byte3 whole 2 small 1 characters 1 bool) is Byte,short,int Long,flort,double,char,boolean
    • String
    • Custom encapsulation Classes (entity classes such as PO,VO)
    • Array, which stores a set of data, either a basic data type or a reference data type, but only the same type, and the size must
    • A collection class that acts like an array, or stores a set of data, also known as a container class

  

Why is the collection class present? What is the difference between a collection class and an array?

  Features of the array:

    1. An array can store only data of the same data type
    2. The array length is immutable, and the length must be defined when the array is defined
    3. The array is stored in only one way and needs to be positioned through the cursor, which starts at 0 and does not have access to the cursor outside the range.

Features of the collection class:

    1. A collection class can store data of multiple data types (once the object is put into the collection class, the collection class treats it as an object type, which is why the collection class can store data of multiple data types)
    2. Variable length of collection class
    3. Each collection class is stored differently, and the appropriate collection class can be selected according to the actual situation
    4. The collection class provides some methods for sorting, comparing, etc., so that we could use
     What are the collection classes in the Java system?

What information can we get? The figure shows that the red is a common collection implementation class, mainly divided into 2 interfaces collection Map, the classification is based on 2 of different storage methods

    • Collection is an object collection of orphaned elements, one element at a time, belonging to a single column collection
    • A map is a set of objects ("key-value" pairs) that are pairs of elements, one at a time, and two in a double-column collection

What is a generic type?

When we compile the collection class, the compiler appears with a warning (yellow exclamation mark), and the collection class can store different types of data because once the object is put into the collection, the collection class treats it as an object type. Doing so brings two questions:

    • Each time you remove a collection element, you are forced to type the conversion, not only the code is bloated, but also increase the complexity of programming
    • The collection has no restrictions on the element type, and if you want to create only a collection of dog objects, the program can easily put a cat object into the collection, and when the collection element is removed, if a cat type object is cast to the dog type, It will cause classcastexception anomalies.
How does generics solve the problem?

  Once a generic is defined, only the data type of the corresponding generic type is stored in the container.

Causes the collection class to remember the data type of the collection element, ensuring that the program does not have a warning at compile time and that there is no classcastexception exception at run time.

Java Collection Class (top)

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.