Java Basics - Collections Framework and its implementation class --Set

A .Set definitions and concepts:
the Set Collection is a sub-interface, analog sets on the concept of mathematics.
Set collection storage characteristics:
1): Do not allow duplicate elements.
2): No record has added an element of order.

Set contains only the Collection methods inherited from, but can not remember the order Set added, may not contain duplicate elements. When you are trying to add two identical elements Set into the collection, the add operation fails, the Add () method returns to false .
Set determines whether two objects are equal with ** equals, ** == instead. That equals compare two objects returns true, Set collection will not accept the two objects.

Two .HashSet
HashSet Set interface is the most common implementation class, as the name suggests, the underlying spent the hash table (hash / hash) algorithm.
The underlying fact is an array, the meaning of existence is to provide query speed, insertion speed is faster but suitable for a small amount of data insertion.

How to determine whether two objects are the same problems in the HashSet:
. 1):. Comparison of the equals two objects are equal Returns true, then the objects are the same.
2): the hashCode method object returns two values are equal.

HashCode determines the value of the object storage locations in the hash table.
Both: indispensable.
When a new object is added to the HashSet set when the first value of the return is determined hashCode objects and collections of objects:
1): range: directly storing the new object to the specified location hashCode.
2): is equal to: continue judging new objects and collections of objects equals comparison.
1>: the same hashCode, equals is true: it is considered to be the same an object is not stored in the hash table.
2>: the same as the hashCode, equals to false: very troublesome, before the object is stored on the same channel as the linked list (rejection, troublesome operation).
the hashCode and equals method of object importance:
each of the hash table stored in the object equals and hashCode method had to provide, is used to determine whether the same object.
object is stored in the hash table, should override equals and hashCode method, and ensures time equivalent equals, hashCode should be equal.

Three .LinkedHashSet Class
List Interface: allows duplicate elements, has recorded the order of addition.
The Set Interface: do not allow duplicate elements, has not recorded the order of addition.
Demand: do not allow duplicate elements, but need to ensure that the order has added.
LinkedHashSet: bottom only hash table and linked list algorithms
hash table: to ensure the uniqueness of this case is HashSet, in a hash table of elements in no particular order..
the list: the element has added to record order.
Here Insert Picture Description
four .TreeSet class
TreeSet collection of only the bottom there are red-black tree algorithm, it will use the default storage elements natural ordering (small to large).
Note: You must ensure TreeSet elements of the object in the collection is the same data type, otherwise an error.
TreeSet collation:
natural order (ascending large):
TreeSet compareTo method call set of elements to compare the size relation of elements, then the set of elements in ascending talk (small to large).
Note: a collection of elements required to obtain TreeSet java.util.Comparable implement the interface.
Here Insert Picture Description
Classes in java.util. comparable interface: comparable
cover public int compareTo (Object o) method, comparison rules written in this method.
in this method, comparison of the current (this) and the Parameter object o comparison (strictly speaking is the comparison of data object, such as sorting according to the age).
the this> o: returns a positive integer. 1.
the this <o: negative integer -1.
this == o: returns 0. In this case two objects that for the same object.

In the natural order of the TreeSet that if two objects being compared compareTo method returns zero is considered to be the same object.

Custom sort (from big to small, according to the length of the name of the sort):
delivery java.lang.Comparator object TreeSet constructor and cover public int compare (Object o1, Object o2) re-write the rules of comparison.

For TreeSet collection, the use of either natural ordering, or use a custom ordering.
Determines whether two objects are equal rules:
natural ordering: the compareTo method returns 0;
custom ordering: compare method returns 0;

Performance comparison of five .Set implementation class summary
Here Insert Picture Description
implement the Set interface classes:
common characteristics:
1): elements are not allowed to repeat.
2): not thread-safe class.
Solution: Set s = Collections.synchronizedSet (Set Object);
HashSet: does not guarantee element has the order of addition.
underlying hash table algorithm is to have extremely high search efficiency.
determines whether two objects are equal rules:
. 1): comparison of the equals to true.
2): the same hashCode value.
requirement: the hash in the presence of an object element covers had equals and hashCode method.

