To store a custom object in the collection Yes, the design of the custom object

Source: Internet
Author: User

Custom Objects

  By manipulating the list, set, and MAP collections, the definition of the custom class differs.

1. Custom objects in the list collection

Because the list underlying determines whether the collection is the same as the Equals method, overriding the Equals method when customizing the class

Example:

Custom class Personclass person{private String name;private int age; Person (String name, int.) {this.name = Name;this.age = age;} Public String GetName () {return name;} public int getage () {return age;} public boolean  equals (Object obj) {if (!) ( obj instanceof person) return false; Person P = (man) Obj;return this.name.equals (p.name) && this.age = = p.age;//by name and age, determine whether the same object}}

2.HashSet class when custom class

Because of the hashset underlying data structure hash table, the Hadhcode value and the Equals method are used to determine whether an object is the same

Example:

Class student{private String name;private int age; Student (String name, int age) {this.name = Name;this.age = age;} public int hashcode () {//system.out.println (this.name+ "... hascode"); return Name.hashcode () + age*39;} public boolean equals (Object obj) {if (!) ( obj instanceof Student)) return false; Student stu = (Student) obj;//system.out.println (this.name + "equals" + Stu.name); return This.name.equals (Stu.name) && This.age==stu.age;} Public String toString () {return name+ "..." +age;}}

  

3.TreeSet class when storing custom classes

Since the bottom of TreeSet is a two-fork tree data structure, the CompareTo method is used to determine whether it is a consent object, there are two ways;

Way One:

By implementing Compareable interface, covering CompareTo method;

Example:

Class Studnet_2 implements comparable<object>{private String name;private int age; Studnet_2 (String name, int age) {this.name = Name;this.age = age;} Public String GetName () {return name;} public int getage () {return age;} Based on the streeset underlying data structure, the collection implements element conformance through the CompareTo method public int compareTo (Object obj) {if (!) ( obj instanceof studnet_2)) throw new RuntimeException ("This is not a student object"); Studnet_2 stu = (studnet_2) obj;if (This.age > Stu.age) return 1;if (this.age = stu.age) return This.name.compareTo ( Stu.name); return-1;}}

 

Way two:

By defining the comparator implementation;

Example:

  

Class Strlencomparator implements comparator//comparator {public int compare (Object o1,object O2) {string S1 = (string) O1; String s2 = (string) o2;if (S1.length () >s2.length ()) Return 1;if (S1.length () ==s2.length ()) return 0;int num = new Integer (S1.length ()). CompareTo (New Integer (S2.length ())), if (num==0) return S1.compareto (S2); return num;}}

  

Summary: when a custom class object needs to be stored as a parameter in the collection, it is important to take into account the data structure of the collection, in order to standardize the custom class;

At the same time, in the development process, custom-like best cover the original comparison method .

To store a custom object in the collection Yes, the design of the custom object

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.