henterji's blog

    <<  测试blog价值(娱乐) | 首页 | 计算Java日期  >>
  • 2006-08-01

    Remove Duplicates from a List

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://henterji.blogbus.com/logs/2951575.html

    the best way to remove duplicates from a List in Java. This isn’t too tough to code up yourself (and there are a lot of examples on the net), but Java’s Collection classes actually make this a pretty easy task. Just add all the items in the list to a LinkedHashSet. Because it’s a set, duplicates won’t be added to the collection. Because it’s backed by a linked list, item order will be preserved. And because it uses a hashmap, checking to see if an item is already in the list should be pretty quick (near constant time).

    Here’s the code:
    public List removeDuplicates(List items) {
        Set set = new LinkedHashSet();
        set.addAll(items);
        return new ArrayList(set);
    }

    随机文章:

    Java swing tutorial Website 2006-09-29
    How to encode Enums? 2006-08-08
    计算Java日期 2006-08-02
    Swing and Roundabouts 4: Grid Bag Grease 2006-07-28
    Minding the Queue: Java 1.5 Adds a New Data Structure Interface 2006-07-22

    收藏到:Del.icio.us




    Tag:Java
    引用地址:
    henterji 发表于09:38:11 | 编辑 | 继续话题 | 转发 | 分享 0

个人资料

henterji

日历

搜索

管理

  • 进入后台 写新日志
  • 文章管理 评论管理
  • 更换模板 访问统计

文章分类

    Tag

    • Java[143]
    • GUI[70]
    • eclipse[10]
    • Windows[10]
    • ENGLISH[7]
    More..

    最新文章

    • Java swing tutorial Website
    • 在线Web IM 工具列表
    • 如何获得全文文献
    • Ubuntu安装后root的密码是什么?
    • Building cheat sheets in Eclipse V3.2
    • How to encode Enums?
    • 计算Java日期
    • Remove Duplicates from a List
    • 测试blog价值(娱乐)
    • Swing and Roundabouts 4: Grid Bag Grease
    全部日志>>

    最新评论

    • 兜兜:这个程序最后的结果是不是动画显示的,不是的话!能进一步做...
    • 梁达劲:我看得不是很明白,请问root的密码的多少??...
    • a dating info:Nice world <a href="...
    • j dating info:Nice world <a href="...
    • i dating info:Nice world <a href="...
    • h dating info:My favorites <a href=&quo...
    • kekee:佩服
    • pop:very good!!
    • ksdmaj kmsghwoxc:cldrp zgrb evmxh lnfvk mwpck...
    • xsqkyjg zmveuwa:qgnpeoyiu rtqnscmp qnxayh ur...

    链接

      存档

      • 2006 [181]
      • 2005 [190]
      自定义HTML代码区域
      • 访问统计:
      • RSS 什么是RSS?
        用IM提醒我内容更新
        订阅到QQ邮箱
        订阅到鲜果阅读器
        订阅到Google阅读器
        订阅到抓虾阅读器
      • 《城客》第四期:创意之城
        博客大巴
        博客大巴使用指南
        博客大巴模板中心
        免费注册博客大巴
        一键博客搬家工具
        中文互动杂志城客
      Copyright © 2002-2009 BlogBus.com, All Rights Reserved. 博客大巴 版权所有
      博客大巴模板设计:新空气之旅 | 作者:enid | 素材来源:香必飘新空气之旅