A LinkedHashSet:
HashSet subclass, also the underlying algorithm uses a hash table, but are also used to maintain the linked list algorithm has the order of addition of the elements.
Determining whether two objects are equal HashSet and the same rules.
Because of the need to use a multi-chain two recording order of the elements, the performance with respect to low HashSet.
Usually less, if a set of requirements necessary to ensure that the element will not be repeated, but also need to add the record order, before choosing to use LinkedHashSet.

TreeSet:
no guarantee has added an element of order, but the elements of the collection will do the sorting operation.
Ground floor have a red-black tree algorithm (tree structure, relatively good at doing range queries).
TreeSet either have a natural ordering, or custom sort.
natural ordering: objects in claim TreeSet java.lang.Comparable collection must implement the interface, and covers compareTo method.
custom ordering: TreeSet required when building objects, object passing a comparator (must implement the interface java.lang.Comparator .)
covered in the comparator compare methods and to prepare comparison rules.
TreeSet elements of the object to determine duplicate rules:
. compareTo / compare method returns 0. If it returns 0, then regarded as the same object
HashSet do the equivalent of a high query efficiency high TreeSet do range queries efficiently .
and more of our situation, we are doing the equivalent query, do range in the index database query, which is why the number of structures is mainly used for indexing, to improve query efficiency.

Appendix: HashSet implementation principle
(1) HashMap based implementation, a default constructor is to build an initial capacity of 16, the load factor is 0.75 HashMap. HashMap object encapsulates a collection to store all the elements of the set of all elements into a HashSet HashMap actually be held by the key, and the HashMap is stored a the PRESENT value, which is a static Object object.

(2) When we try to put a class of objects as a HashMap key, or trying to put HashSet object of this class to save, to rewrite the class equals (Object obj) method and hashCode () method is very important, and either return these two methods must be consistent: if two class hashCode () returns the value of the same, which method also should return true comparison by equals (). Generally speaking, all involved in the calculation hashCode () Returns the value of the key attribute, it should be used as the equals () standard for comparison.

Other operations (3) HashSet are based on the HashMap.

It is based on the realization HashMap, HashSet HashMap used to hold all of the underlying element, so HashSet is easy to implement, operate related HashSet are basically related to directly call a method to complete the underlying HashMap, HashSet source code is as follows:

import java.util.AbstractSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Set;
 
import javax.swing.text.html.HTMLDocument.Iterator;
 
