Java中的LinkedHashSet及其示例

这个 LinkedHashSet 是HashSet的一个有序版本,它在所有元素之间维护一个双链接列表。当需要维护迭代顺序时,使用这个类。当遍历 哈希集 顺序是不可预测的,而LinkedHashSet允许我们按照元素插入的顺序迭代元素。当使用迭代器在LinkedHashSet中循环时,元素将按插入顺序返回。

null

LinkedHashSet的层次结构如下:

LinkedHashSet in Java with Examples

参数: 此集合维护的元素类型

All Implemented Interfaces are as listed below:SerializableCloneable,Iterable<E>Collection<E>Set<E>

语法: 公告

public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, Serializable
  • 只包含HashSet之类的唯一元素。它扩展了HashSet类并实现了Set接口。
  • 保持插入顺序。

LinkedHashSet类的构造函数

1.LinkedHashSet(): 此构造函数用于创建默认哈希集

LinkedHashSet<E> hs = new LinkedHashSet<E>();

2.LinkedHashSet(集合C): 用于用集合C的元素初始化哈希集。

LinkedHashSet<E> hs = new LinkedHashSet<E>(Collection c);

3.LinkedHashSet(整数大小): 用于使用参数中提到的整数初始化LinkedHashSet的大小。

LinkedHashSet<E> hs = new LinkedHashSet<E>(int size);

4.LinkedHashSet(整数容量、浮点填充率): 可用于初始化容量和填充率,也称为LinkedHashSet的负载容量,参数中提到了参数。当元素的数量超过哈希集的容量时,哈希集将乘以填充率,从而扩展LinkedHash集的容量。

LinkedHashSet<E> hs = new LinkedHashSet<E>(int capacity, int fillRatio);

例子:

JAVA

// Java Program to Illustrate LinkedHashSet
// Importing required classes
import java.util.LinkedHashSet;
// Main class
// LinkedHashSetExample
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty LinkedHashSet of string type
LinkedHashSet<String> linkedset
= new LinkedHashSet<String>();
// Adding element to LinkedHashSet
// using add() method
linkedset.add( "A" );
linkedset.add( "B" );
linkedset.add( "C" );
linkedset.add( "D" );
// Note: This will not add new element
// as A already exists
linkedset.add( "A" );
linkedset.add( "E" );
// Getting size of LinkedHashSet
// using size() method
System.out.println( "Size of LinkedHashSet = "
+ linkedset.size());
System.out.println( "Original LinkedHashSet:"
+ linkedset);
// Removing existing entry from above Set
// using remove() method
System.out.println( "Removing D from LinkedHashSet: "
+ linkedset.remove( "D" ));
// Removing existing entry from above Set
// that does not exist in Set
System.out.println(
"Trying to Remove Z which is not "
+ "present: " + linkedset.remove( "Z" ));
// Checking for element whether it is present inside
// Set or not using contains() method
System.out.println( "Checking if A is present="
+ linkedset.contains( "A" ));
// Noew lastly printing the updated LinkedHashMap
System.out.println( "Updated LinkedHashSet: "
+ linkedset);
}
}


输出

Size of LinkedHashSet = 5Original LinkedHashSet:[A, B, C, D, E]Removing D from LinkedHashSet: trueTrying to Remove Z which is not present: falseChecking if A is present=trueUpdated LinkedHashSet: [A, B, C, E]

在LinkedHashSet类上执行各种操作

让我们看看如何在LinkedHashSet上执行一些常用的操作。

操作1: 添加元素

为了向LinkedHashSet添加元素,我们可以使用 添加() 方法这与HashSet不同,因为在HashSet中,插入顺序不会保留,而是保留在LinkedHashSet中。

例子:

JAVA

// Java Program to Add Elements to LinkedHashSet
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
// AddingElementsToLinkedHashSet
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty LinkedHashSet
LinkedHashSet<String> hs = new LinkedHashSet<String>();
// Adding elements to above Set
// using add() method
// Note: Insertion order is maintained
hs.add( "Geek" );
hs.add( "For" );
hs.add( "Geeks" );
// Printing elements of Set
System.out.println( "LinkedHashSet : " + hs);
}
}


输出:

LinkedHashSet : [Geek, For, Geeks]

操作2: 移除元素

可以使用 删除() 方法

例子:

JAVA

// Java program to Remove Elements from LinkedHashSet
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
// RemoveElementsFromLinkedHashSet
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty LinekdhashSet of string type
LinkedHashSet<String> hs
= new LinkedHashSet<String>();
// Adding elements to above Set
// using add() method
hs.add( "Geek" );
hs.add( "For" );
hs.add( "Geeks" );
hs.add( "A" );
hs.add( "B" );
hs.add( "Z" );
// Printing all above elements to the console
System.out.println( "Initial HashSet " + hs);
// Removing the element from above Set
hs.remove( "B" );
// Again removing the element
System.out.println( "After removing element " + hs);
// Returning false if the element is not present
System.out.println(hs.remove( "AC" ));
}
}


输出:

Initial HashSet [Geek, For, Geeks, A, B, Z]After removing element [Geek, For, Geeks, A, Z]false

操作3: 在LinkedHashSet中迭代

使用 这个 迭代器() 方法最著名的是使用 增强的for循环。

