根据规则去掉List 对象数组中的重复元素
package container.main; import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet; import container.bean.GxyJobEntity; public class RemoveSame {
public static void main(String[] args) {
List<GxyJobEntity> list = new ArrayList<>();
GxyJobEntity gxyJobEntity1 = new GxyJobEntity();
gxyJobEntity1.setUserId("user001");
gxyJobEntity1.setPlanId("plan001");
gxyJobEntity1.setStudentId("stu001"); GxyJobEntity gxyJobEntity2 = new GxyJobEntity();
gxyJobEntity2.setUserId("user002");
gxyJobEntity2.setPlanId("plan002");
gxyJobEntity2.setStudentId("stu002"); GxyJobEntity gxyJobEntity3 = new GxyJobEntity();
gxyJobEntity3.setUserId("user003");
gxyJobEntity3.setPlanId("plan001");
gxyJobEntity3.setStudentId("stu001"); list.add(gxyJobEntity1); list.add(gxyJobEntity2); list.add(gxyJobEntity3); List<GxyJobEntity> list1 = removeDuplicate(list);
System.out.println(list);
System.out.println(list1);
} private static List<GxyJobEntity> removeDuplicate(List<GxyJobEntity> list) {
Set<GxyJobEntity> set = new TreeSet<GxyJobEntity>(new Comparator<GxyJobEntity>() {
public int compare(GxyJobEntity a, GxyJobEntity b) {
System.out.println("a:" + a);
System.out.println("b:" + b); // 字符串则按照asicc码升序排列
if ( a.getStudentId().equals(b.getStudentId()) &&
a.getPlanId().equals(b.getPlanId()) ) {
return 0;
} else {
return 1;
}
}
});
System.out.println("set:" + set.size() + "-->" + set);
set.addAll(list);
System.out.println("set:" + set.size() + "-->" + set);
return new ArrayList<GxyJobEntity>(set);
} }
如果 对象 GxyJobEntity 中两个属性 studentId 和 planId 相等,则保留一个,去掉多余的。
输出:
set:0-->[]
a:GxyJobEntity [jobId=null, studentId=stu001, planId=plan001, userId=user001]
b:GxyJobEntity [jobId=null, studentId=stu001, planId=plan001, userId=user001]
a:GxyJobEntity [jobId=null, studentId=stu002, planId=plan002, userId=user002]
b:GxyJobEntity [jobId=null, studentId=stu001, planId=plan001, userId=user001]
a:GxyJobEntity [jobId=null, studentId=stu001, planId=plan001, userId=user003]
b:GxyJobEntity [jobId=null, studentId=stu001, planId=plan001, userId=user001]
set:2-->[GxyJobEntity [jobId=null, studentId=stu001, planId=plan001, userId=user001], GxyJobEntity [jobId=null, studentId=stu002, planId=plan002, userId=user002]]
[GxyJobEntity [jobId=null, studentId=stu001, planId=plan001, userId=user001], GxyJobEntity [jobId=null, studentId=stu002, planId=plan002, userId=user002], GxyJobEntity [jobId=null, studentId=stu001, planId=plan001, userId=user003]]
[GxyJobEntity [jobId=null, studentId=stu001, planId=plan001, userId=user001], GxyJobEntity [jobId=null, studentId=stu002, planId=plan002, userId=user002]]
根据规则去掉List 对象数组中的重复元素的更多相关文章
- 去掉有序数组中的重复元素 c/c++
去掉有序数组中的重复元素: int RemoveDuplates(int A[], int nCnt) { ; ; , j = ; i < nCnt && j < nCnt ...
- Java-Runoob-高级教程-实例-数组:10. Java 实例 – 查找数组中的重复元素-un
ylbtech-Java-Runoob-高级教程-实例-数组:10. Java 实例 – 查找数组中的重复元素 1.返回顶部 1. Java 实例 - 查找数组中的重复元素 Java 实例 以下实例 ...
- 【leetcode-82,83,26,80】 删除排序链表/数组中的重复元素
83. 删除排序链表中的重复元素 (1 pass) 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: ...
- LeetCode#26 | Remove Duplicates from Sorted Array 删除有序数组中的重复元素
一.题目 Description Given a sorted array, remove the duplicates in-place such that each element appear ...
- 获取JS数组中所有重复元素
//获取数组内所有重复元素,并以数组返回 //例:入参数组['1','2','4','7','1','2','2'] 返回数组:['1','2'] function GetRepeatFwxmmc(a ...
- 26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)
Given a sorted array, remove the duplicates in-place such that each element appear only once and r ...
- 计蒜客--移除数组中的重复元素 (set)
给定一个升序排列的数组,去掉重复的数,并输出新的数组的长度. 例如:数组 A = \{1, 1, 2\}A={1,1,2},你的程序应该输出 22 即新数组的长度,新数组为 \{1, 2\}{1,2} ...
- JSK 11: 移除数组中的重复元素
题目描述 给定一个升序排列的数组,去掉重复的数,并输出新的数组的长度. 例如:数组 $A = \{1, 1, 2\}$,你的程序应该输出 $2$ 即新数组的长度,新数组为 $\{1, 2\}$. 要求 ...
- 74 使用BitSet输出数组中的重复元素
[本文链接] http://www.cnblogs.com/hellogiser/p/using-bitset-to-print-duplicate-elements-of-array.html [题 ...
随机推荐
- JS继承的原理、方式和应用
概要: 一.继承的原理 二.继承的几种方式 三.继承的应用场景 什么是继承? 继承:子类可以使用父类的所有功能,并且对这些功能进行扩展.继承的过程,就是从一般到特殊的过程.要了解JS继承必须首先要了解 ...
- HTML禁用一块区域点击
style="pointer-events: none;" 此方法可以禁止鼠标点击指定区域,但是对于键盘事件无法屏蔽,最好禁用一下键盘事件,如:tab
- python+Appium自动化:toast定位
Toast简介 Toast是一种简易的消息提示框. 当视图显示给用户,在应用程序中显示为浮动.和Dialog不一样的是,它永远不会获得焦点,无法被点击. 用户将可能是在中间键入别的东西.Toast类的 ...
- Kubernetes1.16下部署Prometheus+node-exporter+Grafana+AlertManager 监控系统
Prometheus 持久化安装 我们prometheus采用nfs挂载方式来存储数据,同时使用configMap管理配置文件.并且我们将所有的prometheus存储在kube-system #建议 ...
- Aizu - 1382 Black or White (分段决策,单调队列优化dp)
题意:给定只有黑白两种颜色的序列A,B,每次可以选择一段连续的长度不超过k的区间将其染成同一种颜色,求把序列A变成B所需的最小操作次数. 首先需要找出一些最优解的特征: 1.如果序列A的第一个颜色和B ...
- nodejs解析url参数的三种方法
要解析的url:http://127.0.0.1:8090/?name=cpc&age=21 利用js字符串操作函数进行解析 const myserver = require("ht ...
- HDU 6102 - GCDispower | 2017 Multi-University Training Contest 6
个人感觉题解的复杂度很玄,参不透,有没有大佬讲解一下- - /* HDU 6102 - GCDispower [ 数论,树状数组] | 2017 Multi-University Training C ...
- jvm虚拟机笔记<六> 运行期优化
这节我们总结一下JVM运行期的优化问题. https://www.cnblogs.com/zhouyuqin/p/5224573.html JVM运行期优化 即时编译器(JIT) 编译对象与触发条件 ...
- nginx配置项
try_files location / { try_files $uri $uri/ /index.php?$query_string; } 当用户请求 http://host/instance时, ...
- SQL Server text field里面有换行符的时候copy到excel数据会散乱
解决方法: select '"'+convert(varchar(8000),testField)+'"'astestField from testTable 这样虽然结果集里面有 ...