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

需要排序的对象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. 测试使用API

    https://api.github.com/users/github 返回值中的某些URL也可以作为测试API使用

  2. MongoDB 分片键分类与数据分发

    In sharded clusters, if you do not use the _id field as the shard key, then your application must en ...

  3. web+文件夹上传

    一. 大文件上传基础描述: 各种WEB框架中,对于浏览器上传文件的请求,都有自己的处理对象负责对Http MultiPart协议内容进行解析,并供开发人员调用请求的表单内容. 比如: Spring 框 ...

  4. ROStopic 通信方式

    操作演示,对 topic 通信方式的理解请看:点击打开链接 开启终端,运行 roscore 新开一个终端(已经安装好(ros-<distro>-ros-tutorials 包,否则运行  ...

  5. Invalid JDK version in profile 'doclint-java8-disable': Unbounded range: [1.8, for project com.google.code.gson:gson 解决办法

    利用maven打包的时候遇到这个问题 在git上发现一个解决方案 问题解决

  6. 小程序 之嵌套循环修改index与item

    <block wx:for="{{menu}}" wx:key="index"> <view class="cate-item {{ ...

  7. 走进JavaWeb技术世界11:单元测试框架Junit

    JUnit你不知道的那些事儿 转自 老刘 码农翻身 2016-02-24 话说有一次Eric Gamma 坐飞机的时候偶遇Kent Beck(对,就是极限编程和TDD的发起人) ,  两位大牛见面寒暄 ...

  8. TCP和UDP并实现socket的简单通信

    http://www.cnblogs.com/IPrograming/archive/2012/10/15/CSharp_Socket_4.html http://www.cnblogs.com/do ...

  9. OpenJudge计算概论-奇数求和

    /*=================================================== 奇数求和 总时间限制: 1000ms 内存限制: 65536kB 描述 计算非负整数 m 到 ...

  10. maven install的时候把源码一起放到仓库

    在pom.xml中加入插件 <build>    <plugins>        <!-- Source attach plugin -->        < ...