Java HashSet和LinkedHashSet的用法

类HashSet和LinkedHashSet都是接口Set的实现,两者都不能保存重复的数据。主要区别是HashSet不保证集合中元素的顺序,即不能保证迭代的顺序与插入的顺序一致。

而LinkedHashSet按照元素插入的顺序进行迭代,即迭代输出的顺序与插入的顺序保持一致。

以下是HastSet和LinkedHashSet的用法示例:

  1. import java.util.Collections;
  2. import java.util.HashSet;
  3. import java.util.Iterator;
  4. import java.util.LinkedHashSet;
  5. import java.util.Set;
  6. public class JavaTest {
  7. // HashSet不保证集合的迭代顺序;也许在某些时间迭代的顺序与插入顺序一致,但是不保证该顺序恒久不变。
  8. private static Set<Integer> mSetInt = new HashSet<Integer>();
  9. private static Set<String> mSetString = new HashSet<String>();
  10. // LinkedHashSet按照元素插入的顺序进行迭代,LinkedHashSet不是线程安全的。
  11. private static Set<Integer> mLinkedSetInt = Collections.synchronizedSet(new LinkedHashSet<Integer>());
  12. private static Set<String> mLinkedSetString = Collections.synchronizedSet(new LinkedHashSet<String>());
  13. /**
  14. * @param args
  15. */
  16. public static void main(String[] args) {
  17. for (int i = 0; i < 50; i++) {
  18. mSetInt.add(i);
  19. mSetString.add(String.valueOf(i));
  20. mLinkedSetInt.add(i);
  21. mLinkedSetString.add(String.valueOf(i));
  22. }
  23. Iterator<Integer> setIntIt = mSetInt.iterator();
  24. System.out.println("The sequence of HashSet for Integer:");
  25. while(setIntIt.hasNext()) {
  26. System.out.print(setIntIt.next() + " ");
  27. }
  28. System.out.println();
  29. System.out.println("The sequence of HashSet for String:");
  30. Iterator<String> setStringIt = mSetString.iterator();
  31. while(setStringIt.hasNext()) {
  32. System.out.print(setStringIt.next() + " ");
  33. }
  34. System.out.println();
  35. System.out.println("The sequence of LinkedHashSet for Integer:");
  36. Iterator<Integer> linkedSetIntIt = mLinkedSetInt.iterator();
  37. while(linkedSetIntIt.hasNext()) {
  38. System.out.print(linkedSetIntIt.next() + " ");
  39. }
  40. System.out.println();
  41. System.out.println("The sequence of LinkedHashSet for String:");
  42. Iterator<String> linkedSetStringIt = mLinkedSetString.iterator();
  43. while(linkedSetStringIt.hasNext()) {
  44. System.out.print(linkedSetStringIt.next() + " ");
  45. }
  46. System.out.println();
  47. }
  48. }

