JAVA (collections and data structures)

Source: Internet
Author: User

the difference between collection and collections :

1, Java.util.Collection is a collection interface . It provides a common interface method for basic manipulation of collection objects. The collection interface has many specific implementations in the Java class Library. The meaning of the collection interface is to provide a maximum unified operation for a variety of specific collections. Declares a common method that applies to a Java collection that includes only set and list. both Set and list inherit the Conllection,map.

2, Java.util.Collections is a packaging class. It contains a variety of static polymorphic methods related to set operations. This class cannot be instantiated , just like a tool class that serves the Java collection framework.

There are three main types of JAVA collections:Set ( set ), list (lists ) , Map ( map )

Set ( set ) :

HashSet class :

The Java.util.HashSet class implements the Java.util.Set interface.

It does not allow duplicate elements to appear;

The order of elements in the political and political collections is not guaranteed

allows an element that contains a value of NULL, but can have at most one null element.

TreeSet class :

TreeSet describes a variant of Set-a collection of functions such as sorting, which is automatically inserted into an ordered sequence of objects by a comparison rule when the object element is added to the collection, and guarantees that the collection element is arranged in ascending order .

function method of Set

The set has exactly the same interface as the collection, so there is no additional functionality, unlike the previous two different lists. In fact set is collection, but behaves differently. (This is a typical application of inheritance and polymorphic thinking: behavior that behaves differently.) Set does not save duplicate elements (more responsible for how the elements are judged)

Set: Each element that is stored in the set must be unique because set does not save duplicate elements. The element that joins the set must define the Equals () method to ensure the uniqueness of the object. The set has exactly the same interface as the collection. The set interface does not guarantee the order in which elements are maintained.

HashSet: Set for quick find design. The object that is deposited into the hashset must define HASHCODE ().

TreeSet: The set of the save order, the underlying tree structure. Use it to extract ordered sequences from the set.

Linkedhashset: has hashset query speed, and internally uses the chain list to maintain the order of elements (the Order of insertions). The result is displayed in the order in which the elements are inserted when iterating through the set using Iterators.

list ( lists ) :

A list is characterized by its elements being stored in a linear fashion, in which duplicate objects can be stored in the collection.

  The main implementation classes of the list interface are:

  ArrayList (): Represents the length can be changed by the group. Elements can be randomly accessed and inserted into ArrayList () at a slower rate than the element being deleted.

  LinkedList (): A linked list data structure is used in the implementation. Fast insertion and deletion, and slow access times. For random access to a list, it is just random to retrieve the element at a particular location.

  The Get (int index) method of the List returns the object in the collection at the index location specified by the parameter index, starting with "0".

function method of List

There are actually two kinds of lists: One is the basic ArrayList, the advantage is the random access element, the other is the more powerful linkedlist, it is not for the fast random access design, but has a more general method.

List: Order is the most important feature of the list: it ensures that the elements are maintained in a specific order. The list adds a number of methods to collection, allowing you to insert and remove elements to the middle of the list (this is recommended for linkedlist use only. A list can generate Listiterator, which can be used to traverse a list in two directions or to insert and remove elements from the middle of a list.

ArrayList: A list implemented by an array. Allows fast random access to elements, but inserts and removes elements in the middle of the list is slow. Listiterator should only be used to traverse the ArrayList backward, not to insert and remove elements. Because that's much more expensive than linkedlist.

LinkedList: Sequential access is optimized, and the cost of inserting and deleting to the list is small. Random access is relatively slow. (use ArrayList instead.) ) also has the following methods: AddFirst (), AddLast (), GetFirst (), GetLast (), Removefirst (), and Removelast (), which are not defined in any interface or base class Enables LinkedList to be used as stacks, queues, and bidirectional queues.

Map ( map ):

  Map is a collection of key object and value object mappings, each of which contains a pair of key objects and value objects.

  Map does not inherit from the collection interface

from When retrieving an element in the map collection, the corresponding value object is returned whenever the key object is given.

  Common methods of Map:

  1 Add, delete operation:

  Object put (object key, Object value): Adding elements to the collection

  Object remove (Object key): Delete the element associated with key

  void Putall (Map t): Adds all elements from a specific image to the image

  void Clear (): Remove all mappings from the image

  2 Query operation:

  Object get (Object key): Gets the value associated with the keyword key

  The key object in the Map collection does not allow duplicates, which means that any two key objects that are compared by the Equals () method are false.

