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. 集成新版(5.17+)Activiti Modeler与Rest服务

    声明: 此教程适合Activiti 5.17+版本. 本博客所涉及的内容均可在kft-activiti-demo中找到. 在线demo可以访问 http://demo.kafeitu.me:8080/ ...

  2. Jmeter重要组件介绍(一)

    一.常用的取样器 二.常用的逻辑控制器 三.前置处理器 四.后置处理器 五.断言 六.定时器 七.配置元件 八.监听器

  3. SQL条件语句(IF, CASE WHEN, IF NULL)

    1.IF   表达式:IF( expr1 , expr2 , expr3 )   expr1条件,条件为true,则值是expr2 ,false,值就是expr3 SELECT o.id,u.acco ...

  4. Spring MVC全局异常后返回JSON异常数据

    问题: 当前项目是作为手机APP后台支持,使用spring mvc + mybaits + shiro进行开发.后台服务与手机端交互是发送JSON数据.如果后台发生异常,会直接返回异常页面,显示异常内 ...

  5. 02Hibernate基本配置

    Hibernate基本配置 1.引入jar 2.建立项目 3.创建实体类 package com.sqlserver.domain; public class Customer { long cust ...

  6. JavaSE-18 常用工具类

    学习要点 Object类 枚举 包装类 Math类 Random类 字符串处理 日期时间 Object类 1  什么是Object类 Object类存储在java.lang包中,是所有java类(Ob ...

  7. python logging 日志使用

    https://docs.python.org/3/library/logging.html1.日志级别 日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRI ...

  8. MySQL-----用户和授权管理

    用户管理: 创建用户:  create user '用户名'@'用户pc的ip地址(ip可以写精准点的,也可以是网段的,也可以写一个‘’%‘’提所有)' identified(设置密码) by '密码 ...

  9. 第十九节:Scrapy爬虫框架之Middleware文件详解

    # -*- coding: utf-8 -*- # 在这里定义蜘蛛中间件的模型# Define here the models for your spider middleware## See doc ...

  10. LeetCode(59)SPiral Matrix II

    题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...