【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 ...
随机推荐
- CentOS 7 配置本地yum 源
1. 加载 CentOS的ISO镜像并挂载: [root@localhost files]# mount /media/files/CentOS-7-x86_64-DVD-1611.iso /mnt/ ...
- chown - 修改文件所有者和组别
总览 chown [options] user [:group] file... POSIX 选项: [-R] GNU 选项(最短格式): [-cfhvR] [--dereference] [--re ...
- sqlalchemy ORM进阶- 批量插入数据
参考: https://www.jb51.net/article/49789.htm https://blog.csdn.net/littlely_ll/article/details/8270687 ...
- HTTP的缺点与HTTPS
a.通信使用明文不加密,内容可能被窃听 b.不验证通信方身份,可能遭到伪装 c.无法验证报文完整性,可能被篡改 HTTPS就是HTTP加上加密处理(一般是SSL安全通信线路)+认证+完整性保护
- shell 循环 read line
cat dockerlist |while read line;do docker rmi $line ;done
- mysql安装及navicat连接
1.下载mysql官方连接:https://dev.mysql.com/downloads/mysql/ 下载成功后,解压到自己想要的路径下并创建my.ini文件和配置环境变量 然后我们在根目录下创建 ...
- 安装配置elasticsearch、安装elasticsearch-analysis-ik插件、mysql导入数据到elasticsearch、安装yii2-elasticsearch及使用
一.安装elasticsearch 获取elasticsearch的rpm:wget https://download.elastic.co/elasticsearch/release/org/ela ...
- Python面向对象之类属性类方法静态方法
类的结构 实例 使用面向对象开发时,第一步是设计类: 当使用 类名() 创建对象时,会自动执行以下操作: 1.为对象在内存中分配空间--创建对象: 2.为对象的属性 设置初始值--初始化方法(init ...
- poj2325 大数除法+贪心
将输入的大数除以9 无法整除再除以 8,7,6,..2,如果可以整除就将除数记录,将商作为除数继续除9,8,...,3,2. 最后如果商为1 证明可以除尽 将被除过的数从小到大输出即可 #includ ...
- 【03】placeholder
placeholder 表单占位符解决方案 Css Code :-moz-placeholder, ::-webkit-input-placeholder{ color: #bfbfbf; } . ...