说到Java中的LinkedHashSet类,就不得不提到它实现了Set接口。LinkedHashSet创建一个集合,该集合将元素存储在哈希表中,但保持元素的插入顺序,这与其HashSet对应物不同。
简单的 LinkedHashSet 示例在下面的示例中,我们展示了
LinkedHashSet对象的初始化并使用
add()方法填充集合。
什么是 Java 中的设置
让我们简单回顾一下,Set接口定义了一个集合(set)。它扩展了Collection并定义了不允许重复元素的集合的行为。因此,如果尝试向集合中添加重复元素,add()方法将返回false 。该接口没有定义自己的任何其他方法。Set接口负责存储对象的唯一性,唯一性由 equals() 方法的实现决定。因此,如果要将创建的类的对象添加到 Set 中,最好重写equals()方法。LinkedHashSet 类
在说LinkedHashSet类之前,我们需要先提一下它的近亲HashSet类。HashSet实现了Set接口。它创建一个集合,将元素存储在哈希表中。哈希表的元素存储为键值对。键指定存储值的单元格(或段)。密钥的内容用于确定称为哈希码的唯一值。我们可以将哈希码视为对象标识符,尽管它不一定是唯一的。此散列码还用作存储与密钥关联的数据的索引。LinkedHashSet Java 类在不添加任何新方法的情况下扩展了HashSet。LinkedHashSet允许您快速检查条目是否存在,就像HashSet一样,但内部包含一个有序列表。这意味着它存储元素的插入顺序。换句话说,LinkedHashSet按照插入顺序维护集合元素的链表。这允许有序迭代插入到集合中。但是这会导致LinkedHashSet类执行操作的时间比HashSet类长。LinkedHashSet 的重要特性
-
我们只能在LinkedHashSet中存储唯一元素
-
LinketHashSet让我们按照插入的顺序提取元素
-
LinkedHashSet未同步
-
LinkedHashSet允许存储空元素
-
LinkedHashSet使用哈希技术根据哈希码将元素存储在指定索引处
LinkedHashSet 方法
HashSet除了从其父类继承的方法外,还定义了以下方法:-
boolean add(Object o)将指定的元素添加到此集合中(如果它不存在)。
-
void clear()从此集合中删除所有元素。
-
对象 clone()返回此LinkedHashSet实例的浅表副本:元素本身未被克隆。
-
boolean contains(Object o)如果此集合包含指定元素,则返回 true。
-
boolean isEmpty()如果此集合不包含任何元素,则返回true 。
-
迭代器 iterator()返回此集合元素的迭代器。
-
boolean remove(Object o)从此集合中移除指定元素(如果存在)。
-
int size()返回此集合中的元素数(它的元素数)。
import java.util.LinkedHashSet;
import java.util.Set;
public class LinkedHashSetEx1 {
public static void main(String[] args) {
//LinkedHashSet() Init
Set<String> set = new LinkedHashSet<>();
//adding elements to LinkedHashSet
set.add("Re"); //first added element
set.add("Do");
set.add("Fa");
set.add("Sol");
set.add("La");
set.add("Ti");
set.add("Mi");//last added element
System.out.println(set);
}
}
输出是:
[Re, Do, Fa, Sol, La, Ti, Mi]
如您所见,我们集合中的元素出现的顺序与我们放置它们的顺序相同。

