mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 07:14:30 +02:00
8266571: Sequenced Collections
Reviewed-by: alanb
This commit is contained in:
parent
bad6aa68e4
commit
17ce0976e4
42 changed files with 7060 additions and 150 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -27,15 +27,19 @@ package java.util;
|
|||
|
||||
/**
|
||||
* <p>Hash table and linked list implementation of the {@code Set} interface,
|
||||
* with predictable iteration order. This implementation differs from
|
||||
* with well-defined encounter order. This implementation differs from
|
||||
* {@code HashSet} in that it maintains a doubly-linked list running through
|
||||
* all of its entries. This linked list defines the iteration ordering,
|
||||
* which is the order in which elements were inserted into the set
|
||||
* (<i>insertion-order</i>). Note that insertion order is <i>not</i> affected
|
||||
* if an element is <i>re-inserted</i> into the set. (An element {@code e}
|
||||
* is reinserted into a set {@code s} if {@code s.add(e)} is invoked when
|
||||
* {@code s.contains(e)} would return {@code true} immediately prior to
|
||||
* the invocation.)
|
||||
* all of its entries. This linked list defines the encounter order (iteration
|
||||
* order), which is the order in which elements were inserted into the set
|
||||
* (<i>insertion-order</i>). The least recently inserted element (the eldest) is
|
||||
* first, and the youngest element is last. Note that encounter order is <i>not</i> affected
|
||||
* if an element is <i>re-inserted</i> into the set with the {@code add} method.
|
||||
* (An element {@code e} is reinserted into a set {@code s} if {@code s.add(e)} is
|
||||
* invoked when {@code s.contains(e)} would return {@code true} immediately prior to
|
||||
* the invocation.) The reverse-ordered view of this set is in the opposite order, with
|
||||
* the youngest element appearing first and the eldest element appearing last. The encounter
|
||||
* order of elements already in the set can be changed by using the
|
||||
* {@link #addFirst addFirst} and {@link #addLast addLast} methods.
|
||||
*
|
||||
* <p>This implementation spares its clients from the unspecified, generally
|
||||
* chaotic ordering provided by {@link HashSet}, without incurring the
|
||||
|
@ -53,8 +57,8 @@ package java.util;
|
|||
* the copy. (Clients generally appreciate having things returned in the same
|
||||
* order they were presented.)
|
||||
*
|
||||
* <p>This class provides all of the optional {@code Set} operations, and
|
||||
* permits null elements. Like {@code HashSet}, it provides constant-time
|
||||
* <p>This class provides all of the optional {@link Set} and {@link SequencedSet}
|
||||
* operations, and it permits null elements. Like {@code HashSet}, it provides constant-time
|
||||
* performance for the basic operations ({@code add}, {@code contains} and
|
||||
* {@code remove}), assuming the hash function disperses elements
|
||||
* properly among the buckets. Performance is likely to be just slightly
|
||||
|
@ -117,7 +121,7 @@ package java.util;
|
|||
|
||||
public class LinkedHashSet<E>
|
||||
extends HashSet<E>
|
||||
implements Set<E>, Cloneable, java.io.Serializable {
|
||||
implements SequencedSet<E>, Cloneable, java.io.Serializable {
|
||||
|
||||
@java.io.Serial
|
||||
private static final long serialVersionUID = -2851667679971038690L;
|
||||
|
@ -221,4 +225,100 @@ public class LinkedHashSet<E>
|
|||
return new LinkedHashSet<>(HashMap.calculateHashMapCapacity(numElements));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
LinkedHashMap<E, Object> map() {
|
||||
return (LinkedHashMap<E, Object>) map;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>
|
||||
* If this set already contains the element, it is relocated if necessary so that it is
|
||||
* first in encounter order.
|
||||
*
|
||||
* @since 21
|
||||
*/
|
||||
public void addFirst(E e) {
|
||||
map().putFirst(e, PRESENT);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>
|
||||
* If this set already contains the element, it is relocated if necessary so that it is
|
||||
* last in encounter order.
|
||||
*
|
||||
* @since 21
|
||||
*/
|
||||
public void addLast(E e) {
|
||||
map().putLast(e, PRESENT);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws NoSuchElementException {@inheritDoc}
|
||||
* @since 21
|
||||
*/
|
||||
public E getFirst() {
|
||||
return map().sequencedKeySet().getFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws NoSuchElementException {@inheritDoc}
|
||||
* @since 21
|
||||
*/
|
||||
public E getLast() {
|
||||
return map().sequencedKeySet().getLast();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws NoSuchElementException {@inheritDoc}
|
||||
* @since 21
|
||||
*/
|
||||
public E removeFirst() {
|
||||
return map().sequencedKeySet().removeFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws NoSuchElementException {@inheritDoc}
|
||||
* @since 21
|
||||
*/
|
||||
public E removeLast() {
|
||||
return map().sequencedKeySet().removeLast();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>
|
||||
* Modifications to the reversed view are permitted and will be propagated to this set.
|
||||
* In addition, modifications to this set will be visible in the reversed view.
|
||||
*
|
||||
* @return {@inheritDoc}
|
||||
* @since 21
|
||||
*/
|
||||
public SequencedSet<E> reversed() {
|
||||
class ReverseLinkedHashSetView extends AbstractSet<E> implements SequencedSet<E> {
|
||||
public int size() { return LinkedHashSet.this.size(); }
|
||||
public Iterator<E> iterator() { return map().sequencedKeySet().reversed().iterator(); }
|
||||
public boolean add(E e) { return LinkedHashSet.this.add(e); }
|
||||
public void addFirst(E e) { LinkedHashSet.this.addLast(e); }
|
||||
public void addLast(E e) { LinkedHashSet.this.addFirst(e); }
|
||||
public E getFirst() { return LinkedHashSet.this.getLast(); }
|
||||
public E getLast() { return LinkedHashSet.this.getFirst(); }
|
||||
public E removeFirst() { return LinkedHashSet.this.removeLast(); }
|
||||
public E removeLast() { return LinkedHashSet.this.removeFirst(); }
|
||||
public SequencedSet<E> reversed() { return LinkedHashSet.this; }
|
||||
public Object[] toArray() { return map().keysToArray(new Object[map.size()], true); }
|
||||
public <T> T[] toArray(T[] a) { return map().keysToArray(map.prepareArray(a), true); }
|
||||
}
|
||||
|
||||
return new ReverseLinkedHashSetView();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue