[自然排序] package com.hxl; public class Student implements Comparable<Student> { private String name; private int age; public Student() { super(); } public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getNa…
代码: 1 import java.util.*; 2 3 /** 4 * 学习自定义排序:继承Comparable接口,重写compareTo方法(排序规则). 5 * TreeMap容器的Key是自动排序的,Key为自定义类时,必须重写排序规则. 6 * Iterator迭代器遍历Map容器 7 */ 8 public class DiySort { 9 10 /** 11 * 测试 12 * @param args 13 */ 14 public static void main(Stri…
/*TreeSet * treeSet存入数据后自动调用元素的compareTo(Object obj) 方法,自动对数据进行排序 * 所以输出的数据是经过排序的数据 * 注:compareTo方法返回值有:负数,零,正数.分别表示小于,等于,大于 * 对于存入自定义的对象元素,要重写元素的compareTo(Object obj)方法 * 元素定义时,需要实现Comparable接口 * */ import java.util.Iterator; import java.util.TreeSe…
1.java代码里进行排序(若sql能排序,就不要用代码排序) 可以继承Comparable接口来实现,若是在类中,可以声明Comparator对象,来进行比较 List<Map> maps=new ArrayList<>(); //对maps进行赋值 Comparator comp = new Comparator() { @Override public int compare(Object o1, Object o2) { Map<String, Object>…
TreeSet是以自然顺序存的数据,例如 Set<Student> students=new TreeSet(); students.add(new Student("111")); students.add(new Student("333")); students.add(new Student("222")); for (Student student : students) { System.out.println(stude…
public class TreeMapTest { public static void main(String[] args) { Map<Student,Integer> students = new TreeMap<>(); students.put(new Student("11"),1); students.put(new Student("11"),1); System.out.println(students.size());…
Effective Java --Comparable接口 compareTo方法是Comparable接口的唯一方法.类实现了Comparable接口,表明它的实例具有内在的排序关系. 自己实现compareTo方法应该注意的点: 自反性: x.compareTo(x) == 0 可传递性: x.compareTo(y)>0 && y.compareTo(z)>0 ==> x.compareTo(z) > 0 可逆性: x.compareTo(y) == -y.c…
package com.bjpowernode.t01list; import java.util.ArrayList; /* * 向集合中添加自定义类型 */public class TestList04 { public static void main(String[] args) { ArrayList list = new ArrayList(); Student s1 = new Student("1001","jack",19); Student s2…
Swing自定义JScrollPane的滚动条设置,重写BasicScrollBarUI方法 摘自:https://blog.csdn.net/qq_31635851/article/details/80986870 1.自定义BasicScrollBarUI类 import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Dimension; import java.awt.GradientPaint; impor…
假设现在有一个学生类 class Student { int age; public Student(int age) { this.age = age; } } 要使学生类之间能进行比较,实现System.IComparable接口的CompareTo方法 class Student : System.IComparable { int age; public Student(int age) { this.age = age; } public int CompareTo(object ob…