Java-preliminary Understanding-Common Object API (collection framework-treeset collection)

Source: Internet
Author: User
Tags comparable set set

One.

Next, the common subclass TreeSet in set is explained, and the characteristics of TreeSet are demonstrated.

The collection tells about the data structure inside different sets.

An iterator output is required here, and the result of the output is unordered. See the deposit in and take out is inconsistent, but the results obtained some rules, according to the character of the first alphabetical order to output.

This we do not call order, is a specified sequence, according to the order of the elements in the dictionary.

Look at the characteristics of TreeSet,

Now that we have some strings stored in them, the output is sorted in the order of the dictionaries. Some custom objects will be stored below.

The result shows an error, (as in the previous notation), the type conversion exception.

Line 19th, Wangwu error occurred. If you keep only the Zhangsan line, the result is fine. Keep two lines or an error,

TreeSet, what are you doing? To sort the elements, the sort will necessarily mean a comparison. Only one object is saved, no comparison is necessary. Save two objects to compare.

It is not a good time to keep two lines, because this person class has no comparison function. Some say that writing equals, what is needed here is a comparison, not a judgment of the same. The comparison is to divide the size, the situation is not two, is three kinds of.

Now, let's see what java.lang.Comparable is.

How does it feel to read the above? The person who normally describes something like this, what to describe. After the description, I will now put the person in the TreeSet collection to sort. Do you think person

Should not be in the basic functions already have, to have an extension function is to compare ( why not TreeSet collection master this function ?). )。 How does this comparison come about? It has itself been defined in the Congparable interface, if you want people to have a comparative, as long as the person to do a function extension, so that the implementation of comparable interface, it has a comparative function.

People need to compare, so to implement comparable (in Eclipse case can be, the name is too long can use out+ slash)

To inherit the implementation after the first, do not write inherit object also line. The Copareto method in the comparable interface is overridden in the person class. This returns an int, what is the result of the object comparison returned? Do not say 1, that is actually negative, the number can be changed at any time.

Now back to the original example, enter five elements, see, the result is no error.

There is only one output, but there is no error. We change the WANGWU to the first add position, and the result is only output WANGWU. The first deposit is WANGWU, when the storage Zhangsan, it and Wangwu, return is 0, return 0 means that the two elements are the same, and the TreeSet collection is a subclass of the set set, they all have a common feature, called the uniqueness of the guaranteed element.

Does TreeSet have anything to do with hashcode,equals? There is no relationship, at the beginning of the example, there are hashcode and equals two methods, the output is an error, also no line.

The TreeSet collection simply does not look at hashcode and equals, and because it writes a CompareTo method and returns 0, it is considered the same as the following element and Wangwu. So the way TreeSet sets the element uniqueness is particularly simple, which is based on ...

Because the structure is different, and the other containers are not the same, TreeSet look at the comparison results, as long as the 0, according to the same calculation.

Some people say, not so, you have to write something specific, meaning that the above to solve the abnormal operation is too rough. Without using some of the properties of the object itself, the direct CompareTo gets 0 and now wants to be fine.

It is the problem to put the person in the treeset without saying what the definite demand is.

It is a question of the name and age of the person, and in what way the sort is not clear. Now, let's start by making it clear that the person object's age is sorted from small to large. That means we're going to write this requirement in person's CompareTo method. If we return 0, then all the elements are the same, which is certainly not possible.

We want to convert the object type first, some say it needs to be judged. There is no need, and will be a unified change in the future. Now let's write it down in a simple way, judging by the need. It is important to add robustness judgement (instanceof) before the type is strong, especially before the reference data type, otherwise the classcast exception will occur. The following is the result of copying the CompareTo method in the person class.

Data is stored in the collection as a result of age comparison. The final display is possible, this is the code distributed in two class files, TreeSet should have other restrictions, not just the age of comparison in the current CompareTo method.

We look at the result of the output is small to large, how to adjust it to from large to small. Swap 1 and 1 in return. There is another way to swap this and p. Later there is a reversal of the sequence of actions, the source of return 1 and 1, and < and > are difficult to change, but this and p is a reference variable, method modification.

After the order is finished, the program is modified to set the age of the two custom objects to be the same.

The result of Zhouqi not, Wangwu still in. What is the reason? In the definition of the CompareTo method, the age of Zhouqi and Wangwu is neither greater nor less than, is equal to. Then the two elements are the same, the same object, so the Zhouqi is canceled.

Some people say that the judgment is one step less, and when the age is judged the same, the name should be judged again.

Note that when we write this comparison, we usually rely on the conditions are not unique, called there may be the main conditions at the same time, there are secondary conditions. It is called if the primary condition is the same, continue the comparison on a secondary basis.

Some conditions are not unique, then continue to write (said is the definition of the CompareTo method body). If the age is not in line, then follow the name row. The name is a string, and the string itself has a CompareTo method,

This method of it also comes from the interface with the comparable. This comparable interface (Unified interface) is implemented as long as the object wants to compare.

But these two kinds of writing is not very good, change a simple way.

After the modification, Wangwu and Zhouqi all appeared, and Wangwu in front.

This is what treeset can do to sort the elements. Why did you just store the string inside and sort it? Because the string itself implements the comparable.

Java-preliminary Understanding-Common Object API (collection framework-treeset 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.