Java Container Classes

Source: Internet
Author: User
Tags addall comparable int size

Java Container class Java silently looking under the rain 1. Overview

The Java Container class's JDK provides classes for saving objects . It hides specific implementations (arrays, linked lists), including commonly used data structures: collections, queues, stacks, mappings

The Java container mainly consists of 3 parts:Collection Collection (List, set),map map , tool class (iterator iterator, enumeration enumeration class, Arrays, Collections)

2. Framework

Description: The diagram on the left is a simplified diagram, and the picture on the right is the full graph (does not include the implementation of queue)

There are mainly two different concepts:

1). The collection interface, a sequence of independent elements that contains some basic operations and properties. Divided into List, Set.

The list is a collection of insertions, each of which has its index (the first element has an index value of 0). List implementation classes mainly include: ArrayList, LinkedList, Stack, Vector.
A set is a collection that cannot have duplicate elements (a partial implementation class can have a null object). The set implementation classes mainly include: HashSet, TreeSet, Linkedhashset. Where HashSet is dependent on the HashMap implementation, TreeSet bottom-level relies on SORTEDMAP implementation.

2). Map map interface, which is the Key-vallue key-value pair.

Map is a Key-value key-value pair mapping interface. Where key cannot have duplicate elements (a partial implementation class can have a null object). Map implementation classes are mainly: HashMap, TreeMap, Linkhashmap, Hashtable and so on.

3). Iterator interface, which is an iterator. is a tool that iterates through the collection.

4). Arrays and collections are tool classes that manipulate arrays and collections. There are many methods that have been implemented to better manipulate arrays and collections.

3. Detailed Collection interface

1). Define
public interface Collection<E> extends Iterable<E>

2). Some common APIs for collection

boolean add(E e) 向集合添加对象,若其发送改变则返回true。boolean addAll(Collection<? extends E> c)void clear()boolean contains(Object o)  通过equals()方法判断该集合是否有该元素,若有返回true。Iterator<E> iterator() 返回Iterator。boolean remove(Object o) 移除集合中与该对象相等(o.equals(e))的第一个对象,若集合发送改变,返回true。boolean isEmpty()int size() 返回该集合包含元素的数量。<T> T toArray(T[] a) 返回包含此集合中所有元素的数组,数组元素必须是T类型或者T类型的父类。
List interface

1). Define
public interface List<E> extends Collection<E>
Lists can maintain elements in a particular sequence, with no elements indexed, and duplicate elements are allowed.

2) List of common APIs

boolean add(E e) 向集合添加对象,若其发送改变则返回true。boolean addAll(Collection<? extends E> c)void clear()boolean contains(Object o) 通过equals()方法判断该集合是否有该元素,若有返回true。Iterator<E> iterator() 返回Iterator。boolean remove(Object o) 移除集合中与该对象相等(o.equals(e))的第一个对象,若集合发送改变,返回true。boolean isEmpty()int size() 返回该集合包含元素的数量。<T> T toArray(T[] a) 返回包含此集合中所有元素的数组,数组元素必须是T类型或者T类型的父类。// 相比Collection新增的APIE get(int index) 返回List中该索引的元素。int indexOf(Object o) 返回该元素在List的索引,通过equals()方法比较,若在返回其索引,不在返回-1。ListIterator listIterator() 返回listIterator。E remove(int index)  移除List指定索引的元素,返回该元素。E set(int index,E element) 替换List指定索引的元素,返回该元素。

3) class that implements the list interface

arraylist (thread out of sync)
public class Arraylist<e> extends Abstractlist implements List<e>, Randomacess, Cloneable, Serializable
ArrayList is a linear table implemented with arrays that are excellent at random access and are our most commonly used collection.

linkedlist (thread out of sync)
pulbic class Linkedlist<e> extends abstractsequentiallist<e> implements LIST<E> Deque<e>, cloneable, serialzable
LinkedList is implemented in a doubly linked list, with excellent insertions and deletions.

Vector (thread safe)
public class Vector<E> extends AbstractList<E> implements List<E>, RandomAcess, Cloneable, Serializable
And ArrayList are basically the same, but vectors are thread-safe.

Stack (thread safe)
public class Stack<E> extends Vector<E>
Stack based on vector implementation (LIFO).

Where ArrayList, LinkedList can be converted to thread safety through the Collections.synchronizedlist () method.
For example:List arrayList = Collections.synchronizedList(new ArrayList())

Set interface

