Java HashSet和LinkedHashSet的用法
类HashSet和LinkedHashSet都是接口Set的实现,两者都不能保存重复的数据。主要区别是HashSet不保证集合中元素的顺序,即不能保证迭代的顺序与插入的顺序一致。
而LinkedHashSet按照元素插入的顺序进行迭代,即迭代输出的顺序与插入的顺序保持一致。
以下是HastSet和LinkedHashSet的用法示例:
[java] view plain copy
- 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.