代码记录(需求:根据店铺等级和店铺到某个点的距离进行排序,其中店铺等级由高到低,距离由近及远)

需要排序的对象Store,Store.java

package com.zhipengs.work.test;

import java.io.Serializable;

/**
* 实体类或DTO
*
* @author zhipengs
*/
public class Store implements Serializable { private static final long serialVersionUID = -1947476757586351017L; private double distance;// 店铺到某个经纬度(点)的距离--距离某个固定的点越近,排序优先级越高
private int sgrade;// 店铺等级--等级越高,排序优先级越高 public Store(double distance, int sgrade) {
super();
this.distance = distance;
this.sgrade = sgrade;
} public double getDistance() {
return distance;
} public void setDistance(double distance) {
this.distance = distance;
} public int getSgrade() {
return sgrade;
} public void setSgrade(int sgrade) {
this.sgrade = sgrade;
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(distance);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + sgrade;
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Store other = (Store) obj;
if (Double.doubleToLongBits(distance) != Double
.doubleToLongBits(other.distance))
return false;
if (sgrade != other.sgrade)
return false;
return true;
} @Override
public String toString() {
return "Store [distance=" + distance + ", sgrade=" + sgrade + "]";
} }

自定义Comparator,StoreComparator.java

package com.zhipengs.work.test;

import java.util.Comparator;

/**
* 自定义StoreComparator,实现Comparator接口,重写compare方法
*
* @author zhipengs
*/
public class StoreComparator implements Comparator<Store> { @Override
public int compare(Store o1, Store o2) {
int ret = 0;
// 店铺等级由高到低
int sg = o2.getSgrade() - o1.getSgrade();
if (sg != 0) {
ret = sg > 0 ? 1 : -1;
} else {
// 店铺距离由近及远
sg = (o1.getDistance() - o2.getDistance()) > 0 ? 1 : -1;
if (sg != 0) {
ret = sg > 0 ? 1 : -1;
}
}
return ret;
} }

测试类Main.java

package com.zhipengs.work.test;

import java.util.Set;
import java.util.TreeSet; /**
* 测试多条件排序TreeSet--Comparator
*
* @author zhipengs
*/
public class Main { public static void main(String[] args) {
// 先用TreeSet按自定义排序规则排序并控制size大小,再转为有序List遍历进行其它操作或处理
Set<Store> storeSet = new TreeSet<Store>(new StoreComparator());
storeSet.add(new Store(1, 0));
storeSet.add(new Store(2, 1));
storeSet.add(new Store(5, 1));
storeSet.add(new Store(9, 2));
storeSet.add(new Store(3, 0));
storeSet.add(new Store(6, 0));
storeSet.add(new Store(4, 1));
storeSet.add(new Store(7, 2));
storeSet.add(new Store(0, 0));
storeSet.add(new Store(8, 1));
int sgrade = -1;
// 打印排序后的结果
for (Store s : storeSet) {
if (sgrade != s.getSgrade() && -1 != sgrade) {
System.out.println("------------------------------");
}
System.out.println(s);
sgrade = s.getSgrade();
}
}
}

测试结果:

Store [distance=7.0, sgrade=2]
Store [distance=9.0, sgrade=2]
------------------------------
Store [distance=2.0, sgrade=1]
Store [distance=4.0, sgrade=1]
Store [distance=5.0, sgrade=1]
Store [distance=8.0, sgrade=1]
------------------------------
Store [distance=0.0, sgrade=0]
Store [distance=1.0, sgrade=0]
Store [distance=3.0, sgrade=0]
Store [distance=6.0, sgrade=0]

