Using TreeSet and Comparator, write Treesettestinner classes that require elements in the TreeSet "HashSet", "ArrayList", "TreeMap", "HashMap", "TreeSet", " LinkedList "is arranged in ascending and descending order and is implemented separately using anonymous inner classes and static inner classes.Method One:Static In
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 Overal
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
setSize (int size) {this.size = size;} private String color;
Private String shape;
private int size; Public Game (String materia,string color,string shape,int size) {this.materia=materia; this.color=color; this.shape=
Shape
This.size=size;
@Override public String toString () {return materia+ ">" +color+ ">" +shape+ ">" +size;} /** * Referee class is also called comparator. Enumeration for better control of comparison content * @author Len
: return 0 of compareTo.Features of the TreeSet set:
Two sorting methods:
Method 1: Make the elements in the set have a comparison, so that the objects added to the TreeSet set must implement the comparable interface to override the compareTo (Object obj) method.
This method also becomes the natural or default sorting of elements. (However, if the sorting elements are not written by myself and others do not
above, the TreeSet set will be compared using the compareTo method. In our program, Student with the same name and age will not be added to the set.
Next we will consider another scenario. For example, the code above in the company has been written by someone else, and we need another sort method, such as sorting by name. What should we do? We cannot modify the code written by others. At this time, we need to implement the compare method through the
sorting, such as descending, you can use the comparator interface. The interface contains an int compare (t O1, T O2) method, which is used to compare the sizes of O1 and O2.If you need to implement custom sorting, you need to create a TreeSet collection object and provide an comparator object associated with the TreeSet
Here is a brief introduction to Java's comparable internal comparator and comparator external comparator usage implementationSo let's do a sort of between objects, first build a model or call Javaben. As follows:The use of the 1.Java comparable internal comparator is implemented:Comparable internal
Comparablecomparable is the sort interface.If a class implements the comparable interface, it means "this class supports sorting." The class that implements the comparable interface supports sorting, assuming that there is now a list (or array) of objects for the class that implements the comparable interface, the list (or array) can be passed through Collections.sort (or Arrays.sort) to sort.In addition, the object of the class implementing the comparable interface can be used as a key in an or
based on the size of the collection elements and treeset them in ascending order. If you need to implement custom sorting, such as descending, you can use the comparator interface. The interface contains an int compare (t O1, T O2) method, which is used to compare the size of O1 and O2.
If you need to implement a custom sort, you need to create a TreeSet collec
How can a TreeSet set ensure that elements are unique?
TreeSet:
1. FeaturesTreeSet is used to sort objects. You can specify an order. objects are sorted in the specified order after they are saved.2. UsageA. Comparable)The add () method of the TreeSet class promotes the stored object to the Comparable type.Compare the compareTo () method of the called object with
conditions---------------------------------------------------------------------------------TreeSet the underlying data structure is a two-fork tree. The basis for guaranteeing the uniqueness of the element: CompareTo method return 0.TreeSet the first way of ordering: to make the elements themselves comparable, elements need to implement comparable interfaces, overriding CompareTo methods, which is also cal
Java basics-TreeSet, javatreeset
TreeSet is the implementation class of the SortedSet interface. Sorted is classified in English.
TreeSet ensures that the elements of the set are sorted. Compared with the HashSet set, TreeSet provides the following additional methods:
Method Name
Explanation
O2, returns 0 for O1 and O2 equal, and returns a positive number that represents O1 greater than O2.Publicclasstreesettest{publicstaticvoidmain (String []args) {treesettree=newtreeset ( Newmycomparator ()); tree.add (NewPerson); tree.add (Newperson); tree.add (Newperson); tree.add (new Person ()); System.out.println (tree); //[20,21,24,33]}}classperson{ intage;stringname;public person (intage) {this.age=age; }Nbsp;publicstringtostring () {return string.valueof (this.age);}}classmycomparatorimpl
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.