TreeSet: You can Sort the elements in the set collection by default , sorted by ASCII table, binary tree structureThe left fork is small, the right fork is big.Storing a custom objectDefine a class Student implement the comparable class to make the custom class comparableDefining attributes Age Define property name nameImplement the CompareTo () method, passing in another Student ObjectDetermines whether the age of the current Student object is gr
HashSet, edged, we are most used to. The other two, we are less used to, today, we look at their differences.ImportJava.util.HashSet;ImportJava.util.Set; Public classHashsettester { Public Static voidMain (string[] args) {SetNewHashset(); Set.add (15); Set.add (31); Set.add (12); Set.add (123); Set.add (42); for(Integer i:set) {System.out.println (i); } }}View CodeHashSet, the ability to quickly find key values.See its realization, depends on the implementation of HashMap . Print the log:421
One.The TreeSet collection can sort the elements in it, there are two sorting methods, and now we want to know what structure it is at the bottom, how does it determine the position of the element? It is for a reason, and its structure is highly efficient in terms of sequencing.When it comes to sorting, talk about choosing bubbles. Select bubble For comparison times, more, because it is turned around in the ratio. For
1-Letter Comparator Implementation method code in reverse order
Import Java.util.Comparator;
Import Java.util.Iterator;
Import Java.util.TreeSet;
public class TreeSetTest3 {public
static void Main (string[] args) {
TreeSet set = new TreeSet (new Mycomparator ()) ;
Set.add ("C");
Set.add ("A");
Set.add ("B");
Set.add ("E");
Set.add ("F");
Set.add ("D");
for (Iterator iter = Set.itera
TreeSet
Conditions
Same type
Design to sort
Comparable is natural (you can modify the case of a class)
Comparator is custom (in the case of a class cannot be modified) higher priority
Instructions for use:
Generally used is the natural sort, but when the class can not be modified to have to use the comparator method, the following demo using two ways.
Code Overall Description:
The Birthday property of the employee class is the custom class mydate type
The difference between HashSet and TreeSetI. Issues1. How does Hashset,treeset use the hashcode () and Equals () methods?2. Why do the objects in Treemap,treeset implement the comparable interface?Two. Answer:1. HashSet is implemented through the HASHMAP, TreeSet is implemented through TreeMap, only set with only map key2. Map key and set have a common feature is
customize a comparer to sort the elements in the TreeSet collection by the specified methodImport java.util.comparator;import java.util.iterator;import java.util.treeset;//Customize a comparator class Mycompare implements comparator{@Overridepublic int Compare (object O1, Object O2) {//TODO auto-generated method stubminstudent ms1= (Minstuden t) O1; Minstudent ms2= (minstudent) O2;int i=ms1.getname (). CompareTo (Ms2.getname ()); if (i==0) return ms1.
Features of Set set (compared to ArrayList)Unordered, uniqueTwo sub-classes that mainly study itHashSet CollectionA: The underlying data structure is a hash table (is an array of elements as linked lists)B: Hash table bottom-dependent two methods: Hashcode () and Equals ()Execution order:First, compare whether the hash value is the sameSame: Continue to execute equals () methodReturns true: The element is duplicated, not addedreturn false: Add elements directly to the collectionDifferent: Add el
Label: style blog HTTP color ar SP data on 2014
Threeset can sort objects in the set. When a treeset wants to add an object to the set, it will be inserted into the ordered object sequence.
Threeset comes with a sorting method that specifies the sorting rules for general data. If you want to specify your own sorting method, You have to rewrite the comparator method for a long time.
The following is an example of the built-in method sorting of thr
Combined with Huawei's machine questions to showObviously want to ask some students in school to do a questionnaire survey, in order to the objectivity of the experiment, he first used a computer to generate N 1 to 1000 random integer (n≤1000), for the repeated numbers, only one, the rest of the same number is removed, the different numbers correspond to different student's number. Then the numbers from small to large sorting, in line with the order to find students to do the investigation. Plea
1. TreeSet Store your custom objects and traverse Exercise 2:(1)Student. java1 Packagecn.itcast_06;2 3 /*4 * If an element of a class wants to be able to sort naturally, it must implement the natural sort interface5 */6 Public classStudentImplementsComparable {7 PrivateString name;8 Private intAge ;9 Ten PublicStudent () { One Super(); A } - - PublicStudent (String name,intAge ) { the Super(); - This.
TreeSet requires that the class to which the object belongs must implement the comparable interface, which provides the CompareTo () method of the comparison element, which is called back when the element is inserted and the method compares the size of the element. treemap requires that keys stored in a key pair map must implement the comparable interface to sort the elements according to the key. The sort method of the collections tool class has two
1 compare () is consistent with hashcode () and Equals () @Test//Custom Sorting Public voidTestTreeSet2 () {//1. Create an anonymous class object that implements the comparator interfaceComparator com =NewComparator () {//add an object of the student class to TreeSet, in this compare method, indicate which property of the customer is sorted by Public intCompare (Object o1,object O2) {if(O1instanceofStudent O2instanceofStudent) {Student S1=
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.