Java-preliminary Understanding-Common Object API (Collection framework-map collection-hashmap store custom objects)

Source: Internet
Author: User
Tags set set

One.

Use the three sets mentioned earlier.

To account for the demand, the student is the key, the attribution is the value. Whether the place belongs to a string is debatable. If the place of attribution is relatively simple, use Tianjin, Shanghai .... These are the strings. If the place of attribution is more complex, Beijing's Haidian ..., it is recommended to encapsulate the attribution to the object. Why? When the attribution is encapsulated into an object, it has a province field, a city field, a zone field ... These fields you want to go alone to get, attribution to the object of their own clearest, so this time to be encapsulated. Remember, once things become complex, encapsulate objects. Make complex things simple. If it is a simple answer, it is directly on the string, without encapsulating the object.

in the first example, the HashMap collection is demonstrated. The map collection is described earlier, and the collection of map subclasses is discussed here. Regardless of the type of collection, it is a container that operates on various types of storage elements. Most of the different sets of elements of the operation of the same type, increase, delete, check ...

In this example, there are two ways to take an element, either keyset, or .... Show keyset first.

The set set contains the key type,

In fact, the way of writing here is very strange, from the distance, lisi,38 and Beijing three attributes in the collection, the writing method is rather strange, did not read.

The hash is unordered and the output is unordered.

Now modify the program, for Zhaoliu This object, modify the place of attribution, add more once.

Output as above, how to solve. If you think the keys are the same, there will be a value overwrite, which can be all columns because the keys are not the same. If you want to guarantee the same key, you must provide the key to judge the same basis.

Hash table structure to ensure that two methods, a said HashSet all understand, a said HashMap is confused. Since this key is stored in the HashMap (hash table), it has the hashcode and Equals method, has its own content, so in the person class, to do a replication action. ( set this piece, the code seems to be written separately ) is said to be student, but student inherited the person,student there is no attribute, directly in the person to write the good. Now in the person class, add the Hashcode and Equals methods, and then look at the output.

Shanghai belongs to the object, was Tieling covered.

Now mention a small thing, if you do not write the two sentences in the comments, but the last sentence. They are a meaning.

Java-preliminary Understanding-Common Object API (Collection framework-map collection-hashmap store custom objects)

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.