1. package com.test;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.List;
  6. /**
  7. *
  8. * 找出两个list中不同的元素
  9. * @author leiwei 2011-12-14
  10. *
  11. */
  12. public class NoEqualsElement {
  13. public static void main(String[] args) {
  14. List<String> list1 = new ArrayList<String>();
  15. List<String> list2 = new ArrayList<String>();
  16. list1.add("1");
  17. list1.add("3");
  18. list1.add("8");
  19. list1.add("9");
  20. list2.add("2");
  21. list2.add("3");
  22. list2.add("7");
  23. for(String s2:list1) {
  24. boolean flag = false;
  25. for(String s1:list2) {
  26. if(s2.equals(s1)) {
  27. flag = true;
  28. break;
  29. }
  30. }
  31. if(!flag){
  32. System.out.println(s2);
  33. }
  34. }
  35. }
  36. }
  1. package com.test;
  2. import java.util.ArrayList;
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.Set;
  7. import com.model.Student;
  8. /**
  9. * 重写对象Student的equals方法
  10. * 只要age相等 对象就相等
  11. *
  12. * 删除两个集合中 相同的对象只保留一个
  13. * @author leiwei 2011-12-19
  14. *
  15. */
  16. public class ListOBJ {
  17. public static void main(String[] args) {
  18. Set<Student> setStudent = new HashSet<Student>();
  19. List<Student> list1 = new ArrayList<Student>();
  20. List<Student> list2 = new ArrayList<Student>();
  21. Student s1 = new Student();
  22. s1.setAge("10");
  23. list1.add(s1);
  24. Student s2 = new Student();
  25. s2.setAge("20");
  26. list1.add(s2);
  27. Student s3 = new Student();
  28. s3.setAge("20");
  29. list2.add(s3);
  30. Student s4 = new Student();
  31. s4.setAge("30");
  32. list2.add(s4);
  33. /**
  34. * 我们知道set集合,中的元素不会重复
  35. *
  36. * 将连个对象集合放进去,自然就会排出重复的
  37. *
  38. * 对象(前提Student 对象重写了equals方法)
  39. */
  40. setStudent.addAll(list1);
  41. setStudent.addAll(list2);
  42. //输出 测试看是否 排出成功
  43. System.out.println(setStudent.size());
  44. Iterator<Student> iterator = setStudent.iterator();
  45. while (iterator.hasNext()) {
  46. Student s = iterator.next();
  47. System.out.println(s.getAge());
  48. }
  49. }
  50. }
  1. package com.model;
  2. public class Student {
  3. private String age;
  4. public String getAge() {
  5. return age;
  6. }
  7. public void setAge(String age) {
  8. this.age = age;
  9. }
  10. //重写equals方法只要age相等,我们就认为对象两个相等
  11. @Override
  12. public boolean equals(Object obj) {
  13. if(obj instanceof Student){
  14. Student st=(Student) obj;
  15. return (age.equals(st.age));
  16. }else{
  17. return super.equals(obj);
  18. }
  19. }
  20. @Override
  21. public int hashCode() {
  22. return age.hashCode();
  23. }
  24. }

转自:http://blog.csdn.net/goodleiwei/article/details/7083199

