Java Object Collection

Source: Internet
Author: User

Set of Java Collection overview

Abstract
    • The collection of Java mainly has Set , List , Queue and Map four kinds of systems. these four systems are interfaces cannot be used directly, but in these four systems contain a lot of implementation classes can be used directly.
    • The difference between a set class and a fixed-length array is that the fixed-length array can store not only the base data type but also the object, but the collection class can only store objects. The object here refers to the object reference
    • All of the collection classes are located under the Java.util package, and later in order to deal with concurrency security problems in multithreaded environments, Java5 also provides some of the collection classes supported by multithreading under the Java.util.concurrent package.
    • The collection classes in Java are--collection and maps derived from two interfaces, where Set , Queue and List interfaces are Collection derived from interfaces, they represent unordered and ordered collections, respectively.
    • MapAll implementation classes of an interface are used to hold data that has a mapping relationship (associative array)
Collection interface
    • boolean add(Object o)Failed to return false.
    • boolean addAll(Collection o)Used to add all the elements in the collection C to the specified collection, failing the chapter false.
    • void clear()Clears all the elements in the collection, and the collection length becomes 0.
    • boolean contains(Object o)Determines whether the specified element is contained in the collection.
    • boolean containsAll(Collection c)Determines whether all elements in collection C are contained in the collection.
    • boolean isEmpty()Returns whether the collection is empty.
    • Iterator iterator()Returns an iteratior object that iterates through all the elements in the collection.
    • boolean remove(Object o)Delete the development element O, when there are multiple qualifying elements in the collection, just delete the first one and return True
    • boolean removeAll(Collection c)Removes all elements from the set C from the collection, if there is less than or equal to a delete successful edge returns TRUE.
    • boolean retainAll(Collection c)Removes all elements of the collection that are not collection C if there is at least one delete succeeded fan Hu true.
    • int size()Returns the number of elements in the collection.
    • Object[] toArray()Converts a collection to an array, and all the collection elements become corresponding array elements.
1. Set

SetIt is Collection , in fact, because it is its sub-interface. The only difference is that it is Set not allowed to contain duplicate elements. If the same element is forcibly added, the add() function returns FALSE. Unordered, non-repeatable collection.
SetThere are three implementation classes under the interface, respectively: HashSet , TreeSet and EnumSet , and HashSet there is a subclass LinkedHashSet .

1.1 HashSet Class

HashSet is the most common implementation class for set. Has the following characteristics:

    • There is no guarantee that the order is added, because the principle hashset is to store elements through the hash algorithm, so the order cannot be determined.
    • HashSet is not synchronous, and if there are multiple threads modifying the value of HashSet at the same time, it must be guaranteed to synchronize by code
    • The collection element value can be null.
Principle:
    • Focus One: The added element can only be an object.
    • Focus Two: Objects need to override equals() functions and HashCode() functions.
According to the key points are:
    • When you add an element to the HashSet collection, you first determine whether it is equal to one of the elements in the collection.
    • The criterion for judging equality is equal to the Equals () function of two elements.
    • In general, overrides equals() and hashCode() functions are required, especially since the hashcode value of the object is closely related to the hashCode() object's member variables after the function has been overridden (see later). It is important to pay attention to the hashcode changes when doing any function operations related to the object's member variables. Although the member variable will change, but the hashcode value will not change!
    • If they are not equal, then the storage location is selected based on the new computed hashcode value of the object. It should also be noted here that the hashcode is actually based on the member variable ———— because hashcode is calculated from the member variable!
classA@Override Public int hashcode() {return 1;}}classB@Override Public Boolean equals(Object obj) {return true;}}classC@Override Public Boolean equals(Object obj) {return true;}@Override Public int hashcode() {return 2;}} Public classMyTest { Public Static void Main(string[] args) {HashSet books =NewHashSet (); books.Add(New A()); books.Add(New A()); books.Add(New B()); books.Add(New B()); books.Add(New C()); books.Add(New C()); System. out.println(books);}}

Running the above code and based on Set the non-repeating rules, we can think that if the object that the collection is about to add needs to override the equals() function, but hashCode() the function is not rewritten, it will result in two results:
Either the same objects of different hashcode are stored in different locations causing the same elements to appear in the collection, or different objects of the same hashcode are chained to the same place.
But no matter what kind of result is a serious non Set -conforming definition.

Note about overriding Hashcode () a. When two objects are in a comparison equals()When the function returns True, HashCode()The function should also return an equal value.

A function that overrides an object equals() , typically by comparing all instance variables in that object. Rewrite is the HashCode() same, just use a certain algorithm 实例变量相等变成hashCode相等 , we can easily think of the linear operation ———— directly add up. The Hashcode calculation method for the basic data type is given below:

instance Variable type calculation Method
Boolean Hashcpde = (f?0:1);
Integer type (byte, short, int, char) Hashcode = (int) F;
Long Hashcode = (int) (f^ (f>>>32));
Float Hashcode = Float.floattobits (f);
Double Long L = double.doubletolongbits (f); Hashcode = F.hashcode ();
Reference type Hashcode = F.hashcode ();

The value of each instance variable computed by the first step is multiplied by the addition of a prime number (preventing the redundancy of direct addition) to get the new hashcode value of the object.

B. HashSetThe same object in the collection must be used during a program run hashCode, be sure to avoid modifying some instance variables of the object during the process.

Mainly in the process of running the program, it is possible to modify the variables in the collection elements may be, according to the previous rewrite algorithm and 原理 part can be known: 成员变量 , HashCode , equals these three basically equated.

classA Public intCount Public A(intCount) { This.Count= Count;}@Override Public Boolean equals(Object obj) {if( This= = obj) {return true;}if(obj! =NULL&& obj.GetClass() = = A.class) {A A = (a) obj;return  This.Count= = A.Count;}return false;}@Override Public int hashcode() {return  This.Count;}@Override PublicStringtoString() {return "A[count]"+count +"]";} Public classMyTest { Public Static void Main(string[] args) {HashSet books =NewHashSet (); books.Add(New A(5)); books.Add(New A(-3)); books.Add(New A(9)); books.Add(New A(-2)); System. out.println(books); Iterator it = books.iterator(); A first = (a) it.Next(); first.Count= -3; System. out.println(books); books.Remove(New A(-3)); System. out.println(books); System. out.println(Books.contains(New A(-3))); System. out.println(Books.contains(New A(5)));}}

(assuming that the first element here is 5) see the above code, first change the count value of the first element to 3, then there will be two count =-3 element exists, but!
Don't forget, even if the count of the first element is changed to-3, its hashcode is 5! It's the same as when you first joined HashSet! This can be found by outputting the hashcode of the entire collection element.
So when the back is about to remove (-3), actually the collection will first look for the storage location of Hashcode-3 ———— This time only the first count of 3 of the element satisfies the condition, so the deletion is correct, but then determine whether there is a-3 in the collection? According to Hashcode has been deleted return false; Is there a 5? Although the modified element can be found based on 5, equals also returns false because its count value is modified to 3.

Linkedhashset
    • Because the HashSet elements are stored according to the hash algorithm, the order in which the elements are accessed is often inconsistent with the order in which they are stored. Sometimes arcs are inconvenient. LinkedHashsetis a HashSet subclass, and the only difference is that the order of elements is maintained through a list of links.
    • But because the list is used, the performance is slightly worse.
1.2 TreeSet class

Java Object Collection

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.