Hold the container in the object--java (i)

Source: Internet
Author: User
Tags iterable

  1. Generic and type-safe containers
    1. Before using Java SE5, the compiler allowed the insertion of an incorrect type into the container, and after Java SE5 introduced generics, applying a predefined generic could prevent objects of the wrong type from being placed in the container at compile time.
  2. Basic concepts
    • Collection. A sequence of independent elements that obey one or more rules. The list must save the elements in the order in which they are inserted, the set cannot hold duplicate elements, and the queue determines the order in which the objects are produced (usually in the same order as they were inserted).
    • Map. A pair of "key-value pairs" objects that allow keys to be used to look up values. Where keys cannot be duplicated, otherwise the value of the key will be overwritten.
      • hashmap--provides the fastest search technology and does not preserve elements in any apparent order;
      • treemap--Save the key in ascending order of the comparison result;
      • linkedhashmap--saves the key in the order in which it was inserted, while preserving the HashMap query speed.
  3. List. A list can maintain elements in a particular sequence. The list consists of two subclasses: ArrayList and LinkedList.
    • arraylist--random access is faster, but the insertion and removal of elements in the middle is relatively slow;
    • linkedlist--random access is slower, but inserting and removing elements in the middle of the list is faster, providing an optimized sequential access mechanism. LinkedList also adds a method that can be used as a stack, queue, and double-ended queue.
  4. Set. Set does not save duplicate elements. The longest used in set is test attribution, so it is found to be the most important operation in set. The set has exactly the same interface as the collection.
    • hashset--uses a hash, so the HashSet has a faster search speed;
    • treeset--stores elements in a red-black tree data structure, maintaining a certain order;
    • linkedhashset--also uses hashes, and lists are used to maintain the order in which elements are inserted.
  5. Iterators--iterator (one-way only) iterators are able to separate the operations of the traversal sequence from the underlying structure of the sequence, and the iterator unifies the way the container is accessed.
    • Using method iterator () requires the container to return a iterator;
    • Use Next () to get the next element in the sequence;
    • Use Hasnext () to check if there are elements in the sequence;
    • Use Remove () to delete the element that is newly returned by the iterator.
  6. listiterator--can only be used for access to a variety of list classes
    • Listiterator can move in two directions;
    • It can produce an index relative to the previous and subsequent elements of the current position that the iterator points to in the list, and the last element that it accesses can be replaced with the set () method;
    • by Listiterator () you can generate a listiterator that points to the beginning of the list;
    • by Listiterator (n), you can generate a pointer to the listiterator at the element with the list index n.
  7. stack--"Stack" is usually referred to as a "last in First Out" (LIFO) container
    • Because LinkedList has a way to directly implement all the functions of the stack, the LinkedList can be used directly as a stack.
    • However, if you just want to use the functionality of the stack, it is best to combine the LinkedList implementation instead of inheriting or replacing it directly with LinkedList to avoid introducing LinkedList other methods (Java1.0 design makes this mistake)

 Public classStack<t> {  Privatelinkedlist<t> storage =NewLinkedlist<t>();  Public voidpush (T v) {Storage.addfirst (v);}  PublicT Peek () {returnStorage.getfirst ();}  PublicT Pop () {returnStorage.removefirst ();}  Public BooleanEmpty () {returnstorage.isempty ();}  PublicString toString () {returnstorage.tostring ();}}

    1. The queue--queue is a typical FIFO (queue is particularly important in concurrent programming).
      • LinkedList provides a method to support queue behavior, and implements the queue interface, so it is an implementation of the queue;
      • Offer (): Insert element at the end of the team;
      • Both peek () and element () will return to the team head without removing;
      • The poll () and remove () methods will be removed and returned to the team header;
      • The difference is that when the queue is empty, peek () and poll () return null, and element () and removed () throw a nosuchelementexception exception.
    2. priorityqueue--sortable queues
      • Priorityqueue The default sort will use the natural order of the objects in the queue, but you can modify the order by providing your own comparator.
    3. Collection and iterator (slightly)
    4. foreach and iterators
      • The foreach syntax is used primarily for arrays, but can also be applied to any collection object. This is because Java SE5 introduces the Iterable interface, which contains a iterator () method to produce iterator objects, and the Iterable interface is used by foreach to move through the sequence. So you create any class that implements Iterable, and you can use a foreach statement.

Hold the container in the object--java (i)

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.