Java Collection Basics

Source: Internet
Author: User

The origin of the Java collection framework

prior to java1.2,java did not have a complete collection framework. It has only a few simple extensible container classes, such as Victor,Stack,Hashtable , etc.

Why container classes are required: Containers (collection classes) can store multiple data.

The drawbacks of arrays:

1. The length is immutable, once the array is initialized, the length is fixed

2. to store multiple data in N places, you have to write the operation method of the array specifically.

What is a collection frame

Although these container classes are very useful, they cannot be centrally and uniformly managed. A collection framework is a uniform standard architecture that is defined for representing and manipulating collections. Any set frame contains three chunks of content: external interfaces, interface implementations, and algorithms for set operations.

Why collections are required (the classes and interfaces of the collection framework are placed in the Java.util package)

1. Reuse of the provided functionality

2. Let programmers focus on business development, not data structures and algorithms

Common collections:

The objects stored in the collection, called the Elements of the collection.

Set: Objects in the collection are not sorted in a particular way, and elements are not allowed to repeat

List: Objects in the collection are sorted by index location, allowing duplicates

Map: Each element in the collection contains a bunch of key and value objects, does not allow the key object to repeat, the value can be repeated

Linked list structure

1. one-way linked list: can only be traversed from the beginning to the tail or from the tail to traverse the head

2. Bidirectional linked list: Can be traversed from beginning to end or from tail to head

To represent the relationship of the previous node and the next node through a reference relationship

Two iterative operations for collections

1.for Cycle

2.for-each

3.Iterrator Steps

A. call iterator () method B. called Hasnext () Method c. called Next () Method

Three Set Interface

the 1.Set interface stores a unique set of unordered objects

2. The order in which elements are added is not recorded

3.HashSet is a Common implementation class for SET interfaces

HashSet How to tell if two objects are the same

1. the equals of two objects is equal and returns true, indicating that the same object

2. the Hashcode method of two objects returns a value equal

comparison process: when you go to When you add a new object to the HashSet collection, the hashcode values in the object and the collection object are judged first

1) unequal: Store the new object directly in the location specified by the hashcode

2) Equality: Then continue to judge the new object and the Set object in the comparison of equals

A) hashcode is the same ,equals true: is considered to be the same object, it is not guaranteed to exist in the hash table

b) Hashcode same,equals different false

How does the Set interface determine when an object is joined?

The Equals () method of the object compares two objects with equal

Four Map Interface

Mathematical explanations of the mappings:

set a/b Two non-empty sets, if there is a rule f a f b b a to b

mapping relationship (two collections) : A Collection and b collection >>>>a Each element in the collection can be in the b find the only word corresponding to it in the collection.

strictly speaking, Map is not a good set, it should be a mapping between two sets, but map can store data (each store should store An element in the A collection (Key ), An element in the B collection (value), we are accustomed to the Map also called the set.

Question: Establishing a key-value mapping between the national abbreviation and the Chinese population

Traverse Map Collection: Iterator enhancement for Loop

Common methods : put (k,v) get (k) remove (k) size () KeySet ()--- return Set

Five Generic type

Why generics are used:

1. store any type of data in the collection, but when it is taken out is the Object type, you have to force the type conversion

2. The elements that can be constrained to be stored in the collection must be of the same data type (the same data type can be compared)

3. Design a point class to represent the coordinate location, requiring the coordinate location to support String ,Integer, Double type

What is generics: specifying the type of an object as an argument to another class or method, guaranteeing the security and stability of type conversions

1. Wide range of common types

2. The type in the code template is indeterminate, who calls the code, who indicates what type is

generic classes: generics that are defined directly on a class/interface

Using generics:

Ensure the same type before and after

List<string> list=new arraylist<string> ()// The collection can only store String Types

since the type is the same, the generic diamond syntax has been introduced from Java7 <>

List<string> list=new arraylist<> ();

There is no inheritance relationship for generics: List<object> list=new arraylist<string> () >>>>> error Syntax

Later, you use the collection to constrain the element types in the collection with generics.

Generic method: Declaring a generic on a method

Scenario 1: Generics in a generic class apply only to non-static methods, and if you need to set a generic for a static method, use a generic method

Scenario 2: Methods in a generic class

In general, it makes sense to have a custom generic as the return type of the method, and the generics at this time must be set in by the parameter. If there are no parameters to set the specific type of the generic, the method at this time generally returns the design Object can be.

Java Collection Basics

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.