http://blog.csdn.net/huaishuming/article/details/47778319

1. 单个List 去重:

如果用的是Set集合就不用怕重复的问题了,如果用的List就要想办法将它变为Set

  1. package com;
  2. import java.util.ArrayList;
  3. import java.util.HashSet;
  4. import java.util.List;
  5. public class Test4 {
  6. /**
  7. * @param args
  8. */
  9. public static void main(String[] args) {
  10. // TODO Auto-generated method stub
  11. List<String> l1 = new ArrayList<String>();
  12. l1.add("a");
  13. l1.add("b");
  14. l1.add("c");
  15. l1.add("e");
  16. l1.add("e");
  17. l1.add("e");
  18. l1.add("e");
  19. l1.add("a");
  20. List<String> listWithoutDup = new ArrayList<String>(new HashSet<String>(l1));
  21. for(String str : listWithoutDup){
  22. System.out.println(str);
  23. }
  24. }
  25. }

2. 2个集合合并后去重:

  1. package com;
  2. import java.util.ArrayList;
  3. import java.util.HashSet;
  4. import java.util.List;
  5. public class Test3 {
  6. /**
  7. * @param args
  8. */
  9. public static void main(String[] args) {
  10. // TODO Auto-generated method stub
  11. List<String> l1 = new ArrayList<String>();
  12. l1.add("a");
  13. l1.add("a");
  14. l1.add("c");
  15. l1.add("c");
  16. List<String> l2 = new ArrayList<String>();
  17. l2.add("b");
  18. l2.add("b");
  19. l2.add("k");
  20. l2.add("k");
  21. l1.removeAll(l2);//此处指的是将与l2重复的删除
  22. l1.addAll(l2);//此处指加上l2
  23. //如果保证l1,和l2  2个各自的LIST 本身不重复,此行代码不用写。否则会出现合并后LIST重复的问题,具体看业务需要
  24. l1 = new ArrayList<String>(new HashSet<>(l1));
  25. for(String str : l1){
  26. System.out.println(str);
  27. }
  28. }
  29. }

结果:

b
c
a
k

如果没有

  1. l1 = new ArrayList<String>(new HashSet<>(l1));
    1. 则结果为:a
    2. a
    3. c
    4. c
    5. b
    6. b
    7. k
    8. k

list去重 转载的更多相关文章

  1. 使用SimHash进行海量文本去重[转载]

    阅读目录 1. SimHash与传统hash函数的区别 2. SimHash算法思想 3. SimHash流程实现 4. SimHash签名距离计算 5. SimHash存储和索引 6. SimHas ...

  2. C#大数据文本高效去重

    C#大数据文本高效去重 转载请注明出处 http://www.cnblogs.com/Huerye/ TextReader reader = File.OpenText(@"C:\Users ...

  3. 【C#】list 去重(转载)

    Enumerable.Distinct 方法 是常用的LINQ扩展方法,属于System.Linq的Enumerable方法,可用于去除数组.集合中的重复元素,还可以自定义去重的规则. 有两个重载方法 ...

  4. 基于Redis的Bloomfilter去重(转载)

    转载:http://blog.csdn.net/bone_ace/article/details/53107018 前言 “去重”是日常工作中会经常用到的一项技能,在爬虫领域更是常用,并且规模一般都比 ...

  5. C# MVC 用户登录状态判断 【C#】list 去重(转载) js 日期格式转换(转载) C#日期转换(转载) Nullable<System.DateTime>日期格式转换 (转载) Asp.Net MVC中Action跳转(转载)

    C# MVC 用户登录状态判断   来源:https://www.cnblogs.com/cherryzhou/p/4978342.html 在Filters文件夹下添加一个类Authenticati ...

  6. 【转载】C#中通过Distinct方法对List集合进行去重

    在C#的List集合对象中,可以使用Distinct方法来对List集合元素进行去重,如果list集合内部元素为值类型,则Distinct方法根据值类型是否相等来判断去重,如果List集合内部元素为引 ...

  7. [转载]基于Redis的Bloomfilter去重(附Python代码)

    前言: “去重”是日常工作中会经常用到的一项技能,在爬虫领域更是常用,并且规模一般都比较大.去重需要考虑两个点:去重的数据量.去重速度.为了保持较快的去重速度,一般选择在内存中进行去重. 数据量不大时 ...

  8. js 数组去重求和 (转载)

    方法一:js数组id去重,value值相加问题 来源:https://www.jianshu.com/p/8f79e31b46ed // js let arr = [ { id: 1, value: ...

  9. js 数组 去重 算法(转载)

    以下内容可能有重复部分,项目有用上,但还没来得急整理和验证. 一:https://www.cnblogs.com/jiayuexuan/p/7527055.html 1.遍历数组法 它是最简单的数组去 ...

随机推荐

  1. hdu 5562 Clarke and food(贪心)

    Problem Description Clarke is a patient with multiple personality disorder. One day, Clarke turned i ...

  2. hibou 主界面自己侧滑的定义

    要打滑View参加UIPanGestureRecognizer #pragma mark 手势识别器回调方法 - (void)dragView:(UIPanGestureRecognizer *)ge ...

  3. android _scrollview嵌套listview出现高度显示不全解决方案

    只要在工具类里写上这一段代码:/** * scrollview嵌套listview显示不全解决 * @param listView */ public static void setListViewH ...

  4. Java File类读取文件属性

     package myjavademo;import java.io.*; publicclass MyJavaDemo {     public static void main(String[]  ...

  5. 用HtmlLink来改变网站的主题

    #region Theme // 注册样式(将主题样式至于通用样式后面) HtmlLink themeCss = new HtmlLink(); themeCss.Href = GetThemeUrl ...

  6. 【 枚举 Enum 】循环 名称与值

      foreach (WeekDay c in (WeekDay [])Enum.GetValues(typeof(WeekDay ))) {Console.Write(String.Format(& ...

  7. C语言strcmp()函数:比较字符串(区分大小写)

    头文件:#include <string.h> strcmp() 用来比较字符串(区分大小写),其原型为: int strcmp(const char *s1, const char *s ...

  8. linux下socket编程-UDP

    下面是UDP的服务器的代码: /* server.c */ #include <stdio.h> #include <string.h> #include <netine ...

  9. Spring学习之切入点表达式

    链接地址:http://jinnianshilongnian.iteye.com/blog/1415606

  10. PHP发红包程序

    //红包算法$total = 20;   //红包总金额$num = 10;     //红包个数$min = 0.01;   //每个人最少能收到0.01 for ($i=1;$i<$num; ...