【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 ...
随机推荐
- 集成新版(5.17+)Activiti Modeler与Rest服务
声明: 此教程适合Activiti 5.17+版本. 本博客所涉及的内容均可在kft-activiti-demo中找到. 在线demo可以访问 http://demo.kafeitu.me:8080/ ...
- Jmeter重要组件介绍(一)
一.常用的取样器 二.常用的逻辑控制器 三.前置处理器 四.后置处理器 五.断言 六.定时器 七.配置元件 八.监听器
- SQL条件语句(IF, CASE WHEN, IF NULL)
1.IF 表达式:IF( expr1 , expr2 , expr3 ) expr1条件,条件为true,则值是expr2 ,false,值就是expr3 SELECT o.id,u.acco ...
- Spring MVC全局异常后返回JSON异常数据
问题: 当前项目是作为手机APP后台支持,使用spring mvc + mybaits + shiro进行开发.后台服务与手机端交互是发送JSON数据.如果后台发生异常,会直接返回异常页面,显示异常内 ...
- 02Hibernate基本配置
Hibernate基本配置 1.引入jar 2.建立项目 3.创建实体类 package com.sqlserver.domain; public class Customer { long cust ...
- JavaSE-18 常用工具类
学习要点 Object类 枚举 包装类 Math类 Random类 字符串处理 日期时间 Object类 1 什么是Object类 Object类存储在java.lang包中,是所有java类(Ob ...
- python logging 日志使用
https://docs.python.org/3/library/logging.html1.日志级别 日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRI ...
- MySQL-----用户和授权管理
用户管理: 创建用户: create user '用户名'@'用户pc的ip地址(ip可以写精准点的,也可以是网段的,也可以写一个‘’%‘’提所有)' identified(设置密码) by '密码 ...
- 第十九节:Scrapy爬虫框架之Middleware文件详解
# -*- coding: utf-8 -*- # 在这里定义蜘蛛中间件的模型# Define here the models for your spider middleware## See doc ...
- LeetCode(59)SPiral Matrix II
题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...