@Override public int length() { return map.size(); }
/** * Write an object to a serialization stream. * * @param s An object serialization stream. * @throws IOException If an error occurs writing to the stream. */ private void writeObject(ObjectOutputStream s) throws IOException { s.defaultWriteObject(); s.writeInt(map.size()); for (Tuple2<T, Object> e : map) { s.writeObject(e._1); } }
/** * Adds all of the given elements to this set, replacing existing one if they are not already contained. * <p> * Note that this method has a worst-case quadratic complexity. * * @param elements The elements to be added. * @return A new set containing all elements of this set and the given {@code elements}, if not already contained. */ @Override public LinkedHashSet<T> addAll(Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); if (isEmpty() && elements instanceof LinkedHashSet) { @SuppressWarnings("unchecked") final LinkedHashSet<T> set = (LinkedHashSet<T>) elements; return set; } final LinkedHashMap<T, Object> that = addAll(map, elements); if (that.size() == map.size()) { return this; } else { return new LinkedHashSet<>(that); } }
@Override public LinkedHashSet<T> takeRight(int n) { if (map.size() <= n) { return this; } return LinkedHashSet.ofAll(() -> iterator().takeRight(n)); }
@Override public LinkedHashSet<T> take(int n) { if (map.size() <= n) { return this; } return LinkedHashSet.ofAll(() -> iterator().take(n)); }
@Override public LinkedHashMap<K, V> removeAll(Iterable<? extends K> keys) { Objects.requireNonNull(keys, "keys is null"); final HashSet<K> toRemove = HashSet.ofAll(keys); final Queue<Tuple2<K, V>> newList = list.filter(t -> !toRemove.contains(t._1)); final HashMap<K, V> newMap = map.filter(t -> !toRemove.contains(t._1)); return newList.size() == size() ? this : wrap(newList, newMap); }
@Override public int length() { return map.size(); }
/** * Write an object to a serialization stream. * * @param s An object serialization stream. * @throws IOException If an error occurs writing to the stream. */ private void writeObject(ObjectOutputStream s) throws IOException { s.defaultWriteObject(); s.writeInt(map.size()); for (Tuple2<T, Object> e : map) { s.writeObject(e._1); } }
/** * Adds all of the given elements to this set, replacing existing one if they are not already contained. * <p> * Note that this method has a worst-case quadratic complexity. * * @param elements The elements to be added. * @return A new set containing all elements of this set and the given {@code elements}, if not already contained. */ @Override public LinkedHashSet<T> addAll(Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); if (isEmpty() && elements instanceof LinkedHashSet) { @SuppressWarnings("unchecked") final LinkedHashSet<T> set = (LinkedHashSet<T>) elements; return set; } final LinkedHashMap<T, Object> that = addAll(map, elements); if (that.size() == map.size()) { return this; } else { return new LinkedHashSet<>(that); } }
@Override public LinkedHashSet<T> take(int n) { if (map.size() <= n) { return this; } return LinkedHashSet.ofAll(() -> iterator().take(n)); }
@Override public LinkedHashSet<T> takeRight(int n) { if (map.size() <= n) { return this; } return LinkedHashSet.ofAll(() -> iterator().takeRight(n)); }
@Override public LinkedHashMap<K, V> removeAll(Iterable<? extends K> keys) { Objects.requireNonNull(keys, "keys is null"); final HashSet<K> toRemove = HashSet.ofAll(keys); final Queue<Tuple2<K, V>> newList = list.filter(t -> !toRemove.contains(t._1)); final HashMap<K, V> newMap = map.filter(t -> !toRemove.contains(t._1)); return newList.size() == size() ? this : wrap(newList, newMap); }