荐 LinkedHashSet方法API实例演示
程序员文章站
2022-03-02 10:11:54
LinkedHashSet是HashSet的子类,底层使用hash算法计算存储位置,同时使用链表来维护顺序,顺序与添加顺序一致。在查看检索时,效率比较高。底层数据结构是链表和哈希表。1.由链表保证元素有序2.由哈希表保证元素唯一API方法实例spliterator@Testpublic void spliterator() { //spliterator: 用于对集合数据的分割与遍历 LinkedHashSet
LinkedHashSet是HashSet的子类,底层使用hash算法计算存储位置,同时使用链表来维护顺序,顺序与添加顺序一致。在查看检索时,效率比较高。
底层数据结构是链表和哈希表。
1.由链表保证元素有序
2.由哈希表保证元素唯一
API方法实例
spliterator
@Test
public void spliterator() {
//spliterator: 用于对集合数据的分割与遍历
LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("spliterator method");
Spliterator<Object> spliterator = linkedHashSet.spliterator();
spliterator.forEachRemaining(System.out :: println);
}
add
@Test
public void add() {
//add: 添加一条数据,不能重复
LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("add");
linkedHashSet.add("add");
System.out.println("linkedHashSet = " + linkedHashSet);
}
clear
@Test
public void clear() {
//clear:清除集合所有元素
LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("clear");
linkedHashSet.clear();
}
clone
@Test
public void Clone() {
//clone: 复制打印集合所有元素
LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("clone");
System.out.println("linkedHashSet.clone() = " + linkedHashSet.clone());
}
contains
@Test
public void contains() {
//contains: 判断集合是否存在指定元素,存在返回true,否则false
LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("contains");
System.out.println(linkedHashSet.contains("contains"));
}
isEmpty
@Test
public void isEmpty() {
//isEmpty: 判断集合是否为空,空则返回true,否则false
LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>();
//没有添加数据,返回为true
System.out.println("linkedHashSet.isEmpty() = " + linkedHashSet.isEmpty());
}
iterator
@Test
public void iterator() {
//iterator: 返回一个迭代器接口,用于遍历集合
LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("iterator");
Iterator<Object> iterator = linkedHashSet.iterator();
iterator.forEachRemaining(System.out :: println);
}
remove
@Test
public void remove() {
//remove: 移除集合指定元素
LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("remove");
//添加要移除的值
linkedHashSet.remove("remove");
}
size
@Test
public void size() {
//size: 返回集合长度
LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>();
//集合没有数据,返回为0
System.out.println("linkedHashSet.size() = " + linkedHashSet.size());
}
addAll
@Test
public void addAll() {
//addAll: 根据Collection集合批量添加数据
LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>();
Collection<Object> collection = new HashSet<>();
collection.add("addAll");
collection.add("addAll");
//返回为true
System.out.println(linkedHashSet.addAll(collection));
}
containsAll
@Test
public void containsAll() {
//containsAll: 根据Collection集合比较set判断内容
LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("containsAll");
Collection<Object> collection = new LinkedHashSet<>();
//添加匹配set集合的值
collection.add("containsAll");
//Collection里的值和set集合的值一直,所以判断为true
System.out.println(linkedHashSet.containsAll(collection));
}
parallelStream
@Test
public void parallelStream() {
//parallelStream: 返回一个顺序流方法
LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("parallelStream");
Stream<Object> parallelStream = linkedHashSet.parallelStream()
//调用Stream接口方法遍历集合
parallelStream.forEach(System.out::println);
}
stream
@Test
public void stream() {
//stream: 返回一个stream流接口,可对集合进行操作
LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("stream");
Stream<Object> stream = linkedHashSet.stream();
//调用Stream接口方法判断集合总数
System.out.println("stream.count() = " + stream.count());
}
本文地址:https://blog.csdn.net/weixin_44863459/article/details/107289619