例子:

JAVA

// Java Program to Illustrate Iterating over LinkedHashSet
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
// IteratingLinkedHashSet
class GFG {
// Main driver method
public static void main(String[] args)
{
// Instantiate an object of Set
// Since LinkedHashSet implements Set
// Set points to LinkedHashSet
Set<String> hs = new LinkedHashSet<String>();
// Adding elements to above Set
// using add() method
hs.add( "Geek" );
hs.add( "For" );
hs.add( "Geeks" );
hs.add( "A" );
hs.add( "B" );
hs.add( "Z" );
// Iterating though the LinkedHashSet
// using iterators
Iterator itr = hs.iterator();
while (itr.hasNext())
System.out.print(itr.next() + ", " );
// New line
System.out.println();
// Using enhanced for loop for iteration
for (String s : hs)
System.out.print(s + ", " );
System.out.println();
}
}


输出:

Geek, For, Geeks, A, B, Z, Geek, For, Geeks, A, B, Z, 

LinkedHashSet的方法

在这里 E 是存储的元素的类型。

方法

描述

拆分器() 在此集合中的元素上创建后期绑定和快速失败拆分器。

java类中声明的方法。util。抽象集

方法

描述

等于(对象o) 将指定的对象与此集合进行相等性比较。
hashCode() 返回此集合的哈希代码值。
removeAll(集合c) 从该集合中删除指定集合中包含的所有元素(可选操作)。

java类中声明的方法。util。抽象集合

方法

描述

阿道尔​(收集c) 将指定集合中的所有元素添加到此集合(可选操作)。
康纳萨尔​(收集c) 如果此集合包含指定集合中的所有元素,则返回true。
保留​(收集c) 仅保留此集合中包含在指定集合中的元素(可选操作)。
toArray() 返回包含此集合中所有元素的数组。
托雷​(T[]a) 返回包含此集合中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。
toString() 返回此集合的字符串表示形式。

在java接口中声明的方法。util。收集

方法

描述

parallelStream() 返回一个可能与此集合并行的流作为其源。

removeIf(谓词

E> 过滤器)

删除此集合中满足给定谓词的所有元素。
流() 返回以此集合为源的连续流。

java类中声明的方法。util。哈希集

方法

描述

加(E) 如果指定的元素尚未存在,则将其添加到此集合。
清除() 从该集中删除所有元素。
克隆() 返回此HashSet实例的浅层副本:元素本身不会被克隆。
包含(对象o) 如果此集合包含指定的元素,则返回true。
isEmpty() 如果此集合不包含任何元素,则返回true。
迭代器() 返回此集合中元素的迭代器。
移除(对象o) 从该集合中删除指定的元素(如果存在)。
大小() 返回此集合中的元素数(其基数)。

在java接口中声明的方法。lang.Iterable

方法

描述

forEach(消费者

T> (行动)

对Iterable的每个元素执行给定的操作,直到所有元素都已处理或该操作引发异常。

在java接口中声明的方法。util。设置

方法 描述
添加(元素) 此方法用于向集合中添加特定元素。仅当指定的元素在集合中不存在时,函数才会添加该元素;否则,如果元素在集合中已存在,函数将返回False。
addAll(集合c) 此方法用于将上述集合中的所有元素附加到现有集合中。元素是随机添加的,不遵循任何特定顺序。
清除() 此方法用于删除集合中的所有元素,但不删除集合。集合的引用仍然存在。
包含(元素) 此方法用于检查集合中是否存在特定元素。
CONTANSALL(c系列) 此方法用于检查集合是否包含给定集合中的所有元素。如果集合包含所有元素,则此方法返回true;如果缺少任何元素,则返回false。
hashCode() 此方法用于获取集合的此实例的hashCode值。它返回一个整数值,该整数值是集合的这个实例的hashCode值。
isEmpty() 此方法用于检查集合是否为空。
迭代器() 此方法用于返回集合的迭代器。集合中的元素以随机顺序返回。
移除(元素) 此方法用于从集合中删除给定元素。如果集合中存在指定的元素,则此方法返回True,否则返回False。
removeAll(收藏) 此方法用于从集合中删除集合中存在的所有元素。如果此集合因调用而更改,则此方法返回true。
保留(收藏) 此方法用于保留给定集合中提到的集合中的所有元素。如果此集合因调用而更改,则此方法返回true。
大小() 此方法用于获取集合的大小。这将返回一个表示元素数的整数值。
toArray() 此方法用于形成与集合相同元素的数组。
托雷​(T[]a) 返回包含此集合中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。

以下是两者之间的区别 LinkedHashMap 和LinkedHashSet:

类别 LinkedHashMap LinkedHashSet
活动 Usd用于存储键值对。 用来存放物品的集合
复制品 获取唯一的密钥,无重复的密钥,但可以获取重复的值 不存储重复的元素
工具 哈希图 哈希集
实例 Map lhm=new-LinkedHashMap (); Set lhs=new LinkedhashSet ();

注: 在LinkedHashmap和LinkedHashset中保持插入顺序会带来额外的相关成本,包括额外的CPU周期和需要更多内存。如果不需要维护插入顺序,建议使用较轻的重量 哈希集 哈希图 相反

本文由 普拉蒂克·阿加瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks的主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享