集合框架05

一、HashSet集合

 1 public class Demo01 {
 2     /*
 3      * Set接口,特点不重复元素,没索引
 4      * Set接口的实现类,HashSet(哈希表)
 5      * 特点:无序集合,存储和取出的顺序不同,没有索引,不存储
 6      */
 7     public static void main(String[] args) {
 8         Set<String> set = new HashSet<String>();
 9         set.add("wea1");
10         set.add("a2");
11         set.add("rw3");
12         set.add("ea4r");
13         set.add("awew5");
14         
15         Iterator<String> it = set.iterator();
16         while(it.hasNext()){
17             System.out.println(it.next());
18         }
19         
20         System.out.println("=============");
21         for (String string : set) {
22             System.out.println(string);
23         }
24     }
25 }

字符串对象的哈希值

1 String s1 = new String("123");
2 System.out.println(s1.hashCode());
3 String s2 = new String("234");
4 System.out.println(s2.hashCode());

二、LinkHashSet集合

 1 public class Demo01 {
 2     /*
 3      * LinkedHashSet基于链表的哈希表
 4      * 继承自HashSet
 5      * 特点:具有顺序,存储和取出的顺序相同
 6      * 线程不安全的集合,运行速度快
 7      */
 8     public static void main(String[] args) {
 9         LinkedHashSet<Integer> link = new LinkedHashSet<Integer>();
10         link.add(12);
11         link.add(222);
12         link.add(122);
13         link.add(322);
14         link.add(122);
15         System.out.println(link);
16     }
17 }
原文地址:https://www.cnblogs.com/Nelsoner/p/6685559.html