61.md 8.2 KB
Newer Older
W
wizardforcel 已提交
1
# Java `LinkedHashSet`示例
W
init  
wizardforcel 已提交
2 3 4

> 原文: [https://javatutorial.net/java-linkedhashset-example](https://javatutorial.net/java-linkedhashset-example)

W
wizardforcel 已提交
5
Java 中的`LinkedHashSet`类与[`HashSet`](https://javatutorial.net/java-hashset-example)不同,因为其实现维护了所有元素上的双链表。 此链表定义了迭代顺序,即将元素插入到集合中的顺序。 这称为插入顺序。 如果将元素重新插入到集合中,则插入顺序不受其影响。 某些人更喜欢此实现,因为它使用户摆脱了[`HashSet`](https://javatutorial.net/java-hashset-example)提供的未指定且混乱的排序。
W
init  
wizardforcel 已提交
6 7 8

![java-featured-image](img/e0db051dedc1179e7424b6d998a6a772.jpg)

W
wizardforcel 已提交
9
## `LinkedHashSet`的优点
W
init  
wizardforcel 已提交
10

W
wizardforcel 已提交
11 12 13 14
*   `LinkedHashSet`维护元素的插入顺序
*   插入,删除和检索操作的时间复杂度为`O(1)`(恒定时间)。
*   `LinkedHashSet`允许 1 个`null`元素。
*   `LinkedHashSet`使用`equals()``hashCode()`,因此可以删除可能的重复元素。
W
init  
wizardforcel 已提交
15 16 17 18 19

## 继承图

![Inheritance diagram of LinkedHashSet](img/8a195dfab57c2ccf0e2850805186b6b4.jpg)

W
wizardforcel 已提交
20
`LinkedHashSet`的继承图
W
init  
wizardforcel 已提交
21

W
wizardforcel 已提交
22
## `LinkedHashSet`中的构造方法
W
init  
wizardforcel 已提交
23

W
wizardforcel 已提交
24 25 26 27
1.  `LinkedHashSet()`:初始化一个新的链接哈希集,默认初始容量为 16,负载因子为 0.75。
2.  `LinkedHashSet(Collection <? extends E> c)`:使用与指定集合相同的元素初始化一个新的链接哈希集。
3.  `LinkedHashSet(int initialCapacity)`:使用指定的初始容量初始化一个新的链接哈希集。
4.  `LinkedHashSet(int initialCapacity, float loadFactor)`:使用指定的容量和负载因子初始化一个新的链接哈希集。
W
init  
wizardforcel 已提交
28

W
wizardforcel 已提交
29
## `LinkedHashSet`中的方法
W
init  
wizardforcel 已提交
30

W
wizardforcel 已提交
31 32 33 34 35
1.  从类`java.util.HashSet`继承的方法:[`add`](https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html#add(E)),[`clear`](https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html#clear()),[`clone`](https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html#clone()),[`contains`](https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html#contains(java.lang.Object)), [`isEmpty`](https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html#isEmpty()) , [`iterator`](https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html#iterator()),[`remove`](https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html#remove(java.lang.Object)),[`size`](https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html#size())
2.  从类`java.util.AbstractSet`继承的方法:[`equals`](https://docs.oracle.com/javase/7/docs/api/java/util/AbstractSet.html#equals(java.lang.Object)), [`hashCode`](https://docs.oracle.com/javase/7/docs/api/java/util/AbstractSet.html#hashCode()) , [`removeAll`](https://docs.oracle.com/javase/7/docs/api/java/util/AbstractSet.html#removeAll(java.util.Collection))
3.  从类`java.util.AbstractCollection`继承的方法: [`addAll`](https://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html#addAll(java.util.Collection)) , [`containsAll`](https://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html#containsAll(java.util.Collection)) , [`keepAll`](https://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html#retainAll(java.util.Collection)) , [`toArray`](https://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html#toArray()) , [`toArray(T[])`](https://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html#toArray(T[])) , [`toString`](https://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html#toString())
4.  从类`java.lang.Object`继承的方法:[`finalize`](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#finalize()), [`getClass`](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#getClass()) ,[`notify`](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#notify()), [`notifyAll`](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#notifyAll()) , [`wait`](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#wait()) , [`wait(long)`](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#wait(long)),[`wait(long, int)`](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#wait(long,%20int))
5.  从接口`java.util.Set`继承的方法: [`add`](https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#add(E)) , [`addAll`](https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#addAll(java.util.Collection)) ,[`clear`](https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#clear()),[`contains`](https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#contains(java.lang.Object)), [`containsAll`](https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#containsAll(java.util.Collection)) , [`equals`](https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#equals(java.lang.Object)), [`hashCode`](https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#hashCode()) , [`isEmpty`](https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#isEmpty()) ,[`iterator`](https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#iterator()),[`remove`](https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#remove(java.lang.Object)), [`removeAll`](https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#removeAll(java.util.Collection)) , [`retainAll`](https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#retainAll(java.util.Collection)) ,[`size`](https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#size()), [`toArray`](https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#toArray()) , [`toArray(T[])`](https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#toArray(T[]))
W
init  
wizardforcel 已提交
36 37 38

有关所有方法的文档,请访问 [Oracle 官方文档页面](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashSet.html)

W
wizardforcel 已提交
39
## 使用`Iterator()`迭代`LinkedHashSet`
W
init  
wizardforcel 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

```java
import java.util.*;  
class LinkedHashSetExample{  
     public static void main(String args[]){  
        LinkedHashSet<String> animals=new LinkedHashSet();  
               animals.add("Elephant");    
               animals.add("Tiger");    
               animals.add("Lion");   

               Iterator<String> iterator=animals.iterator();  
               while(iterator.hasNext())  {  
                     System.out.println(iterator.next());  
               }  
      }  
}
```

**输出:**

```java
Elephant
Tiger
Lion
```

W
wizardforcel 已提交
66
## 使用`Fo​​r`循环遍历`LinkedHashSet`
W
init  
wizardforcel 已提交
67 68 69 70 71 72 73 74

```java
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>();
for (int i = 0; i < linkedHashSet.size(); i++) {
    System.out.println(linkedHashSet.get(i));
}
```

W
wizardforcel 已提交
75
## 使用增强`For`循环遍历`LinkedHashSet`
W
init  
wizardforcel 已提交
76 77 78 79 80 81 82

```java
for (String temp : linkedHashSet) {
    System.out.println(temp);
}
```

W
wizardforcel 已提交
83
## 使用`While`循环遍历`LinkedHashSet`
W
init  
wizardforcel 已提交
84 85 86 87 88 89 90 91 92

```java
int i = 0;
while (i < linkedHashSet.size()) {
    System.out.println(linkedHashSet.get(i));
    i++;
}
```

W
wizardforcel 已提交
93
## `LinkedHashSet`的示例程序
W
init  
wizardforcel 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

```java
import java.util.LinkedHashSet;   
public class LinkedHashSetExample  
{   
    public static void main(String[] args)  
    {   
        LinkedHashSet<String> coins =  
                           new LinkedHashSet<String>();   

        // Adding element to LinkedHashSet   
        coins.add("5");   
        coins.add("10");   
        coins.add("20");   
        coins.add("50"); 
        coins.add("100"); 

        // Adding a duplicate would result in no addition of the element  
        coins.add("5");
        // Adding another coin value
        coins.add("75");   

        System.out.println("Size of the list = " + 
                                    coins.size());   
        System.out.println("Original LinkedHashSet:" + coins);   
        System.out.println("Removing 75 from LinkedHashSet: " + 
                            coins.remove("75"));    
        System.out.println("Checking if 50 is present=" +  
                            coins.contains("50")); 
        System.out.println("Updated LinkedHashSet: " + coins);   
    }   
}
```

**输出:** 

```java
Size of LinkedHashSet=5
Original LinkedHashSet:[5, 10, 20, 50, 75, 100]
Removing 75 from LinkedHashSet: true
Checking if 50 is present=true
Updated LinkedHashSet:[5, 10, 20, 50, 100]
```