However, you can map any number of keys to a single value object.

  Conllections: Collection Utility class

  Conllections provides a static method that is useful for Java collections

function method of Map

Method put (Object key, object value) adds a "value" (What you Want) and the "Key" (key) associated with the value (using it to find). Method get (Object key) returns the value associated with the given "key". You can test whether a "key" or "value" is included in the map with ContainsKey () and Containsvalue (). The standard Java class library contains several different map:hashmap, TreeMap, Linkedhashmap, Weakhashmap, and Identityhashmap. They all have the same basic interface map, but the behavior, the efficiency, the sorting strategy, the life cycle of the saved object, and the policy that determines the "key" equivalence are different.

Execution efficiency is a big problem in map. Look at what get () does, and you'll see why searching for "keys" in ArrayList is pretty slow. And that's where the hashmap to improve speed. HashMap uses a special value, called a hash code, to replace the slow search for keys. A hash code is a "relatively unique" int value that represents an object that is generated by converting some information of that object. All Java objects can produce hash codes, because Hashcode () is the method defined in the base class object.

HashMap is a quick query using the object's Hashcode (). This method can significantly improve performance.

MAP: Maintain the relevance of key-value pairs so that you can find "values" by "Keys"

Hashmap:map based on the implementation of the hash table. The cost of inserting and querying "key-value pairs" is fixed. The capacity capacity and load factor load factor can be set through the constructor to adjust the performance of the container.

Linkedhashmap: Similar to HashMap, but when iterating through it, the order in which key-value pairs are obtained is their insertion order, or the least recently used (LRU) order. Just a little slower than HashMap. The iteration is accessed faster because it uses the list to maintain the internal order.

TREEMAP: Based on the implementation of red-black tree data structure. When you look at key or key-value pairs, they are sorted (the order is determined by comparabel or comparator). TreeMap is characterized by the fact that the results you get are sorted. TreeMap is the only map with the Submap () method, which can return a subtree.

Weakhashmao: The objects used in the weak key (weak key) Map,map are also allowed to be released: This is designed to solve special problems. If no references outside of map point to a key, the key can be reclaimed by the garbage collector.

Identifyhashmap: Use = = instead of equals () to compare the "key" to the hash map. Designed to solve special problems

The list saves objects in the order in which they are entered, without sorting or editing operations. Set accepts only once for each object and uses its own internal ordering method (typically, you are only concerned about whether an element belongs to set, not the order of it – otherwise you should use list). Map also saves one copy of each element, but this is based on the "key", and the map has a built-in sort, so it doesn't care about the order in which the elements are added. If the order of adding elements is important to you, you should use Linkedhashset or Linkedhashmap.  

Stack class :

(stack) is a last-in-first-out pattern that can only be inserted and deleted on the stack header.

1. Push pushes the item into the top of the stack. The effect is the same as addelement (item) . the item that the parameter item presses into the top of the stack . return: item parameter ;

2. Pop () removes the top of the stack object and returns the object as the function's value. Returns: The top object of the stack (the last item in the Vector object). Throw an exception : emptystackexception If the stack is empty ...

3. Peek () View the top of the stack object without removing it ... Returns: The top object of the stack (the last item in the Vector object). Throw an exception : emptystackexception If the stack is empty ...

4. Empty (the test stack is empty.) Returns false if and only if no items are included in the stack .

5. int search (object o) returns the position of the object in the stack , with a base of 1, and if the object O is an item in the stack, the method returns the nearest occurrence bit from the top of the stack The distance to the top of the stack, and the distance 1 of the topmost item in the stack. Make the Equals method compare o with the items in the stack ...

Queue class :

(queue) is a FIFO mode that can only be inserted at the end of a team and deleted in the team header.

1.add Add a single cable if the queue is full, a Iiiegaislabeepeplian exception is thrown
2.remove Remove and return elements of the queue header if the queue is empty, throw a nosuchelementexception exception
3.element returns the element of the queue header if the queue is empty, a Nosuchelementexception exception is thrown
4.offer adds an element and returns true if the queue is full, returns false
5.poll removing and returning elements to the queue header returns null if the queue is empty
6.peek returns the element of the queue header returns null if the queue is empty
7.put Add an element block if the queue is full
8.take Remove and return elements of the queue header if the queue is empty, it blocks

JAVA (collections and data structures)

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.