zoukankan      html  css  js  c++  java
  • 【转】SortedSet实现过滤重复字符串并排序

     参考了:http://www.lurenjia.net/article.asp?id=233http://csourcesearch.net/java/fidAB55053EB2EBD58B30F9B28C706AAF40861E637C.aspx?s=mdef%3Ainsert

    一个同事在项目过程中要实现过滤字符串并排序的功能,比如输入:

    111-1111
    131-1111
    111-1111
    131-1111
    121-1221
    111-1111
    111-1111
    输出:
    111-1111
    121-1221
    131-1111

    我想过滤重复的话,可以用Set,因为集合是不允许重复元素的,至于排序,Collections的sort方法可以实现List的排序,以下由方法二实现,其中removeIterance用来过滤重复元素,sortStringList用来排序。

    后 来在查Java Collection API的时候,看到有个SortedSet的接口可以一步实现两个功能,以下由方法一实现,但是TreeSet 是非同步的,应该使用 Collections.synchronizedSet方法进行“包装”。此操作最好在创建时进行,以防止对 set 的意外非同步访问,即:
    SortedSet set = Collections.synchronizedSortedSet(new TreeSet(…));

    实现代码:

    程序代码 程序代码


    Code
  • 相关阅读:
    Java中数据结构对应mysql数据类型
    pom.xml设置字符编码
    java.lang.IllegalStateException: Service id not legal hostname (/test-gw-aqa)
    org.springframework.context.ApplicationContextException: Unable to start web server; nested exceptio
    nacos的三种部署方式
    o.s.c.a.n.c.NacosPropertySourceBuilder : get data from Nacos error,dataId:application-dev.yaml
    java使用split注意事项
    《非暴力沟通》之读书心得
    js存储token
    SpringCloudGateWay之网关跨域问题解决
  • 原文地址:https://www.cnblogs.com/fzzl/p/1427344.html
Copyright © 2011-2022 走看看