Learn Java 15th Day Personal summary

Source: Internet
Author: User

DAY15 Personal Summary

First, the object class

1 , Concept:

ObjectA class is the root class of a class hierarchy. Each class is used Object as a superclass. All objects, including arrays, implement methods of this class.

2 , Object use of several important methods

1) ToString Method

The default is to return the memory address, when we print the object, the program will call the Tostrin method by default, we can override the ToString method to put it back to the property value

2) Hashcode method

The Hashcode method prints out a string of memory based on the address of a certain operation, the main function is to determine whether the same object

3) Equals method

In the object class, where equals is the address, you can override this method to make it compare content

Second, set set

1 , Concept:

The set set is an unordered set of disallowed duplicates, so-called disallow re-duplex refers to the set collection if the Access object, by default, will call the Hashcode and Equals method to compare the object is equal, set the bottom of the combination is the map set of key, so is unordered, The following describes the implementation class for the hashset of the set set and the TreeSet implementation class

2 , Hashset

By creating objects, use the following:

set<person> set = new hashset<> ();

The "<>" above means that generics are similar to the previous list collection, when HashSet adds data to the collection, the program automatically calls the Hashcode method to get the hashcode value, and if the result is different, it is considered to be a different object and is added to the collection. If the hashcode is the same, then call the Equals method to compare the values of each property, further confirm whether it is the same set, and not add if all are the same

If you add a custom object to the collection (in addition to the string data type and the base data type, which removes the system's own data type), the programmer is required to override the Hashcode method and the Equals method to determine whether the two objects are equal, and if the equality is not added to the collection, If not equal, the following error (type conversion exception) will be reported if the two methods are not overridden:

The reason is because the custom-built class does not rewrite the hashcode and Equals method, then the collection in the judgment of two objects can not be judged, all will be error

3 , TreeSet

By creating objects, use the following:

treeset<integer> set = new treeset<> ();

TreeSet collections and HashSet collections are different, treeset is to sort the collection in natural order, and if you need to send the data to the collection, you need to implement and rewrite the CompareTo in the interface comparable () method to determine whether two objects are equal and to sort the objects in the collection according to each property condition, and can help to remove duplicates if the caller is greater than the argument return positive represents ascending, and vice versa represents descending

4 , TreeSet the custom sort (Comparator )

Custom sorting is achieved by creating a new class to implement the comparable method in the comparator interface to achieve the custom ordering, the custom ordering is the use of the TreeSet construction method, you can directly pass to the construction method of their own written custom ordering to make the following:

treeset<student> TreeSet2 = new TreeSet (new Mycomparator ());

where comparator () is a custom sorted class written by the programmer, the example is as follows:

Third, Map collection

1 , Concept:

The map collection is a key value key and the value of the values are composed of the key value is unique, the value is allowed to repeat, the key value and value is one by one corresponding, the key value is the only constant amount, can be found through the key value of the content, if the key value is duplicated, Then the new key value content will overwrite the contents of the old key value, the map collection is mainly used for unified management of the collection

2 , Map use and traversal of collections

hashmap<string, string> map = new hashmap<> ();

The traversal is as follows:

Learn Java 15th Day Personal summary

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.