zoukankan      html  css  js  c++  java
  • 【TreeSet:请按照姓名的长度排序】

    package com.yjf.esupplier.common.test;
    
    import java.util.TreeSet;
    
    /**
     * @author shusheng
     * @description 请按照姓名的长度排序
     * @Email shusheng@yiji.com
     * @date 2018/12/17 10:36
     */
    public class TreeSetDemo {
    
        public static void main(String[] args) {
    
            // 创建集合对象
            TreeSet<Student> ts = new TreeSet<Student>();
    
            // 创建元素
            Student s1 = new Student("linqingxia", 27);
            Student s2 = new Student("zhangguorong", 29);
            Student s3 = new Student("wanglihong", 23);
            Student s4 = new Student("linqingxia", 27);
            Student s5 = new Student("liushishi", 22);
            Student s6 = new Student("wuqilong", 40);
            Student s7 = new Student("fengqingy", 22);
            Student s8 = new Student("linqingxia", 29);
            // 添加元素
            ts.add(s1);
            ts.add(s2);
            ts.add(s3);
            ts.add(s4);
            ts.add(s5);
            ts.add(s6);
            ts.add(s7);
            ts.add(s8);
    
            // 遍历
            for (Student s : ts) {
                System.out.println(s.getName() + "---" + s.getAge());
            }
    
        }
    
    }
    
    class Student implements Comparable<Student> {
    
        private String name;
        private int age;
    
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public int compareTo(Student s) {
    
            int num1 = this.name.length() - s.name.length();
            int num2 = num1 == 0 ? this.name.compareTo(s.name) : num1;
            int num3 = num2 == 0 ? this.age - s.age : num2;
    
            return num3;
        }
    }
    终身学习者
  • 相关阅读:
    石头剪刀布技巧+个人经验总结
    能让你聪明的工作DEAL四法则,来自《每周工作四小时》书籍
    开发软件名称简写定义表
    罗永浩简历(自荐新东方的简历)
    感人微电影 《健康树》金赫及作品简介
    陈寅恪
    中国朝代顺序表
    Loading...加载图收集
    KeyBoardUtils.java——android键盘工具类
    LogUtils.java
  • 原文地址:https://www.cnblogs.com/zuixinxian/p/10340924.html
Copyright © 2011-2022 走看看