使用自定义Comparator对TreeSet中的数据进行多条件排序的更多相关文章

  1. JAVA之旅(二十)—HashSet,自定义存储对象,TreeSet,二叉树,实现Comparator方式排序,TreeSet小练习

    JAVA之旅(二十)-HashSet,自定义存储对象,TreeSet,二叉树,实现Comparator方式排序,TreeSet小练习 我们继续说一下集合框架 Set:元素是无序(存入和取出的顺序不一定 ...

  2. android 在自定义的listview(有刷新加载项)列表中,数据过少时不能铺满整个屏幕时,header和footer同时显示问题

    android  在自定义的listview(有刷新加载项)列表中,数据过少时,当刷新时,加载项也会显示,这是很头疼的一个问题,查阅了一些资料,总结了一个比较不错的方法: 原来代码: @Overrid ...

  3. Springboot中使用自定义参数注解获取 token 中用户数据

    使用自定义参数注解获取 token 中User数据 使用背景 在springboot项目开发中需要从token中获取用户信息时通常的方式要经历几个步骤 拦截器中截获token TokenUtil工具类 ...

  4. SpringBoot整合freemarker中自定义标签获取字典表的数据

    因为在前端要根据字典表中的数据去将1.2这些值转换成对应的文字解释 1.首先要创建一个类去实现 TemplateDirectiveModel 类 @Component public class Dic ...

  5. JDK学习---深入理解Comparator、TreeSet、TreeMap为什么可以排序

    我本来打算仔细的去分析分析TreeSet和TreeMap排序规则,并且从底层实现和数据结构入手.当我去读完底层源码以后,我感觉我就的目标定的太大了,单单就是数据结构就够我自己写很久了,因此我决定先易后 ...

  6. 项目中通过Sorlj获取索引库中的数据

    在开发项目中通过使用Solr所提供的Solrj(java客户端)获取索引库中的数据,这才是真正对项目起实质性作用的功能,提升平台的检索性能及检索结果的精确性 第一步,引入相关依赖的jar包 第二步,根 ...

  7. SortedSet的实现类是TreeSet:它的作用是字为添加到TreeSet中的元素排序。

      SortedSet可自动为元素排序. SortedSet的实现类是TreeSet:它的作用是字为添加到TreeSet中的元素排序.   练习:自定义类用TreeSet排序. 与HashSet不同, ...

  8. geotrellis使用(十六)使用缓冲区分析的方式解决投影变换中边缘数据值计算的问题

    Geotrellis系列文章链接地址http://www.cnblogs.com/shoufengwei/p/5619419.html 目录 前言 问题探索 采样说明 实现方案 总结 一.前言     ...

  9. EF如何操作内存中的数据以及加载相关联表的数据:延迟加载、贪婪加载、显示加载

    之前的EF Code First系列讲了那么多如何配置实体和数据库表的关系,显然配置只是辅助,使用EF操作数据库才是每天开发中都需要用的,这个系列讲讲如何使用EF操作数据库.老版本的EF主要是通过Ob ...

随机推荐

  1. sql server update 的批量更新方法

    假定我们有两张表,一张表为Product表存放产品信息,其中有产品价格列Price:另外一张表是ProductPrice表,我们要将ProductPrice表中的价格字段Price更新为Price表中 ...

  2. sql server newid() 的用法

    sql newid()随机函数   从A表随机取2条记录,用SELECT TOP 10 * FROM ywle order by newid()order by 一般是根据某一字段排序,newid() ...

  3. Dao的扩展

    题目: 1.查询所有学生记录,包含年级名称2.查询S1年级下的学生记录 一.项目目录 package com.myschool.entity; import java.util.ArrayList; ...

  4. MongoDB 查看chunk块大小

    使用mongo shell连到mongos执行命令:AllChunkInfo("dbname.cellname",true) 点击(此处)折叠或打开 AllChunkInfo = ...

  5. 使用webuploader实现大文件分片上传

    文件夹数据库处理逻辑 public class DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject() ...

  6. [Luogu] 教主的魔法

    https://www.luogu.org/problemnew/show/P2801 分块 对于每一块进行排序存储在另一个数组中 二分查询 #include<iostream> #inc ...

  7. java equals 和 == 区别

    equals 可以重写, == 不可重写 equals 是方法 equals 是 Object 的方法. 基本作用:判断两个对象是否为引用 public class Object { // 省略部分代 ...

  8. 前端代码规范-CSS

    CSS规范 一.命名规范BEM(Block Element Modifier) 1.Block name -- 实体名称中的单词之间用连字符分隔(-) HTML <div class=" ...

  9. EXCEL公式中如何表示回车符?

    问题: 将 id credttm cdno cdamt cashbrid cashrole note 转换为 "id  credttm  cdno   cdamt  cashbrid  ca ...

  10. 在IntelliJ IDEA中启动tomcat出现Can't load AMD 64-bit .dll on a IA 32-bit' platform问题详解

    第一查看jdk版本 第二查看IntelliJ IDEA中运行tomcat的配置的jdk 比较两个jdk版本是否一致.或者查看tomcat是64还是32位的