1). Define
public interface Set<E> extends Collection<E>
Set is an interface that inherits from collection, and set is a collection that does not include duplicate elements. It maintains its own internal ordering, so random access does not make any sense, and some sets allow a null object.

2). The set API is exactly the same as the collection.

3). Class that implements the set interface

HashSet (thread out of sync)
public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable
HashSet is the fastest collection of queries because it relies on hashmap internally. A collection element cannot be duplicated and can have a null object. The order of its inner elements is determined by the hash code, so it does not guarantee the set's iteration order, especially because it does not guarantee that the order will be constant.
Note: The element is stored byequals()hashCode()Method comparison is repeated, so we need to override the object'sequals()hashCode()Method.

TreeSet (thread out of sync)
pulbic class TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, Serializeble
TreeSet internally relies on TreeMap, which is implemented internally by a red-black tree, and always generates an ordered set. The collection element cannot be duplicated, and there can be no null object. TreeSet are ordered, so the objects that are stored must be implementedComparable<E>interface, or construct the method into the comparer (implementingComparator<E>Interface Class).
Note: The element is stored bycompareTo()Method orcompare()method to compare whether the comparison is repeated and the size.

Linkhashset (thread out of sync)
public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, Serializable
Linkhashset internally relies on LINKEDHASHMAP, which guarantees the insertion order of elements. A collection element cannot be duplicated and can have a null object.
Note: Its storage element isequals()hashCode()Method comparison is repeated, so we need to override the object'sequalshashCode()Method.

Map interface

1). Define
Code style= "BORDER:0; font-size:90%; Background-color: #d6dbdf; Color: #2c3e50; padding-top:2px; padding-right:4px; padding-bottom:2px; padding-left:4px ">public interface Map<k,v>
Map is a Key-value key-value pair that provides a key-to-value mapping. A key in a map cannot have the same element, and value can have the same element. A key in a partial map implementation class can have a null object.
2). Common API for map

booleancontainsKey(Object key)  返回该Map是否有该key。boolean containsValue(Object value)  返回该Map是否有该Value。Set<Map.Entry<K,V>> entrySet()  返回该Map包含的所有映射关系Map.Entry<K,V>的Set集合。V get(Object key)  返回该Map中key映射的value对象,如果Map中没有该key对象返回null。Set<K> keySet()  返回该Map中的所有key的Set集合。V put(K key,V value)  把key-value映射放入Map中,如果Map中有该key则返回原来key对应的value,否则返回null。V remove(K key)  如果Map中有该key返回原来key对应的value,否则返回null。int size()  返回Map中key-value映射的数量

3). Related classes that implement the map interface
HashMap (thread out of sync)
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable
HashMap are internally implemented as hashes, arrays, and linked lists. It is designed for quick queries, unordered. Its key cannot be duplicated but can have a null object.
Note: The key cannot have the same element as theequals()hashCode()method to compare, so we need to rewrite the key object'sequals()hashCode()Method.
HashTable (thread synchronization)
public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, Serializable
Hashtable inherits the Dicionary class and implements the map interface. Hashtable performance is less than HashMap, but it is thread-safe. This class is used less. Neither its key nor value can be a null object.
TreeMap
public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>, Cloneable, Serializable
TreeMap an ordered hash of the inner red-black tree. Its key cannot be duplicated and cannot have a null object. TreeMap are ordered, so the objects that are stored must be implementedComparable<E>interface, or construct the method into the comparer (implementingComparator<E>Interface Class).
Note: The element is stored bycompareTo()Method orcompare()method to compare whether the comparison is repeated and the size.
Linkedhashmap
public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>
Linkedhashmap inherits from HashMap and implements the map interface. Its key cannot be duplicated but can have a null object. It guarantees the insertion order of the elements.
Note: The key cannot have the same element as theequals()hashCode()method to compare, so we need to rewrite the key object'sequals()hashCode()Method.
Weakhashmap
Not used, not very clear.

Iterator interface

1). Define
public interface Iterator<E>
Iterator is the iterator interface for the collection. The collection can be traversed via iterator
Note: Iterator is a fail-fast mechanism when traversing collection. That is, when a thread a passes through iterator to traverse a collection, if the contents of that collection are changed by other threads, thread a accesses the collection and throws an Concurrentmodificationexception exception, resulting in a fail-fast event.
2). Common APIs for iterator

abstract boolean hasNext()abstract E next()abstract void remove()
6.references

http://alexyyek.github.io/2015/04/06/Collection/
http://blog.csdn.net/tsyj810883979/article/details/6897043

Java Container Classes

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.