1 package com.tn.treeSet; 2 3 public class Student { 4 private String name; 5 private int age; 6 public Student(){} 7 public Student(String name, int age) { 8 super(); 9 this.name = name; 10 this.age = age; 11 } 12 public String getName() { 13 return name; 14 } 15 public void setName(String name) { 16 this.name = name; 17 } 18 public int getAge() { 19 return age; 20 } 21 public void setAge(int age) { 22 this.age = age; 23 } 24 @Override 25 public String toString() { 26 return "Student [name=" + name + "]"; 27 } 28 }
Student
1 package com.tn.treeSet; 2 3 import java.util.TreeSet; 4 5 public class TreeSetDemo { 6 public static void main(String[] args){ 7 TreeSet<Student> students=new TreeSet<>();//后一个尖括号内可以省略类型 8 Student student1=new Student("武松",30); 9 Student student2=new Student("林冲",31); 10 Student student3=new Student("鲁智深",29); 11 students.add(student1); 12 students.add(student2); 13 students.add(student3); 14 System.out.println(students); 15 } 16 }
TreeSetDemo
运行结果:
【java】TreeSet
时间: 2024-10-08 22:38:07