首页 > 代码库 > 【java】TreeSet
【java】TreeSet
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 }
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 }
运行结果:
【java】TreeSet
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。