Java HashSet和LinkedHashSet的用法

Java HashSet和LinkedHashSet的用法

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

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

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

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

输出结果如下:

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.
原文地址:https://www.cnblogs.com/timssd/p/5483897.html