根据规则去掉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 [题 ...
随机推荐
- 【Java 关键字this 的使用】还阔以调用重载的构造方法
笔记: /** this 关键字的使用除了调用方法和变量外, * 还可以用来显示 调用当前类的重载的指定的构造方法! * 同时也应该必须放到该方法内部的首行! */ 测试: import java.l ...
- 在java的xml文件配置数据库URL地址时提示The reference to entity "characterEncoding" must end with the ';' delimiter.错误信息
配置数据库的URL<property name="url" value="jdbc:mysql://127.0.0.1:3306/micro_message&quo ...
- MyBatis-05-解决属性名和字段名不一致的问题
5.解决属性名和字段名不一致的问题 1.问题 数据库中的字段 新建一个项目,拷贝之前的,测试实体类字段不一致的情况. public class User { private int id; priva ...
- SARS病毒 (生成函数 + 快速幂)
链接:https://ac.nowcoder.com/acm/contest/992/A来源:牛客网 题目描述 目前,SARS 病毒的研究在世界范围内进行,经科学家研究发现,该病毒及其变种的 DNA ...
- Load store action in vulkan & ogles 的解决方案
metal的带宽之前的blog有讲 这篇主要是vulkan 和ogles的解决方案 https://www.khronos.org/registry/vulkan/specs/1.1-extensio ...
- 关于datagridview控件如何动态改变行数问题
首先要把单元格转换成txt文本框然后根据TextChanged事件来进行修改就可以实现 private void Return_DGV_EditingControlShowing(object sen ...
- [].slice.call(arguments,1) 个人理解
var arr = []; [] == arr; 假设 var arr = [1,2,3,4,5]; 那么 arr.slice(1,2) == [2]; 通过 slice.call 才能使用call显 ...
- python大数据初探--pandas,numpy代码示例
import pandas as pd import numpy as np dates = pd.date_range(',periods=6) dates import pandas as pd ...
- 36. ClustrixDB 使用ClustrixDB加密连接
ClustrixDB使用sha256_password插件支持SSL和身份验证. 一些安全规则要求对存储在数据库中的用户密码进行更强的保护.与默认的mysql_native_password插件相比, ...
- PHP mysqli_get_server_info() 函数
定义和用法 mysqli_get_server_info() 函数返回 MySQL 服务器版本. 语法 mysqli_get_server_info(connection); 实例 返回 MySQL ...