Java Common Data Set analysis

Source: Internet
Author: User

1.hashmap
Realize map interface, key value to store, key to calculate hash value, can directly use key value to obtain value object, more convenient.
Unordered when storing numbers, cannot duplicate key values

2.hashset
From the following source can be seen HashSet is based on HASHMAP implementation, you can take it as a value of the empty HashMap, is the hashmap of encapsulation and utilization. Implement set interface, store by object, compute hash value by storage object, and all iterations can read out object.
Unordered, not repeatable values when storing numbers

public class Hashset<e> extends abstractset<e> implements Set<e>, Cloneable, Java.io.Serializabl

    e {static final long serialversionuid = -5024744406713321676l;

    private transient hashmap<e,object> map;

    Dummy value to associate with a Object in the backing Map private static final Object PRESENT = new Object ();
    Public HashSet () {map = new hashmap<> (); Public HashSet (collection<? extends e> c) {map = new hashmap<> ((int) (Math.max () c.size)
        + 1, 16));
    AddAll (c); Public hashset (int initialcapacity, float loadfactor) {map = new hashmap<> (initialcapacity, Loadfact
    OR);
    Public hashset (int initialcapacity) {map = new hashmap<> (initialcapacity); } hashset (int initialcapacity, float loadfactor, Boolean dummy) {map = new linkedhashmap<> (Initialcapa
    City, Loadfactor); } public iterator<e> IteraTor () {return Map.keyset (). iterator ();
    public int size () {return map.size ();
    public Boolean IsEmpty () {return map.isempty ();
    Public Boolean contains (Object o) {return map.containskey (o);
    Public boolean Add (E. e) {return Map.put (E, PRESENT) ==null;
    public boolean remove (Object o) {return map.remove (o) ==present;
    public void Clear () {map.clear (); }
}

3.collection
There are two basic interfaces for the data collection, collection and map. Implementation of the collection interface has set,list,queue, their implementation class should be very clear, is our common hashset,arraylist and so on, collection is their common interface. And here's what we're going to mention here are three confusing methods in collection Retainall,containsall and RemoveAll:

Boolean retainall (collection<?> C): Call this method to remove elements that do not exist in C in the Collection. In other words, if the element in collection is not found in C, the element in collection is removed and True (collection returns True if it is changed), and if all elements in collection are found in C, Does not change the collection and returns false. Here is the equivalent of an alternative to the intersection of two sets.
For example:

c1:1,2,2
c2:1,2
c1.retainall (C2);//This returns false because the element in the C1 exists in the C2 and does not need to be changed.

Boolean containsall (collection<?> C): Call this method to determine whether Collection contains all elements in C
Boolean RemoveAll (collection<?> C): Call this method to remove the elements that exist in C in all Collection, similar to Retainall, and return False if the colletion has not changed. Otherwise, returns True. Here is the equivalent of an alternative to two sets of the difference set.
For example:

c1:1,2,2,3
c2:1,2
c1.removeall (C2);//This returns true because the elements in the C1 exist in C2, and the elements in C1 are all removed except 3, and the C1 final output is only 3

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.