【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 ...
随机推荐
- ubunut在线音乐比方软件
今天安装了一个音乐在线播放软件,忍不住要来赞一下, 之前一直都是用网页在线的qq音乐听的,这样就有点感觉不爽了, 今天突然想起来好像在网上看到的在ubuntu下有用网易云音乐的,就上网看了一下 还真的 ...
- win7电脑桌面壁纸曝光过高影响图标怎么办?亲测实用解决方法
现在用win7系统的人应该还是挺多的吧,虽然说windows家族已经升级到现在的win11了,相信大多数人家用的电脑系统还是win7吧,今天要讲的是一个壁纸曝光度过高的解决办法,虽然还不清楚为什么,但 ...
- laravel学习:模块化caffeinated
# Modules Extract and modularize your code for maintainability. Essentially creates "mini-larav ...
- Python_练习_VS清理器
#导入os import os #创建列表放入后缀 d=[ '.txt','obj','tlog','lastbuildstate','idb','pdb','pch','res','ilk','sd ...
- caffe编译
用make -j带一个参数,可以把项目在进行并行编译,比如在一台双核的机器上,完全可以用make -j4,让make最多允许4个编译命令同时执行,这样可以更有效的利用CPU资源 也就是说make -j ...
- Django中使用多线程发送邮件
1.settings.py 增加Email设置 #mail EMAIL_HOST = ‘smtp.gmail.com’ #邮件smtp服务器 EMAIL_POR ...
- freenas iscsi initiator 配置
1.加载Iscsi Initiator 模块 freebsd从7.0开始已经包含了Iscsi Initiator ,不需要安装后再使用,但在使用前,需要加载模块. # kldload -v iscsi ...
- Spring Boot 打包分离依赖 JAR 和配置文件
<properties> <java.version>1.8</java.version> <project.build.sourceEncoding> ...
- python 导入beautifulsoup报错
导入Beautifulsoup 报错 AttributeError: 'module' object has no attribute '_base' 解决方法: pip install --up ...
- js正则表达式,只允许输入纯数字或‘/’
//输入框,限数字和/----需要多个数量询价,请以/分隔 function onlyonce(obj) {//先把非数字的都替换掉,除了数字和.obj.value = obj.value.repla ...