输出结果如下:

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的用法的更多相关文章

  1. Java集合框架(二)—— HashSet、LinkedHashSet、TreeSet和EnumSet

    Set接口 前面已经简绍过Set集合,它类似于一个罐子,一旦把对象'丢进'Set集合,集合里多个对象之间没有明显的顺序.Set集合与Collection基本上完全一样,它没有提供任何额外的方法. Se ...

  2. 复习java基础第三天(集合:Collection、Set、HashSet、LinkedHashSet、TreeSet)

    一.Collection常用的方法: Java 集合可分为 Set.List 和 Map 三种体系: Set:无序.不可重复的集合. List:有序,可重复的集合. Map:具有映射关系的集合. Co ...

  3. Java集合系列(三):HashSet、LinkedHashSet、TreeSet的使用方法及区别

    本篇博客主要讲解Set接口的三个实现类HashSet.LinkedHashSet.TreeSet的使用方法以及三者之间的区别. 注意:本文中代码使用的JDK版本为1.8.0_191 1. HashSe ...

  4. Java中的集合HashSet、LinkedHashSet、TreeSet和EnumSet(二)

    Set接口 前面已经简绍过Set集合,它类似于一个罐子,一旦把对象'丢进'Set集合,集合里多个对象之间没有明显的顺序.Set集合于Collection基本上完全一样,它没有提供任何额外的方法. Se ...

  5. List,Set,Map在java.util包下都是接口 List有两个实现类:ArrayList和LinkedList Set有两个实现类:HashSet和LinkedHashSet AbstractSet实现了Set

    List,Set,Map在java.util包下都是接口 List有两个实现类:ArrayList和LinkedListSet有两个实现类:HashSet和LinkedHashSetAbstractS ...

  6. Java自学-集合框架 HashSet、LinkedHashSet、TreeSet之间的区别

    HashSet. LinkedHashSet.TreeSet之间的区别 步骤 1 : HashSet LinkedHashSet TreeSet HashSet: 无序 LinkedHashSet: ...

  7. [Java数据结构]HashSet,LinkedHashSet,TreeeSet

    Java中Set表示一个不包括重复元素的集合,它有HashSet,LinkedHashSet,TreeeSet三种常用实现. HashSet是Set的最常用实现,它常被用来清除重复元素. 例程: Se ...

  8. Java Set 常用集合 HashSet、LinkedHashSet、TreeSet

    Java 中的 Set 是非常常用的数据类型.Set 是无序的 Collection,Java Set 有三个常用的实现类,分别是:HashSet.LinkedHashSet.TreeSet 本文基于 ...

  9. 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 ...

随机推荐

  1. Magnolia-CMS安装配置

    Magnolia-CMS安装配置 Magnolia-CMS安装配置 介绍:Magnolia 是一个开源基于Java的Web内容管理系统(CMS),构建在Java内容知识库标准(JSR-170).它适合 ...

  2. a 标签的四个伪类

    link        有链接属性时visited    链接地址已被访问过active     被用户激活(在鼠标点击与释放之间发生的事件)hover      其鼠标悬停 <!DOCTYPE ...

  3. aix vg lv pv

    lsvg lsvg -o lsvg rootvg 查看rootvg的信息 lsvg -p rootvg 查看rootvg卷里的物理硬盘以及分布信息 lsvg -l rootvg 查看rootvg卷下的 ...

  4. Oracle 中单引号和双引号的区别

    问题产生原因: insert into t_Cluster_Showresult(Outhostname,Domainlist,Iplist,Classify) values ("20145 ...

  5. CSU - 1356 Catch(dfs染色两种写法,和hdu4751比较)

    Description A thief is running away! We can consider the city to N–. The tricky thief starts his esc ...

  6. OC运行时和方法机制笔记

    在OC当中,属性是对字段的一种特殊封装手段. 在编译期,编译器会将对字段的访问替换为内存偏移量,实质是一种硬编码. 如果增加一个字段,那么对象的内存排布就会改变,需要重新编译才行. OC的做法是,把实 ...

  7. 弱类型语言中的0和空字符串(''或"")以及字符串'0'

    在弱类型语言(js/PHP)中, 当我们用==判断0和'0'以及空字符串(''或"")是否相等的时候, 返回的是true. 而且在PHP中, 当我们用==判断0和null是否相等的 ...

  8. BCTF warmup 50

    这是一道关于RSA的解密题:首先,我们要明白,通常是公钥加密.私钥解密,私钥签名.公钥验证.这个题目中给出的是一个公钥和一段密文. 刚开始一直以为和验证签名有关,费劲脑汁也想不出来怎么办.下面介绍些思 ...

  9. C#中T的用法

    之前一直用List<T>这样的泛型,看到过有些参数类型也可以直接用T的,觉得很好用,但是一直用不了,现在才发现原来是少加了<T> public T getdate<T&g ...

  10. node.js(六) UTIL模块

    1.inspect函数的基本用法 util.inspect(object,[showHidden],[depth],[colors])是一个将任意对象转换为字符串的函数,通常用于调试和错误输出.它至少 ...