示例 2. 将副本添加到 LinkedHashSet
让我们再次放入带有乐谱名称的 LinkedHashSet 7 个元素,并放入一个与之前放置的元素相同的新元素。
import java.util.LinkedHashSet;
import java.util.Set;
public class LinkedHashSetEx2 {
public static void main(String[] args) {
Set<String> set = new LinkedHashSet<>();
set.add("Re");
set.add("Do");
set.add("Fa");
set.add("Sol");
set.add("La");
set.add("Ti");
set.add("Mi");
set.add("Sol");
System.out.println(set);
}
}
该程序的输出在这里:
[Re, Do, Fa, Sol, La, Ti, Mi]
示例二的输出与第一个示例中的完全相同。
LinkedHashSet集合中不能有两个相似的元素。当我们尝试放置第二个时,它就被忽略了。
示例 3. 从 LinkedHashSet 中删除元素
import java.util.LinkedHashSet;
import java.util.Set;
public class LinkedHashSet3 {
public static void main(String[] args) {
Set<String> set = new LinkedHashSet<>();
set.add("Re");
set.add("Do");
set.add("Fa");
set.add("Sol");
set.add("La");
set.add("Ti");
set.add("Mi");
System.out.println(set);
set.remove("Fa");// removing an element from our set
set.remove("Score");//trying to remove element that isn't in set
System.out.println(set.remove("Score"));
System.out.println("Print our set without elements removed: ");
System.out.println(set);
set.clear();
System.out.println("Print out our set after clear command: ");
System.out.println(set);
}
}
该程序的输出在这里:
[Re, Do, Fa, Sol, La, Ti, Mi] false 在不删除元素的情况下打印我们的集合:[Re, Do, Sol, La, Ti, Mi] 在清除命令后打印我们的集合:[]
如您所见,应用于不存在元素的
remove()方法不会导致程序错误。如果元素未被删除,它只返回
false ;如果元素在
LinkedHashSet中然后被删除,则返回
true 。
LinkedHashSet 与 HashSet
这两个阶级是近亲。但是在 HashSet内部,它使用 HashMap来存储对象,而 LinkedHashSet使用 LinkedHashMap。如果不需要维护插入顺序但需要存储唯一对象,使用 HashSet更合适。如果您需要维护元素的插入顺序,那么 LinkedHashSet是您的选择。 LinkedHashSet的性能比 HashSet慢一点,因为 LinkedHashSet使用内部的 LinkedList来维护元素的插入顺序。让我们举个例子:
import java.util.*;
public class LinkedHashSetExample1 {
public static void main(String[] args) {
// while regular hash set orders its elements according to its hashcode stamps
Set<Integer> regularHashSet = new HashSet<>();
regularHashSet.add(7);
regularHashSet.add(3);
regularHashSet.add(5);
regularHashSet.add(65536);
regularHashSet.add(9);
// few duplicates
regularHashSet.add(5);
regularHashSet.add(7);
// next will print:
// > regularHashSet = [65536, 3, 5, 7, 9]
System.out.println("regularHashSet = " + regularHashSet);
// linked hash set keeps order of adding unchanged
Set<Integer> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add(7);
linkedHashSet.add(3);
linkedHashSet.add(5);
linkedHashSet.add(65536);
linkedHashSet.add(9);
// few duplicates
linkedHashSet.add(5);
linkedHashSet.add(7);
// next will print:
// > linkedHashSet = [7, 3, 5, 65536, 9]
System.out.println("linkedHashSet = " + linkedHashSet);
}
}
该程序的输出是:
regularHashSet = [65536, 3, 5, 7, 9] linkedHashSet = [7, 3, 5, 65536, 9]
在实际应用程序中使用 Java LinkedHashSet
由于 LinkedHashSet允许您快速检查条目是否存在并存储顺序,因此此集合似乎非常便于从列表中删除重复项。或者,例如,解决像我包里最近看到的物品这样的问题。或者,还记得这样一款游戏吗,Pokemon Go? LinkedHashSet可以存储您遇到的 Pokémon 列表以及它们在您的路径上遇到的顺序。在这种情况下,“重复”的神奇宝贝将不再添加到列表中。或者,例如,您在任何有关卡的游戏中遇到过的按关卡分类的 Boss 列表。或者宇宙天体的发现史。 链接哈希集允许您快速检查一个空间体是否已经在列表中,如果不存在,则将其添加到列表中。让我们举一个消除重复的例子。
import java.util.*;
class LinkedHashSetExample2 {
public static void main(String[] args) {
List<String> listWithDuplicates = List.of("some","elements","with", "few", "duplicates", "were", "here", "duplicates", "duplicates");
Set<String> linkedHashSet = new LinkedHashSet<>(listWithDuplicates);
List<String> listWithoutDuplicates = new ArrayList<>(linkedHashSet);
// next will print:
// > listWithDuplicates = [some, elements, with, few, duplicates, here, duplicates, duplicates]
System.out.println("listWithDuplicates = " + listWithDuplicates);
// next will print:
// > listWithoutDuplicates = [some, elements, with, few, duplicates, here]
System.out.println("listWithoutDuplicates = " + listWithoutDuplicates);
// -------------------------------------------------------------------------
// while using regular Hash Set will generally produces some unexpected order
Set<String> regularHashSet = new HashSet<>(listWithDuplicates);
// next will print:
// > linkedHashSet = [some, elements, with, few, duplicates, were, here]
System.out.println("linkedHashSet = " + linkedHashSet);
// next will print:
// > regularHashSet = [here, some, with, duplicates, were, elements, few]
System.out.println("regularHashSet = " + regularHashSet);
}
}
该程序的输出在这里:
listWithDuplicates = [一些,元素,有,很少,重复,在这里,重复,重复] listWithoutDuplicates = [一些,元素,有,很少,重复,在这里] , were, here] regularHashSet = [here, some, with, duplicates, were, elements, few]
GO TO FULL VERSION