In an attempt to remove duplicate elements from list, I go to the lengths to take advantage of  methods in the java api. After investiagting the document of java api, the result is so satisfying that I speak hightly of wisdom of developer of java language.Next
I will introduce charming usage about set in the java.

[java] view
plain
copy

  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Set;
  7. import java.util.TreeSet;
  8. public class SetUtil
  9. {
  10. public static List<String> testStrList = Arrays.asList("Apple", "Orange",
  11. "Pair", "Grape", "Banana", "Apple", "Orange");
  12. /**
  13. * Gets sorted sets which contains no duplicate elements
  14. *
  15. * @time Jul 17, 2014 7:58:16 PM
  16. * @return void
  17. */
  18. public static void sort()
  19. {
  20. Set<String> sortSet = new TreeSet<String>(testStrList);
  21. System.out.println(sortSet);
  22. // output : [Apple, Banana, Grape, Orange, Pair]
  23. }
  24. public static void removeDuplicate()
  25. {
  26. Set<String> uniqueSet = new HashSet<String>(testStrList);
  27. System.out.println(uniqueSet);
  28. <span style="font-family: Arial, Helvetica, sans-serif;">// output : </span><span style="font-family: Arial, Helvetica, sans-serif;">[Pair, Apple, Banana, Orange, Grape]</span>
  29. }
  30. public static void reverse()
  31. {
  32. Set<String> sortSet = new TreeSet<String>(testStrList);
  33. List<String> sortList = new ArrayList<String>(sortSet);
  34. Collections.reverse(sortList);
  35. System.out.println(sortList);
  36. // output : [Pair, Orange, Grape, Banana, Apple]
  37. }
  38. public static void swap()
  39. {
  40. Set<String> sortSet = new TreeSet<String>(testStrList);
  41. List<String> sortList = new ArrayList<String>(sortSet);
  42. Collections.swap(sortList, 0, sortList.size() - 1);
  43. System.out.println(sortList);
  44. output : [Apple, Orange, Grape, Banana, Pair]
  45. }
  46. public static void main(String[] args)
  47. {
  48. SetUtil.sort();
  49. SetUtil.reverse();
  50. SetUtil.swap();
  51. SetUtil.removeDuplicate();
  52. }
  53. }

【DataStructure】Charming usage of Set in the java的更多相关文章

  1. 如何用javac 和java 编译运行整个Java工程 (转载)【转】在Linux下编译与执行Java程序

    如何用javac 和java 编译运行整个Java工程 (转载)  http://blog.csdn.net/huagong_adu/article/details/6929817 [转]在Linux ...

  2. 【DataStructure】Description and usage of queue

    [Description] A queue is a collection that implements the first-in-first-out protocal. This means th ...

  3. 【DataStructure】One of queue usage: Simulation System

    Statements: This blog was written by me, but most of content  is quoted from book[Data Structure wit ...

  4. 【DataStructure】Description and Introduction of Tree

    [Description] At ree is a nonlinear data structure that models a hierarchical organization. The char ...

  5. 【算法】哈希表的诞生(Java)

    参考资料 <算法(java)>                           — — Robert Sedgewick, Kevin Wayne <数据结构>       ...

  6. 【DateStructure】 Charnming usages of Map collection in Java

    When learning the usage of map collection in java, I found serveral beneficial methods that was enco ...

  7. 【DataStructure】Some useful methods about linkedList(二)

    Method 1: Add one list into the other list. For example, if list1is {22, 33, 44, 55} and  list2 is { ...

  8. 【DataStructure】The description of Java Collections Framework

    The Java Connections FrameWork is a group of class or method and interfacs in the java.util package. ...

  9. 【DataStructure】Some useful methods about linkedList(三)

    Method 4: Gets the value of element number i For example, if list is {22, 33, 44, 55, 66, 77, 88, 99 ...

随机推荐

  1. CentOS 7 配置本地yum 源

    1. 加载 CentOS的ISO镜像并挂载: [root@localhost files]# mount /media/files/CentOS-7-x86_64-DVD-1611.iso /mnt/ ...

  2. chown - 修改文件所有者和组别

    总览 chown [options] user [:group] file... POSIX 选项: [-R] GNU 选项(最短格式): [-cfhvR] [--dereference] [--re ...

  3. sqlalchemy ORM进阶- 批量插入数据

    参考: https://www.jb51.net/article/49789.htm https://blog.csdn.net/littlely_ll/article/details/8270687 ...

  4. HTTP的缺点与HTTPS

    a.通信使用明文不加密,内容可能被窃听 b.不验证通信方身份,可能遭到伪装 c.无法验证报文完整性,可能被篡改 HTTPS就是HTTP加上加密处理(一般是SSL安全通信线路)+认证+完整性保护

  5. shell 循环 read line

    cat dockerlist |while read line;do docker rmi  $line ;done

  6. mysql安装及navicat连接

    1.下载mysql官方连接:https://dev.mysql.com/downloads/mysql/ 下载成功后,解压到自己想要的路径下并创建my.ini文件和配置环境变量 然后我们在根目录下创建 ...

  7. 安装配置elasticsearch、安装elasticsearch-analysis-ik插件、mysql导入数据到elasticsearch、安装yii2-elasticsearch及使用

    一.安装elasticsearch 获取elasticsearch的rpm:wget https://download.elastic.co/elasticsearch/release/org/ela ...

  8. Python面向对象之类属性类方法静态方法

    类的结构 实例 使用面向对象开发时,第一步是设计类: 当使用 类名() 创建对象时,会自动执行以下操作: 1.为对象在内存中分配空间--创建对象: 2.为对象的属性 设置初始值--初始化方法(init ...

  9. poj2325 大数除法+贪心

    将输入的大数除以9 无法整除再除以 8,7,6,..2,如果可以整除就将除数记录,将商作为除数继续除9,8,...,3,2. 最后如果商为1 证明可以除尽 将被除过的数从小到大输出即可 #includ ...

  10. 【03】placeholder

    placeholder   表单占位符解决方案 Css Code :-moz-placeholder, ::-webkit-input-placeholder{ color: #bfbfbf; } . ...