Java HashSet和LinkedHashSet的用法
Java HashSet和LinkedHashSet的用法
类HashSet和LinkedHashSet都是接口Set的实现,两者都不能保存重复的数据。主要区别是HashSet不保证集合中元素的顺序,即不能保证迭代的顺序与插入的顺序一致。
而LinkedHashSet按照元素插入的顺序进行迭代,即迭代输出的顺序与插入的顺序保持一致。
以下是HastSet和LinkedHashSet的用法示例:
- import java.util.Collections;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.LinkedHashSet;
- import java.util.Set;
- public class JavaTest {
- // HashSet不保证集合的迭代顺序;也许在某些时间迭代的顺序与插入顺序一致,但是不保证该顺序恒久不变。
- private static Set<Integer> mSetInt = new HashSet<Integer>();
- private static Set<String> mSetString = new HashSet<String>();
- // LinkedHashSet按照元素插入的顺序进行迭代,LinkedHashSet不是线程安全的。
- private static Set<Integer> mLinkedSetInt = Collections.synchronizedSet(new LinkedHashSet<Integer>());
- private static Set<String> mLinkedSetString = Collections.synchronizedSet(new LinkedHashSet<String>());
- /**
- * @param args
- */
- public static void main(String[] args) {
- for (int i = 0; i < 50; i++) {
- mSetInt.add(i);
- mSetString.add(String.valueOf(i));
- mLinkedSetInt.add(i);
- mLinkedSetString.add(String.valueOf(i));
- }
- Iterator<Integer> setIntIt = mSetInt.iterator();
- System.out.println("The sequence of HashSet for Integer:");
- while(setIntIt.hasNext()) {
- System.out.print(setIntIt.next() + " ");
- }
- System.out.println();
- System.out.println("The sequence of HashSet for String:");
- Iterator<String> setStringIt = mSetString.iterator();
- while(setStringIt.hasNext()) {
- System.out.print(setStringIt.next() + " ");
- }
- System.out.println();
- System.out.println("The sequence of LinkedHashSet for Integer:");
- Iterator<Integer> linkedSetIntIt = mLinkedSetInt.iterator();
- while(linkedSetIntIt.hasNext()) {
- System.out.print(linkedSetIntIt.next() + " ");
- }
- System.out.println();
- System.out.println("The sequence of LinkedHashSet for String:");
- Iterator<String> linkedSetStringIt = mLinkedSetString.iterator();
- while(linkedSetStringIt.hasNext()) {
- System.out.print(linkedSetStringIt.next() + " ");
- }
- System.out.println();
- }
- }
输出结果如下:
The sequence of HashSet for Integer:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 16 19 18 21 20 23 22 25 24 27 26 29 28 31 30 34 35 32 33 38 39 36 37 42 43 40 41 46 47 44 45 49 48
The sequence of HashSet for String:
35 36 33 34 39 37 38 43 42 41 40 22 23 24 25 26 27 28 29 3 2 1 0 7 30 6 5 32 4 31 9 8 19 17 18 15 16 13 14 11 12 21 20 49 48 45 44 47 46 10
The sequence of LinkedHashSet for Integer:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
The sequence of LinkedHashSet for String:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
从输出结果看,如果HastSet中保存的是Integer类型和String类型的对象,迭代的顺序与插入的顺序不一致,其中String类型的元素不一致的情况比Integer类型的元素要明显的多。
map<string,map<string,string>>:map中map用法示例
include <iostream>
#include <map>
using namespace std;
int main()
{
map<string,map<string,string> > mapmaps;
map<string,string> mapmap;
mapmap.insert(pair<string,string>("ysl","ysh"));
mapmaps.insert(pair<string,map<string,string> >("ysy",mapmap));
cout<<mapmaps.begin()->first<<endl;
cout<<mapmaps.begin()->second.begin()->first<<endl;
cout<<mapmaps.begin()->second.begin()->second<<endl;
return 0;
}
结果: ysy ysl ysh Process returned 0 (0x0) execution time : 0.250 s Press any key to continue.
Java HashSet和LinkedHashSet的用法的更多相关文章
- Java集合框架(二)—— HashSet、LinkedHashSet、TreeSet和EnumSet
Set接口 前面已经简绍过Set集合,它类似于一个罐子,一旦把对象'丢进'Set集合,集合里多个对象之间没有明显的顺序.Set集合与Collection基本上完全一样,它没有提供任何额外的方法. Se ...
- 复习java基础第三天(集合:Collection、Set、HashSet、LinkedHashSet、TreeSet)
一.Collection常用的方法: Java 集合可分为 Set.List 和 Map 三种体系: Set:无序.不可重复的集合. List:有序,可重复的集合. Map:具有映射关系的集合. Co ...
- Java集合系列(三):HashSet、LinkedHashSet、TreeSet的使用方法及区别
本篇博客主要讲解Set接口的三个实现类HashSet.LinkedHashSet.TreeSet的使用方法以及三者之间的区别. 注意:本文中代码使用的JDK版本为1.8.0_191 1. HashSe ...
- Java中的集合HashSet、LinkedHashSet、TreeSet和EnumSet(二)
Set接口 前面已经简绍过Set集合,它类似于一个罐子,一旦把对象'丢进'Set集合,集合里多个对象之间没有明显的顺序.Set集合于Collection基本上完全一样,它没有提供任何额外的方法. Se ...
- List,Set,Map在java.util包下都是接口 List有两个实现类:ArrayList和LinkedList Set有两个实现类:HashSet和LinkedHashSet AbstractSet实现了Set
List,Set,Map在java.util包下都是接口 List有两个实现类:ArrayList和LinkedListSet有两个实现类:HashSet和LinkedHashSetAbstractS ...
- Java自学-集合框架 HashSet、LinkedHashSet、TreeSet之间的区别
HashSet. LinkedHashSet.TreeSet之间的区别 步骤 1 : HashSet LinkedHashSet TreeSet HashSet: 无序 LinkedHashSet: ...
- [Java数据结构]HashSet,LinkedHashSet,TreeeSet
Java中Set表示一个不包括重复元素的集合,它有HashSet,LinkedHashSet,TreeeSet三种常用实现. HashSet是Set的最常用实现,它常被用来清除重复元素. 例程: Se ...
- Java Set 常用集合 HashSet、LinkedHashSet、TreeSet
Java 中的 Set 是非常常用的数据类型.Set 是无序的 Collection,Java Set 有三个常用的实现类,分别是:HashSet.LinkedHashSet.TreeSet 本文基于 ...
- Leetcode: LFU Cache && Summary of various Sets: HashSet, TreeSet, LinkedHashSet
Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...
随机推荐
- 设置php在apache下加载ini配置文件路径,~和curl扩展无法加载的问题
php以模块的方式加载到apache的时候,php配置文件目录为C:windows.这不合理,应该选择php本身目录的配置文件加载,可以在apache的httpd.conf配置文件里设置PHPIniD ...
- PHP 魔术常量__FUNCTION__与__METHOD__的区别
__FUNCTION__ 返回 函数名称(PHP 4.3.0 新加).自 PHP 5 起本常量返回该函数被定义时的名字(区分大小写).在 PHP 4 中该值总是小写字母的. __METHOD__ ...
- sqlplus部署
1 下载客户端安装包根据系统选择(建议下载zip包) http://www.oracle.com/technetwork/database/features/instant-client/ ...
- python----iter\next
1.说明:__getitem__\setitem可以迭代,它已经不被推荐了:建议使用__iter__\next. 2.python会先去检查__iter__\next然后再去检查__getitem__ ...
- 进程占用百分百CPU不卡(从未试过,当别的程序运行的时候,当前程序还会运行吗?)
在写程序中.为了让程序效率高.有时会点用很高的CPU.这里用户体验不好可以设置线程的优先级来搞定. BOOL SetThreadPriority( HANDLE hThread, // handle ...
- 关于异常Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
原文:关于异常Microsoft.CSharp.RuntimeBinder.RuntimeBinderException 关于Microsoft.CSharp.RuntimeBinder.Runtim ...
- Matlab.NET混编技巧之——找出Matlab内置函数
原文 http://www.cnblogs.com/asxinyu/p/3295309.html Matlab与.NET的混合编程,掌握了基本过程,加上一定的开发经验和算法基础,肯 定不难.反之,有时 ...
- web编码(转)
问题2.浏览器编码方式是根据“响应标头-response header”中的键为“Content-Type”的值来自动选择判断,而不会简单的根据你在html中看到的标签值<meta http-e ...
- C++ 之再继续
1C++函数重载,内联函数(for程序性能优化),函数递归
- Quartz.NET 2.0 作业调度框架使用
Quartz.NET是一个开源的作业调度框架,是 OpenSymphony 的 Quartz API 的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不 ...