Set-TreeSet-Comparable and treeset

Source: Internet
Author: User
Tags comparable

Set-TreeSet-Comparable and treeset

Student Class: name and age attributes
1 package com. bjpowernode. test01_set2_Comparable; 2/* 3 * T: type 4 */5 public class Student implements Comparable <Student> {6 private String name; 7 private int age; 8 public String getName () {9 return name; 10} 11 public void setName (String name) {12 this. name = name; 13} 14 public int getAge () {15 return age; 16} 17 public void setAge (int age) {18 this. age = age; 19} 20 public Student (String nam E, int age) {21 super (); 22 this. name = name; 23 this. age = age; 24} 25 public Student () {26 super (); 27 // TODO Auto-generated constructor stub28} 29/* @ Override30 public int hashCode () {31 final int prime = 31; 32 int result = 1; 33 result = prime * result + age; 34 result = prime * result + (name = null )? 0: name. hashCode (); 35 return result; 36} 37 @ Override38 public boolean equals (Object obj) {39 if (this = obj) 40 return true; 41 if (obj = null) 42 return false; 43 if (getClass ()! = Obj. getClass () // determines whether the types of the two "objects" are the same. 44 return false; 45 Student other = (Student) obj; // perform a downward transformation of 46 if (age! = Other. age) 47 return false; 48 if (name = null) {// avoid NullPointerException exception 49 if (other. name! = Null) 50 return false; 51} else if (! Name. equals (other. name) 52 return false; 53 return true; 54} */55/* 56 * when the current object and the parameter object are both returned: 057 * when the current object is greater than the parameter object: 1 positive 58 * when the current object is smaller than the parameter object:-1 negative 59 */60 @ Override61 public int compareTo (Student s) {62 // sort by age. if the age is the same, sort by name 63 if (this. age = s. age) {64 // sort by name, call compareTo65 return this of the string. name. compareTo (s. name); 66} 67 return this. age-s. age; 68}69 70}
TreeSet storage, traversal, and sorting output
1 package com. bjpowernode. test01_set2_Comparable; 2 3 import java. util. treeSet; 4/* 5 * elements stored in TreeSet. By default, repeated items are removed in the natural sorting mode, and the natural sorting method is specified in the 6 * String and 8 packaging classes in the sorting mode: java. lang. comparable interface, rewrite the compareTo Method 7 * When the custom class is put into the TreeSet, java is also implemented. lang. comparable interface, override compareTo Method 8 */9 public class StudentTreeSetTest {10 public static void main (String [] args) {11 TreeSet <Student> tree = new TreeSet <> (); 12 Student s = new Student ("Mike", 18); 13 Student s1 = new Student ("Join", 28); 14 Student s3 = new Student ("Join ", 18); 15 Student s4 = new Student ("Smith", 18); 16 tree. add (s); 17 tree. add (s1); 18 tree. add (s3); 19 tree. add (s4); 20 System. out. println (tree. size (); 21 System. out. println ("Name \ t age"); 22 for (Student stu: tree) {23 System. out. println (stu. getName () + "\ t" + stu. getAge (); 24} 25} 26}

 

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.