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. java web 学习笔记 - tomcat数据源

    1. 数据库源 以前的JDBC连接步骤为: 1.加载数据库驱动 2.通过DriverManger获取数据库连接connection 3.通过connection执行prepareStatement的响 ...

  2. redis 其他特性

    1.消息订阅与发布 subscribe my1 订阅频道 psubscribe my1* 批量订阅频道,订阅以my1开头的所有频道 publish my1 hello 在指定频道中发布消息,返回值为接 ...

  3. C++中何时使用引用

    使用引用参数的原因: 程序员能够修改调用函数中的数据对象 通过传递引用而不是整个数据对象,可以提高程序的运行速度. 当数据对象较大时(如结构和类对象),第二个原因最重要,这些也是使用指针参数的原因.这 ...

  4. install docker on centos7

    copy from:https://www.youtube.com/watch?v=pm55BUwQ0iE # Prerequisites - Kernel must be 3.10 at minim ...

  5. react-native 框架升级 安卓第三方插件报错 Android resource linking failed

    亲自经历react-native从0.55升级到0.58的过程,有点坎坷,ios出现的问题还算不多,但是android这里,随着gradle和buildTool的使用升级,导致第三方插件出现各种问题, ...

  6. [USACO12MAR] 摩天大楼里的奶牛 Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...

  7. virtualBox+centos使用mount -t vboxsf挂载

    1.先确保virtualBox安装目录下有对应的文件VBoxGuestAdditions.iso 2.点击设备下的“安装增强功能”,之后再centos可视化界面一步一步点击即可 3.virtualBo ...

  8. MySQL Connector/Python 接口 (三)

    本文参见这里. 使用缓冲的 cursor,下例给从2000年加入公司并且还在公司的员工薪水从明天起加15% from __future__ import print_function from dec ...

  9. AD采集精度中的LSB

    测量范围+5V, 精度10位,LSB=0.0048 精度16位,LSB=0.000076951 测量范围+-5V, 精度10位,LSB=0.009765625,大约为0.01 精度16位,LSB=0. ...

  10. PowerShell Tools for Visual Studio 2015

    首先要去下载Visual Studio 2015 RC 版本 https://www.visualstudio.com/en-us/downloads/visual-studio-2015-downl ...