Set aggregate

   Just repeat , There is no order
  HashSet Of add Method will call hashCode and equals, So it's stored in HashSet Objects in need to override these two methods .

   To repeat , And it can be sorted in some order
  TreeSet Of add Method turns the object into Comparable, And then call compareTo Method , So it's stored in TreeSet Objects in must implement Comparable, rewrite compareTo Method

  HashSet Subclasses of , To repeat , And keep the storage order

4. Iterative way
   Using Iterators Iterator
   Use enhancements for loop

 HashSet principle :
 * If we want a set to have the ability to repeat , It can be in its add Method to check whether the object to be added exists in the collection . 
 * Iterate over each element in the set , Compare with what you want to add , If the same , It doesn't exist .
 * If you use the above method , When there are too many set elements , It's going to be inefficient .

* for example : There are 1 Ten thousand elements , When storing the next one , Need and front 1 Ten thousand are more , Low efficiency .
 * HashSet How it works :
 *    Every time you store an object , Call the object's hashCode() Method , Calculate a hash value . Look in the collection for elements with the same hash value .
 *     If there is no hash value for the same element , Deposit directly into .
 *     If there are elements with the same hash value , Use one by one equals() Methods to compare .
 *      The comparison results are all false Just deposit .
 *      If the comparison turns out to be true Then it doesn't exist .
 * How to save a custom class object into HashSet Go ahead and repeat
 *    Class must override hashCode() Methods and equals() Method
 *   equals() Method
 *   hashCode() Method to ensure that objects with the same properties return the same value , Objects with different properties should be as different as possible

TreeSet principle :
 * TreeSet When storing objects , Can sort , But you need to specify the sort of Algorithm
 * Integer Can sort ( There is a default order ), String Can sort ( There is a default order ), Exception occurred when storing custom class ( There is no order )
 * If you want to store the object of a custom class in TreeSet Sort , Then it must be realized Comparable Interface
 *    On a class implement Comparable
 *    rewrite compareTo() Method
 *    Define a comparison algorithm within a method , According to the size , Returns a positive number, a negative number, or zero
 *    In the use of TreeSet When storing objects , add() Method will be called automatically compareTo() Methods for comparison , Use... According to the comparison Binary tree Form for storage

Java Gather them together HashSet/TreeSet More articles on the principles

  1. Java Set the framework of TreeSet elementary analysis

    Java Set the framework of TreeSet elementary analysis One .TreeSet review : 1.1TreeSet brief introduction : TreeSet yes Java Important members of the collection framework , First look at it. TreeSet stay jdk1.8 It's the definition of : public ...

  2. Java Collection Series 17 TreeSet

    java Collection series directory : Java Collection Series 01 General framework Java Collection Series 02 Collection framework Java Collection Series 03 ArrayList Detailed introduction ( The source code parsing ) And use examples Java ...

  3. Java aggregate :HashSet And ArrayList

    Set A set is out of order and cannot be repeated .List Sets are ordered and repeatable . Java aggregate :HashSet And hashCode.equals  The blog has talked about this issue , But the explanation is not clear enough . Take a small example ...

  4. Java aggregate HashMap & HashSet Pick up

    Java aggregate HashMap & HashSet Pick up @author ixenos Abstract :HashMap Internal structure analysis Java HashMap It's a conflict linked list It's easy to see from the picture above , If you choose ...

  5. Java Set the framework of TreeSet

    sketch TreeSet Is based on TreeMap As a sort of storage . An ordered set with de duplication Inherited from AbstractSet,AbstractSet Realized equals and hashcode Method Realized NavigableSet ...

  6. Java aggregate ---ArrayList Implementation principle of

    Catalog : One . ArrayList summary Two . ArrayList The implementation of the 1) Private property 2)  Construction method 3) Element store 4) Element reading 5) Element delete                  6) Adjust the size of the array ...

  7. java Gather them together ArrayList,TreeSet and HashMap analysis

    java Set is an important and difficult point , If we deliberately remember all the usages and differences, it is not very realistic , I've been using related collection classes , But I didn't study the differences carefully , Now let's analyze and summarize some of the collections that are frequently used , The purpose is to make ...

  8. screwing java Gather them together HashSet Source code analysis

    problem (1) aggregate (Collection) And collection (Set) What's the difference? ? (2)HashSet How to ensure that the added elements are not repeated ? (3)HashSet Whether to allow null Elements ? (4)HashSet Is it orderly ? (5) ...

  9. Java aggregate :HashSet Source code analysis

    Java aggregate ---HashSet Source code analysis   One .  HashSet summary : HashSet Realization Set Interface , By hash table ( It's actually a HashMap example ) Support . It does not guarantee set  Order of iteration : In particular, it does not guarantee that ...

Random recommendation

  1. ZOJ Problem Set - 1394 Polar Explorer

    The subject is still simple , But myself WA Several times , Sum up : 1. A summary of the input , Plus last time ZOJ Problem Set - 1334 Basically Speaking ac Code and summary summary of this topic Subject requirements ...

  2. public POI export Excel Method --java

    I did some data at the beginning Excel Export function , But later, each export needs to write some similar code , After a little research, I wrote a common export method . It's used here POI, And then it's written as a public class , Pass in the formatted data , Just ...

  3. shell Mark the input 、 standard output 、 Error output

    shell You may often see :>/dev/null  2>&1  eg:sudo kill -9 `ps -elf |grep -v grep|grep $1|awk '{print ...

  4. from Project 2007 export WBS Chart to Visio 2007

    Microsoft official website says : stay Microsoft Office Project 2007 in ,Visio WBS Chart Wizard has been replaced by visual report . You can use Microsoft Office Visio Professi ...

  5. About abbr Some uses of

    <span style="margin: 20px;"> position 000 Set up :</span><abbr title=' Group 2, group 1, Jinlong Township, Wenchang Town, Zitong County, Mianyang City, Sichuan Province ...

  6. Libgdx Realize asynchronous loading network pictures and save them to SD Card or data/data Under the catalog

    Libgdx Realize asynchronous loading network pictures and save them to SD Card or data/data Under the catalog , When there are pictures here , Read pictures directly from local , If there are no local pictures , Images will be loaded asynchronously from the server package com.example. ...

  7. Elegant python - yield A concise tutorial

    It turns out that there are still a lot of people who are interested in yield incomprehension , The fog , So try to put it in words . Only if the function contains yield sentence , It returns a generator . So instead of looking at it as a function definition , Think of it as a generator definition . Function use return return , And generate ...

  8. 201521123016 《Java Programming 》 The first 3 Weekly learning summary

    1. This week's learning summary 2. Written work 2.1 Code reading public class Test1 { private int i = 1;// This line cannot be modified private static int j = 2; ...

  9. Run time is obtained by index tuple Elements -C++17

    // Run time is obtained by index tuple Elements -C++17 // Need support C++17 And above ,VS2017 15.5.x.CodeBlocks 16.01 gcc 7.2 // See :http://purec ...

  10. MyEclipse Easy to use shortcut key summary

    MyEclipse There are many shortcut keys commonly used in , Use some of these shortcut key combinations properly , Can effectively improve the efficiency and quality of development . 1.Ctrl + Shift + R: Open resources . You can find and open any file in the workspace , And support the use of general configuration ...