码迷,mamicode.com
首页 > 其他好文 > 详细

TreeMap集合之传入比较器

时间:2018-05-14 21:36:07      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:system   pack   rri   ack   student   int   compare   ati   oid   

package com.day15.Map;

import java.util.Comparator;
import java.util.TreeMap;

import com.day15.bean.Student;

public class TreeMapOne {

  public static void main(String[] args) {
    TreeMap<Student, String> tm=new TreeMap<>(new Comparator<Student>() {

      @Override
      public int compare(Student s1, Student s2) {
        int num=s1.getName().compareTo(s2.getName());
        return num==0 ? s1.getAge()-s2.getAge() :num;
      }

    });
  tm.put(new Student("Kobe",20), "LA");
  tm.put(new Student("KG",21), "Boston");
  tm.put(new Student("PP",22), "Boston");
  tm.put(new Student("Allen",23), "Nanjing");
  System.out.println(tm);//{Student [name=Allen,age=23]=Nanjing, Student [name=KG,age=21]=Boston, Student [name=Kobe,age=20]=LA, Student                                                                                                                                                                                                                                  [name=PP,age=22]=Boston}

  }
}

TreeMap集合之传入比较器

标签:system   pack   rri   ack   student   int   compare   ati   oid   

原文地址:https://www.cnblogs.com/zhujialei123/p/9038090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!