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 对象数组中的重复元素的更多相关文章

  1. 去掉有序数组中的重复元素 c/c++

    去掉有序数组中的重复元素: int RemoveDuplates(int A[], int nCnt) { ; ; , j = ; i < nCnt && j < nCnt ...

  2. Java-Runoob-高级教程-实例-数组:10. Java 实例 – 查找数组中的重复元素-un

    ylbtech-Java-Runoob-高级教程-实例-数组:10. Java 实例 – 查找数组中的重复元素 1.返回顶部 1. Java 实例 - 查找数组中的重复元素  Java 实例 以下实例 ...

  3. 【leetcode-82,83,26,80】 删除排序链表/数组中的重复元素

    83. 删除排序链表中的重复元素 (1 pass) 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: ...

  4. LeetCode#26 | Remove Duplicates from Sorted Array 删除有序数组中的重复元素

    一.题目 Description Given a sorted array, remove the duplicates in-place such that each element appear ...

  5. 获取JS数组中所有重复元素

    //获取数组内所有重复元素,并以数组返回 //例:入参数组['1','2','4','7','1','2','2'] 返回数组:['1','2'] function GetRepeatFwxmmc(a ...

  6. 26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)

      Given a sorted array, remove the duplicates in-place such that each element appear only once and r ...

  7. 计蒜客--移除数组中的重复元素 (set)

    给定一个升序排列的数组,去掉重复的数,并输出新的数组的长度. 例如:数组 A = \{1, 1, 2\}A={1,1,2},你的程序应该输出 22 即新数组的长度,新数组为 \{1, 2\}{1,2} ...

  8. JSK 11: 移除数组中的重复元素

    题目描述 给定一个升序排列的数组,去掉重复的数,并输出新的数组的长度. 例如:数组 $A = \{1, 1, 2\}$,你的程序应该输出 $2$ 即新数组的长度,新数组为 $\{1, 2\}$. 要求 ...

  9. 74 使用BitSet输出数组中的重复元素

    [本文链接] http://www.cnblogs.com/hellogiser/p/using-bitset-to-print-duplicate-elements-of-array.html [题 ...

随机推荐

  1. xgboost&lightgbm调参指南

    本文重点阐述了xgboost和lightgbm的主要参数和调参技巧,其理论部分可见集成学习,以下内容主要来自xgboost和LightGBM的官方文档. xgboost Xgboost参数主要分为三大 ...

  2. Access viewchild from another component

    https://stackoverflow.com/questions/50935728/access-viewchild-from-another-component =============== ...

  3. 微雪的stm32学习资料

    http://www.waveshare.net/wiki/Main_Page里面有很多资料 STM32开发软件 目录 编译软件 Keil MDKSTM32CubeMX 下载软件 STM32 ISP ...

  4. 分布式协调框架_Zookeeper

    Zookeeper 如今在分布式架构中应用十分广泛,它作为分布式协调框架在分布式架构中有着举足轻重的地位,本文是主要从以上几个方面对 Zookeeper 常用的知识进行总结. 一 从集中式到分布式架构 ...

  5. CMake编译OpenCV

    使用CMake来编译OpenCV,以匹配自己使用的VS版本. 主要有两步: CMake编译OpenCV源码得到OpenCV.sln工程文件. VS编译OpenCV.sln. 以最新的cmake-3.1 ...

  6. hdu 5073 Galaxy 数学 铜牌题

    0.5 题意:有n(n<=5e4)个质点位于一维直线上,现在你可以任意移动其中k个质点,且移动到任意位置,设移动后的中心为e,求最小的I=(x[1]-e)^2+(x[2]-e)^2+(x[3]- ...

  7. windbg双机调试配置[转]

    原文 windbg已不提供单独下载,wdk驱动开发工具包里附带有这个调试器.官网提供下载: http://msdn.microsoft.com/en-us/windows/hardware/gg487 ...

  8. WM_PAINT(父子窗口间)

    WM_PAINT(父子窗口间) 窗口句柄(HWND)都是由操作系统内核管理的,系统内部有一个z-order序列,记录着当前从屏幕底部(假象的从屏幕到眼睛的方向),到屏幕最高层的一个窗口句柄的排序,这个 ...

  9. 洛谷——P2058 海港

    题目传送 由于于题目保证输入的ti是递增的,所以发现当我们统计完一艘船的答案后,这个答案多少会对下一艘船的答案有贡献.同时还发现如果对每个艘船都记录他的乘客在整个数据出现的所有国籍中相应出现的次数,在 ...

  10. 理解TCP三次握手和四次挥手

    TCP相关知识 TCP是面向连接的传输层协议,它提供可靠交付的.全双工的.面向字节流的点对点服务.HTTP协议便是基于TCP协议实现的.(虽然作为应用层协议,HTTP协议并没有明确要求必须使用TCP协 ...