Java Learning Note 9.23 (collection)

Source: Internet
Author: User
Tags iterable

Collection Interface:

The Java class Library separates the interfaces and implementations of the Collection. Queue Implementations: loop arrays and linked lists; the former is more efficient and the latter has no upper limit.

The Abstractqueue class is used by users to implement their own queue Classes.

Cllection (extends iterable<e>) interface: Basic Method 1.add 2 iterator (next;hasnext;remove)

collection<e> coll=new collection<> ();

Iterator<string> Iter=coll.iterator ();

While (iter.hasnext ()) {

String A = Iter.next ();

Do something with a ...

}

Enhanced for loop (requires implementation of the Iterable interface):

For (string A in C) {does something with a ...}

Iterable interface: The Iterator method returns an object that implements the iterator interface;

Iterator Interface: Hasnext,next,remove method

1 The iterator is considered to be between two Elements. 2 must first next, then remove

Ohter methods:contains ...

The Abstractcollection class implements the collection interface: making it easier to implement collection interfaces, except that iterator and size are abstracted, contains, and other methods provide a routine Approach.

In addition to the map end (implementation of the map interface), other collections implement the Clloctive interface

A specific collection:

Linked List:

Linkedlist<e>extends abstractsequentiallist<e>implements list<e>, Deque<E>, Cloneable, Serializable

Listitertor iterators:

An ordered collection that uses iterators to add Elements. Interface Listitertor<e> extends iterator<e>{void add (E element); E previous (); Boolean hasprevious ();(reverse traversal of the linked list)}

To insert an element in the middle of a linked List: 1. Call the list Listiterator () to return a Listiterator iterator Object 2. Call the Add method in the iterator

Conflicts between two iterators

Using the Get method to get an element, resulting in inefficiency, may use the wrong data structure. The search is restarted from the beginning before each get.

List.listlistiterator (n)

Reasons to use linked lists: reduce the cost of inserting or deleting elements in a list

Avoid implementing an integer index in a linked list: get (n), set (n), listierator (n), preferably with ArrayList if this random access is to be Performed.

Note where the iterator is located, if you add an element after you call next, the iterator is behind the add Element.

ArrayList: a collection class that can randomly access each element.

Hash set:

Hash conflict, The number of barrels is usually 75% to 150% of the elements, and is a prime, in case of Agglomeration. When the filling factor is 0.75, double the number of buckets to Re-hash

Hashset:add,contains has been rewritten

Iterators are accessed randomly, and are not cared for in the order of the elements when Used.

Tree set (ordered collection): adding elements is slower than hash lists, faster than arrays or linked Lists. Find Complexity: log2n

Comparison of Objects:

Constructor:

TreeSet (comparator<? Super e> Comparator) Compartor interface implements an int copare (t a, T B) method, which implements the object of this interface into the TreeSet constructor, Tells TreeSet how to compare objects (different sets of comparisons are implemented, and the Compareable interface is not implemented in the Object's Class)

function objects, which are typically dynamically defined.

Mapping table:

Java Learning Note 9.23 (collection)

Related Article

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.