【DataStructure】Charming usage of Set in the java
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.
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import java.util.TreeSet;
- public class SetUtil
- {
- public static List<String> testStrList = Arrays.asList("Apple", "Orange",
- "Pair", "Grape", "Banana", "Apple", "Orange");
- /**
- * Gets sorted sets which contains no duplicate elements
- *
- * @time Jul 17, 2014 7:58:16 PM
- * @return void
- */
- public static void sort()
- {
- Set<String> sortSet = new TreeSet<String>(testStrList);
- System.out.println(sortSet);
- // output : [Apple, Banana, Grape, Orange, Pair]
- }
- public static void removeDuplicate()
- {
- Set<String> uniqueSet = new HashSet<String>(testStrList);
- System.out.println(uniqueSet);
- <span style="font-family: Arial, Helvetica, sans-serif;">// output : </span><span style="font-family: Arial, Helvetica, sans-serif;">[Pair, Apple, Banana, Orange, Grape]</span>
- }
- public static void reverse()
- {
- Set<String> sortSet = new TreeSet<String>(testStrList);
- List<String> sortList = new ArrayList<String>(sortSet);
- Collections.reverse(sortList);
- System.out.println(sortList);
- // output : [Pair, Orange, Grape, Banana, Apple]
- }
- public static void swap()
- {
- Set<String> sortSet = new TreeSet<String>(testStrList);
- List<String> sortList = new ArrayList<String>(sortSet);
- Collections.swap(sortList, 0, sortList.size() - 1);
- System.out.println(sortList);
- output : [Apple, Orange, Grape, Banana, Pair]
- }
- public static void main(String[] args)
- {
- SetUtil.sort();
- SetUtil.reverse();
- SetUtil.swap();
- SetUtil.removeDuplicate();
- }
- }
【DataStructure】Charming usage of Set in the java的更多相关文章
- 如何用javac 和java 编译运行整个Java工程 (转载)【转】在Linux下编译与执行Java程序
如何用javac 和java 编译运行整个Java工程 (转载) http://blog.csdn.net/huagong_adu/article/details/6929817 [转]在Linux ...
- 【DataStructure】Description and usage of queue
[Description] A queue is a collection that implements the first-in-first-out protocal. This means th ...
- 【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 ...
- 【DataStructure】Description and Introduction of Tree
[Description] At ree is a nonlinear data structure that models a hierarchical organization. The char ...
- 【算法】哈希表的诞生(Java)
参考资料 <算法(java)> — — Robert Sedgewick, Kevin Wayne <数据结构> ...
- 【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 ...
- 【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 { ...
- 【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. ...
- 【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 ...
随机推荐
- java web 学习笔记 - tomcat数据源
1. 数据库源 以前的JDBC连接步骤为: 1.加载数据库驱动 2.通过DriverManger获取数据库连接connection 3.通过connection执行prepareStatement的响 ...
- redis 其他特性
1.消息订阅与发布 subscribe my1 订阅频道 psubscribe my1* 批量订阅频道,订阅以my1开头的所有频道 publish my1 hello 在指定频道中发布消息,返回值为接 ...
- C++中何时使用引用
使用引用参数的原因: 程序员能够修改调用函数中的数据对象 通过传递引用而不是整个数据对象,可以提高程序的运行速度. 当数据对象较大时(如结构和类对象),第二个原因最重要,这些也是使用指针参数的原因.这 ...
- install docker on centos7
copy from:https://www.youtube.com/watch?v=pm55BUwQ0iE # Prerequisites - Kernel must be 3.10 at minim ...
- react-native 框架升级 安卓第三方插件报错 Android resource linking failed
亲自经历react-native从0.55升级到0.58的过程,有点坎坷,ios出现的问题还算不多,但是android这里,随着gradle和buildTool的使用升级,导致第三方插件出现各种问题, ...
- [USACO12MAR] 摩天大楼里的奶牛 Cows in a Skyscraper
题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...
- virtualBox+centos使用mount -t vboxsf挂载
1.先确保virtualBox安装目录下有对应的文件VBoxGuestAdditions.iso 2.点击设备下的“安装增强功能”,之后再centos可视化界面一步一步点击即可 3.virtualBo ...
- MySQL Connector/Python 接口 (三)
本文参见这里. 使用缓冲的 cursor,下例给从2000年加入公司并且还在公司的员工薪水从明天起加15% from __future__ import print_function from dec ...
- AD采集精度中的LSB
测量范围+5V, 精度10位,LSB=0.0048 精度16位,LSB=0.000076951 测量范围+-5V, 精度10位,LSB=0.009765625,大约为0.01 精度16位,LSB=0. ...
- PowerShell Tools for Visual Studio 2015
首先要去下载Visual Studio 2015 RC 版本 https://www.visualstudio.com/en-us/downloads/visual-studio-2015-downl ...