Summary of whether the list Set Map in Java is ordered or not

Source: Internet
Author: User
Tags comparable repetition

1.Collection List Set Map differential memory

These represent the collection in Java, where the main element is whether the elements are ordered, whether they can be repeated to distinguish between memory, in order to properly use, of course, there are synchronization differences, see the previous article related.

Ordered No

Allow elements to repeat no

Collection

Whether

Is

List

Is

Is

Set

Abstractset

Whether

Whether

HashSet

TreeSet

Yes (sort by binary tree)

Map

Abstractmap

Whether

Using Key-value to map and store data, key must be unique and value can be duplicated

HashMap

TreeMap

Yes (sort by binary tree)

The list interface is a simple extension of collection, and its concrete implementation classes are often ArrayList and LinkedList. You can put anything in a list container and take it out when you need it. ArrayList can be seen in its name as an array-like form of storage, so its random access speed is very fast, and LinkedList's internal implementation is a linked list, it is suitable for in the middle of the list of links need to frequently insert and delete operations. The application can be freely selected as needed. The iterator can only walk forward through the container, while Listiterator inherits the iterator idea and provides a way to iterate through the list.

The set interface is also an extension of collection, and unlike list, the object element in set cannot be duplicated, meaning you cannot put the same thing two times into the same set container. Its commonly used concrete implementation has hashset and TreeSet class. HashSet can quickly locate an element, but the object you put into HashSet needs to implement the Hashcode () method, which uses the algorithm of the previously mentioned hash code. And TreeSet will put the elements in order to store, which requires you to put the object is sortable, which is used in the collection framework provided by the other two practical classes comparable and comparator. A class is sortable, and it should implement the comparable interface. Sometimes more than one class has the same sorting algorithm, it is not necessary to define the same sorting algorithm at each separate time, as long as the comparator interface is implemented. There are two useful common classes in the collection framework: collections and Arrays. Collections provides some very useful methods for a collection container such as sorting, copying, finding, and populating, and arrays is doing a similar operation on an array.


A map is a container that associates a key object with a value object, and a value object can be a map, and so on, so that a multilevel map can be formed. For a key object, like set, a key object in a map container does not allow repetition, which is to keep the consistency of the search results, and if there are two key objects, then you have a problem with the value object that the key object has, and you may not get the value object you want, and the result will be confusing. So the uniqueness of the key is very important, and it conforms to the nature of the set. Of course, in the process of use, the value object corresponding to a key may change, then the last modified value object corresponds to the key. There is no unique requirement for value objects. You can map any number of keys to a value object without any problems (though it may be inconvenient for you to use it, you don't know what you get is the value object for that key). There are two more common implementations of map: HashMap and TreeMap. HashMap also uses the hash code algorithm, in order to quickly find a key, TreeMap is the key in order to store, so it has some extension methods, such as Firstkey (), Lastkey () and so on, you can also specify a range from the TreeMap to get its child map. The association of Keys and values is simple, with the pub (Object Key,object value) method to associate a key with a value object. Use Get (object key) to get the value object corresponding to this key object.

2.List, vector, set, map of the difference and contact

When usingJava, we all encounter the use of collections (Collection), butThe Java API provides a variety of implementations of collections, and I frequently encounter such "choices" when using and interviewing. :) (mainly during the interview)
Over time, there will be a little bit of experience, write out for everyone to discuss.
OverallThe collection classes used in the Java API are implemented with the collection interface, and one of his class inheritance structures is as follows:

Collection<--list<--vector
Collection<--list<--arraylist
Collection<--list<--linkedlist
Collection<--set<--hashset
Collection<--set<--hashset<--linkedhashset
Collection<--set<--sortedset<--treesetvector: array-based list, in fact, encapsulates the array does not have a number of features convenient for us to use, it can not go into the limit of the array. Performance is also impossible beyond array. So, if possible, we need to use array more. Another important point is the vector "sychronized", which is the only difference between the vector and the ArrayList.

ArrayList: The same as a vector is a list based on the array, but the difference is that ArrayList is not synchronous. So it's better than vector in performance, but when running into a multithreaded environment, you need to manage the synchronization of threads.

Linkedlist:linkedlist differs from the previous two list, which is not array-based, so it is not limited by the array performance. Each node has two aspects: 1. Data of the node itself, 2. Information for the next node (nextnode). So when adding to the LinkedList, deleting the action doesn't have to be a lot of data movement like array-based lists. As long as you change the information about the NextNode can be achieved. This is the advantage of LinkedList.

List Summary:

1. All lists can contain only a single table of different types of objects, not key-value key-value pairs. For example: [Tom,1,c];

2. All lists can have the same elements, such as vector can have [tom,koo,too,koo];

3. All lists can have null elements, for example [tom,null,1];

4. Array-based list (vector,arraylist) is suitable for querying, while LinkedList (linked list) is suitable for adding, deleting operations.