public class HashSet<E> 
          extends AbstractSet<E> 
          implements Set<E>, Cloneable, java.io.Serializable 
{ 
static final long serialVersionUID = -5024744406713321676L; 
 
// 底层使用HashMap来保存HashSet中所有元素。 
private transient HashMap<E,Object> map; 
   
// 定义一个虚拟的Object对象作为HashMap的value,将此对象定义为static final。 
private static final Object PRESENT = new Object(); 
 
  
//  默认的无参构造器,构造一个空的HashSet。
//  
//  实际底层会初始化一个空的HashMap,并使用默认初始容量为16和加载因子0.75。
   
public HashSet() { 
map = new HashMap<E,Object>(); 
} 
 
  
//  构造一个包含指定collection中的元素的新set。
// 
//  实际底层使用默认的加载因子0.75和足以包含指定
//  collection中所有元素的初始容量来创建一个HashMap。
//  @param c 其中的元素将存放在此set中的collection。
  
public HashSet(Collection<? extends E> c) { 
map = new HashMap<E,Object>(Math.max((int) (c.size()/.75f) + 1, 16)); 
addAll(c); 
} 
 
  
//  以指定的initialCapacity和loadFactor构造一个空的HashSet。
// 
//  实际底层以相应的参数构造一个空的HashMap。
// @param initialCapacity 初始容量。
//  @param loadFactor 加载因子。
    
public HashSet(int initialCapacity, float loadFactor) { 
map = new HashMap<E,Object>(initialCapacity, loadFactor); 
} 
 
  
//  以指定的initialCapacity构造一个空的HashSet。
// 
//  实际底层以相应的参数及加载因子loadFactor为0.75构造一个空的HashMap。
//  @param initialCapacity 初始容量。
  
public HashSet(int initialCapacity) { 
map = new HashMap<E,Object>(initialCapacity); 
} 
 
 
//  以指定的initialCapacity和loadFactor构造一个新的空链接哈希集合。
//  此构造函数为包访问权限,不对外公开,实际只是是对LinkedHashSet的支持。
// 
//  实际底层会以指定的参数构造一个空LinkedHashMap实例来实现。
//  @param initialCapacity 初始容量。
//  @param loadFactor 加载因子。
//  @param dummy 标记。
    
HashSet(int initialCapacity, float loadFactor, boolean dummy) { 
map = new LinkedHashMap<E,Object>(initialCapacity, loadFactor); 
} 
 
  
//  返回对此set中元素进行迭代的迭代器。返回元素的顺序并不是特定的。
// 
// 底层实际调用底层HashMap的keySet来返回所有的key。
//  可见HashSet中的元素,只是存放在了底层HashMap的key上,
//  value使用一个static final的Object对象标识。
//  @return 对此set中元素进行迭代的Iterator。
 
 public Iterator<E> iterator() { 
     return map.keySet().iterator(); 
 
} 
  
// 返回此set中的元素的数量(set的容量)。
// 
//  底层实际调用HashMap的size()方法返回Entry的数量,就得到该Set中元素的个数。
//  @return 此set中的元素的数量(set的容量)。
   
public int size() { 
return map.size(); 
} 
 
  
//  如果此set不包含任何元素,则返回true。
// 
//  底层实际调用HashMap的isEmpty()判断该HashSet是否为空。
//  @return 如果此set不包含任何元素,则返回true。
  
public boolean isEmpty() { 
return map.isEmpty(); 
} 
 
  
//  如果此set包含指定元素,则返回true。
//  更确切地讲,当且仅当此set包含一个满足(o==null ? e==null : o.equals(e))
//  的e元素时,返回true。
//
//  底层实际调用HashMap的containsKey判断是否包含指定key。
// @param o 在此set中的存在已得到测试的元素。
// @return 如果此set包含指定元素,则返回true。
   
public boolean contains(Object o) { 
return map.containsKey(o); 
} 
 
 
// 如果此set中尚未包含指定元素,则添加指定元素。
// 更确切地讲,如果此 set 没有包含满足(e==null ? e2==null : e.equals(e2))
// 的元素e2,则向此set 添加指定的元素e。
// 如果此set已包含该元素,则该调用不更改set并返回false。
//
// 底层实际将将该元素作为key放入HashMap。
// 由于HashMap的put()方法添加key-value对时,当新放入HashMap的Entry中key
//与集合中原有Entry的key相同(hashCode()返回值相等,通过equals比较也返回true),
//新添加的Entry的value会将覆盖原来Entry的value,但key不会有任何改变,
//  因此如果向HashSet中添加一个已经存在的元素时,新添加的集合元素将不会被放入HashMap中,
//  原来的元素也不会有任何改变,这也就满足了Set中元素不重复的特性。
//  @param e 将添加到此set中的元素。
//  @return 如果此set尚未包含指定元素,则返回true。
  
public boolean add(E e) { 
       return map.put(e, PRESENT)==null; 
} 
 
//  如果指定元素存在于此set中,则将其移除。
//  更确切地讲,如果此set包含一个满足(o==null ? e==null : o.equals(e))的元素e,
//  则将其移除。如果此set已包含该元素,则返回true
// (或者:如果此set因调用而发生更改,则返回true)。(一旦调用返回,则此set不再包含该元素)。
// 
//  底层实际调用HashMap的remove方法删除指定Entry。
//  @param o 如果存在于此set中则需要将其移除的对象。
//  @return 如果set包含指定元素,则返回true。
  
public boolean remove(Object o) { 
return map.remove(o)==PRESENT; 
} 
 
  
//  从此set中移除所有元素。此调用返回后,该set将为空。
// 
//  底层实际调用HashMap的clear方法清空Entry中所有元素。
  
public void clear() { 
map.clear(); 
} 
  
//  返回此HashSet实例的浅表副本:并没有复制这些元素本身。
 
//  底层实际调用HashMap的clone()方法,获取HashMap的浅表副本,并设置到HashSet中。
  
public Object clone() { 
    try { 
        HashSet<E> newSet = (HashSet<E>) super.clone(); 
        newSet.map = (HashMap<E, Object>) map.clone(); 
        return newSet; 
    } catch (CloneNotSupportedException e) { 
        throw new InternalError(); 
    } 
} 
}
Published 99 original articles · won praise 2 · Views 2605

Guess you like

Origin blog.csdn.net/weixin_41588751/article/details/105300757