Javase Review Summary of the collection (Collection)

Source: Internet
Author: User
Tags iterable

Earlier versions of Java provided only a small set of classes for the collection class library, but as the Java version was upgraded, the Java Collection Class library became more and more fulfilling. Collection class library is stored in a variety of data containers, we have basically studied the data structure of this course, the data structure is about the ADT (abstract data Model) part of the data structure is not for a particular language, it is usually used in all languages of the ADT part, ADT and concrete implementation is not related. However, according to the textbook, according to the different majors, each major will choose to use different programming languages to achieve the teaching materials. And what we're talking about is how to use the Java data container, the Java collection (ADT implemented with Java), and I'll elaborate on the algorithms and data structures.

Before we know how to use them, we need to have a general understanding of their principles.

The Java Collection Class library separates interfaces from implementations. First, look at how the queue queues are separated.

Interface Queue<e>{    void  Add (E element);    E remove ();     int size ();}

Queue<e> is a generic aspect, and I'll give it in the next javase summary. At this stage it is only important to understand that,queue<string> represents the only type of object in the queue that can be stored in a String type, and generics are scoped. Separating the interface from implementation is one of the effective decoupling methods of modern programming.

In the Java class Library, the basic interface of the collection class is collection, and the collection interface inherits the iterator interface.

 Public InterfaceIterator<e>{E next (); BooleanHasnext (); voidremove (); Public InterfaceIterable<e>{Iterator<E>iterator ();} Public InterfaceCollection<e>extendsIterable<e>{    BooleanAdd (E Element); Iterator<E>iterator (); //..}

All classes that implement the Iterable interface represent that the class is "iterative" and can call the class's iterator () method, return an iterator to the class, and traverse the class by invoking the next () method of the iterator.

It is worth noting that when traversing to the last element, if you continue to call the next () method, an exception will be thrown: nosuchelementexception, so we usually call this:

collection<string> C = .... Iterator<String> it = c.iterator ();  while (it.hasnext) {    = it.next ();      Do something with S;}

The other thing is that starting from JavaSE1.6 all classes that implement the Iterable interface can use a type-enhanced for loop:

 for (String s:c) {    do  something with s;}

See here you might find that these interfaces and their implementations are available in the Java class library, but wouldn't it be a hassle if we needed to implement a queue by ourselves?

In fact, the Java class Library designed a set of abstract classes, abstractcollection and so on, the actual implementation of the need not implement all the routine methods, greatly reducing the burden.

The collections in the Java Collection Class library are mainly divided into the following categories:

1.List (table)

2.Set (SET)

3.Map (Figure ... I prefer to call a map for the inability to spit on the translation.

Set

The set interface inherits from the collection interface, which does not provide an additional method, but the elements in the collection class that implement the set interface are unordered and non-repeatable.

Features: unordered and non-repeatable. List

The list interface also inherits from the collection interface, but, contrary to the set interface, the elements in the collection class of the list interface are object-ordered and repeatable.

Features: Ordered and repeatable.

Two important implementation classes: ArrayList and LinkedList

1.ArrayList features are ordered and repeatable

2.LinkedList is a doubly linked list structure. Map

Map is also an interface, but does not inherit the collection interface. This interface describes a key-to-value mapping that is never duplicated. The map interface is used to maintain key/value pairs (Key/value pairs).

Feature: It describes a mapping of key-to-value values that are never duplicated.

Two important implementation classes: HashMap and TreeMap

1.HashMap, the Chinese call hash list, based on the hash table implementation, the characteristic is the key value pair's mapping relation. A key corresponds to a value. The ordering of elements in HashMap is not fixed. More suitable for inserting, deleting, and positioning elements.

2.TreeMap, based on the red and Black Book implementation. The elements in the TreeMap maintain some sort of fixed order. More suitable for sequential traversal of elements.

Collections Algorithm Classes:

Collections is an algorithm class that provides a series of static methods to find, sort, replace, thread-secure, and so on, for the collection, and can refer to the following:

Http://www.360doc.com/content/14/0829/10/15242507_405537400.shtml

Javase Review Summary of the collection (Collection)

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.