HashSet: Although both set and list implement the collection interface, they are implemented in a very different way. The list is basically based on an array. But set is implemented on the basis of HashMap, which is the fundamental difference between set and list. The hashset is stored in the HashMap key as the corresponding storage of the set. Look at the implementation of the HashSet's add (Object obj) method to see it at a glance.

Public boolean Add (Object obj)
{
return Map.put (obj, PRESENT) = = NULL;
}
This is also why it is not possible to have duplicate entries in the set as in the list, because the HashMap key cannot be duplicated.

Linkedhashset:hashset a sub-class, a linked list.

Treeset:sortedset, which is different from HashSet, is that TreeSet is orderly. It is achieved through SORTEDMAP.

Set Summary:

1. Set implementation is based on map (HASHMAP);

2. The elements in set cannot be duplicated, and if an existing object is added using the Add (Object obj) method, the previous object is overwritten;3.Java Basic Concepts: Collection Class List/set/map ... The difference and contact

Collection:list, Set
Map:hashmap, HashTable

How to choose between them


One, Array, Arrays

Array is the most efficient of all Java "storage and random access to a series of objects".

1.
High efficiency, but the capacity is fixed and cannot be changed dynamically.
One drawback of array is that it is not possible to determine how many elements are actually stored, and length simply tells us the capacity of the array.

2,Java has a arrays class, specifically used to manipulate the array.
The arrays has a set of static functions,
Equals (): Compares two arrays for equality. Array has the same number of elements, and all corresponding element 22 is equal.
Fill (): Fills the value into the array.
Sort (): Used to sort the array.
BinarySearch (): Looks for elements in a sorted array.
System.arraycopy (): Copy of Array.


Second, Collection, Map

If you do not know exactly how many objects you need to write your program, and you need to automatically expand capacity when you are running out of space, you need to use the Container class library, which is not applicable.

1. The difference between Collection and Map

The number of elements stored in the container is different.
collection type, with only one element per position.
Map type, holding Key-value pair, like a small database.

2, the sub-category of their respective relations

Collection--list: Elements are stored in a specific order. So the order taken out may be different from the order in which it is put.
--arraylist/linkedlist/vector--set: cannot contain duplicate elements
--hashset/treeset
Map
--hashmap
--hashtable
--treemap

3. Other Features

* List,set,map holds objects as Object type.
* Collection, List, Set, map are all interfaces and cannot be instantiated.
ArrayList, vectors, HashTable, and HashMap are inherited from them, and these can be instantiated.
* The vector container knows exactly what type of object it holds. Vectors do not perform boundary checks.


Third, collections

Collections is a helper class for the collection class. Provides a series of static methods for searching, sorting, threading, and so on for various collections.
A class--arrays equivalent to a similar operation on an array.
For example, Collections.max (Collection coll); Take the largest element in the Coll.
Collections.sort (list list); Sort elements in List

Iv. How to choose?

1, the difference between the container class and the array, playable
* The container class can only hold object references (pointers to objects), rather than copy the object information to a position in a sequence.
* Once the object is placed in the container, the object's type information is lost.

2.
* In various lists, the best practice is to use ArrayList as the default choice. When inserting and deleting frequently, use LinkedList ();
Vector is always slower than ArrayList, so try to avoid it.
* In various sets, hashset is usually better than hashtree (INSERT, find). Use TreeSet only if you need to produce a sorted sequence.
The only reason for the existence of Hashtree: the ability to maintain the ordering state of its elements.
* In a variety of maps
The HashMap is used for quick lookups.
* When the number of elements is fixed, use array, because array efficiency is the highest.

Conclusion: The most commonly used is arraylist,hashset,hashmap,array.


Attention:

1. Collection does not have a get () method to get an element. Elements can only be traversed by iterator ().
2. Set and collection have identical interfaces.
3, List, you can use the Get () method to remove one element at a time. Use numbers to select one of a bunch of objects, get (0) .... (Add/get)
4, generally use ArrayList. Use LinkedList to construct stack stacks, queue queues.

5, map with put (K,V)/get (k), you can also use ContainsKey ()/containsvalue () to check if it contains a key/value.
HashMap will use the object's hashcode to quickly find the key.
* The hashing hash code is the transformation of the object's information into a unique int value, stored in an array.
We all know that the array lookup speed is the fastest in all storage structures. So, you can speed up lookups.

When a collision occurs, let the array point to multiple values. That is, the array generates a table of cassia at each location.

6, the element in the map, you can extract the key sequence, the value sequence alone. Use Keyset () to extract the key sequence and generate a set for all keys in the map.
Use values () to extract the value sequence and generate a collection for all values in the map.

Why a build set, a Build collection? That's because key is always unique, and value allows repetition.

Summary of whether the list Set Map in Java is ordered or not

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.