A collection of Java foundations and generics

Source: Internet
Author: User

Words don't say much, direct

1.Collection Collection Summary

Note:

all containers that use a hash table to hold objects are unique in the container, and the uniqueness is through the hashcode of the saved object and equals method to determine together;

any container that uses a two-fork tree to hold objects is unique to the container, and is ordered when preserved, ordered and uniquely dependent on the saved object implementation comparable interface, or the comparer object passed to the container (implements the comparator interface of the object);

2. Map Collection Summary

Features of the Map collection:

1, the mapping relationship of Key-value is stored in the map collection;

2, each map in the map collection contains a key and a value of two objects, and the mapping itself is an object, using the Map.entry class description;

3, the key in the Map collection must be unique, cannot be duplicated, but the value can be repeated;

4, the key and value in the map collection can be null elements, but key can only have one null; (Cannot use NULL for keys and values in Hashtable)

5, the Map collection cannot traverse directly, only through the keyset, the value set or the key value pair set traversal;

The inheritance system for map collections:

the three most used collections in development: ArrayList ; HashSet ; HashMap ;

if the saved data has a mapping relationship, you should use the map set, or else it is with collection set;

If the data you want to save can be duplicated, you should use the list Otherwise, use set set;

3, generic type

1. Generics is a technique that can be used to check data types at compile time, and can only check the data types of reference types.

2. Generics use a pair of angle brackets to denote identifiers in the:< identifier >; angle brackets, representing a reference data type;

3. Generics can be defined on classes, methods and interfaces;

A) defined on a class: write after the class name, determine the specific type of the generic when creating an instance object of the class, which can be used in a non-static function in a class;

b) defined on the method: write in front of the return value type of the method, determine the specific type of the generic at the method invocation, both static and non-static functions can be defined, and can only be used in the currently defined method;

c) defined on the interface: written after the interface name, the implementation of the interface can be defined by the specific type of the generic, or through a generic class, when the object of the class is created to clarify the specific type;

4. Generic wildcard: Generic wildcard characters are required when the data type of the generic type to be used is indeterminate;

How to:<?> a wildcard character

5. The upper and lower bounds of a generic wildcard:

A) when defining a generic wildcard, if you can only match a class and subclasses of that class, you need to use the upper bound of the wildcard character: <? Types of extends Caps >

When using the upper limit of the wildcard, only the data can be fetched from the container, and the data cannot be stored in the container;

b) When defining a generic wildcard, if you can only match a class and the parent class of this class, you need to use the lower bound of the wildcard character: <? Type of super Lower limit >

When using the lower bound of the wildcard character, the object is saved directly to the container, and the type of the object can only be the lower limit type;

To remove an object from a container, you cannot accept it directly with a variable of the lower bound type, you need to force a downward transformation, or use a variable of type object to accept it;

A collection of Java foundations and generics

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.