找出list中的不同元素、删除两个list中相同的对象的更多相关文章

  1. c#封装DBHelper类 c# 图片加水印 (摘)C#生成随机数的三种方法 使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象 c# 制作正方形图片 JavaScript 事件循环及异步原理(完全指北)

    c#封装DBHelper类   public enum EffentNextType { /// <summary> /// 对其他语句无任何影响 /// </summary> ...

  2. java:在Conllection接口中实际上也规定了两个可以将集合变成对象数组的操作

    在Conllection接口中实际上也规定了两个可以将集合变成对象数组的操作 //在Conllection接口中实际上也规定了两个可以将集合变成对象数组的操作 List<String> a ...

  3. majority element(数组中找出出现次数最多的元素)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  4. 笔试算法题(24):找出出现次数超过一半的元素 & 二叉树最近公共父节点

    出题:数组中有一个数字出现的次数超过了数组长度的一半,请找出这个数字: 分析: 解法1:首先对数组进行排序,时间复杂度为O(NlogN),由于有一个数字出现次数超过了数组的一半,所以如果二分数组的话, ...

  5. 算法练习之x的平方根,爬楼梯,删除排序链表中的重复元素, 合并两个有序数组

    1.x的平方根 java (1)直接使用函数 class Solution { public int mySqrt(int x) { int rs = 0; rs = (int)Math.sqrt(x ...

  6. 使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象

    本文需要对C#里的LINQ.Lambda 表达式 .委托有一定了解. 在工作中,经常遇到需要对比两个集合的场景,如: 页面集合数据修改,需要保存到数据库 全量同步上游数据到本系统数据库 在这些场景中, ...

  7. 算法-找出与目标数字相同的digit组成的整数中比该数字大的数集中的最小数字

    题目: 给出1个正整数,找到用与这个数字相同的digit组成的整数中比这个数字大的数集中的最小数字.比如:12352874 的结果是 12354278 分析: 这道题目的考虑目标是数组的查找与排序. ...

  8. day6、Linux下如何找出7天以前的文件删除

    有些时候,由于系统产生的日志文件,使服务器的磁盘空间紧张,所以怎么删除7天以前的日志文件及让系统只保留7天以内的日志文件 方法一 使用命令:find + |xargs + ls 命令方法:find / ...

  9. EF Core中Fluent Api如何删除指定数据表中的行

    这两天一直在研究在code first下如何删除数据表中的指定行,于是开始搜狗,后来百度,压根就找不到资料,后来一想可能我的搜索关键字有问题,而且ef core命令与ef的命令差不多,于是从这两个方面 ...

随机推荐

  1. Convolutional Neural Networks for Visual Recognition 7

    Two Simple Examples softmax classifier 后,我们介绍两个简单的例子,一个是线性分类器,一个是神经网络.由于网上的讲义给出的都是代码,我们这里用公式来进行推导.首先 ...

  2. C++之this指针与另一种“多态”

    一.引入 定义一个类的对象,首先系统已经给这个对象分配了空间,然后会调用构造函数(说明:假设存在构造函数--2010.9.5修正). 一个类有多个对象,当程序中调用对象的某个函数时,有可能要访问到这个 ...

  3. SM234

    2017-2018-2 20179212 <网络攻防> 作业 本次实验课由王孟亚.李栋我们三个共同完成,我主要负责SM3的研究和Python实现. SM3的工作原理 SM3密码杂凑算法采用 ...

  4. 20179215《Linux内核原理与分析》第一周作业

    一.Linux介绍 我们现在很常见Windows系统,对于Linux则显得尤为陌生.当然我也不例外,初识Linux过程中遇到一些困惑,但我也在实验的同时通过不断查找资料与实践中慢慢解决问题.那么下面我 ...

  5. AtCoder Grand Contest 015 题解

    A - A+...+B Problem 常识 Problem Statement Snuke has N integers. Among them, the smallest is A, and th ...

  6. 【LeetCode】018 4Sum

    题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  7. 51nod 1450 闯关游戏——期望dp

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1450 想了半天,不知道不能走的状态(即最后不足m个的状态)怎么办. ...

  8. Ajax知识点整理

    一.javascript原生Ajax 1.简介 Ajax是Asynchronous JavaScript+XML(异步JavaScript和XML)的缩写. 该名称诞生于XML还是数据传输首选格式的时 ...

  9. SQL中replace函数

    string sql1 = "select price from dbo.eazy_farm where REPLACE(title,' ','')='" + cainame + ...

  10. SpringMVC执行流程简介

    1.用户向服务器发送请求,请求被SpringMVC的前端控制器DispatcherServlet截获. 2.DispatcherServlet对请求的URL(统一资源定位符)进行解析,得到URI(请求 ...