/** * Compares the specified object with this entry for equality. * Returns true if the given object is also a map entry and * the two entries represent the same mapping. * * @param obj the object to be compared for equality with this entry. * @return true if the specified object is equal to this entry. */ @Override public boolean equals(final Object obj) { if (obj == this) { return true; } if (!(obj instanceof Map.Entry)) { return false; } final Map.Entry<?, ?> e = (Map.Entry<?, ?>) obj; return getKey().equals(e.getKey()) && getValue().equals(e.getValue()); }
if (node == null) { final Node<K, V> root = new Node<>(key, value); rootNode[KEY.ordinal()] = root; rootNode[VALUE.ordinal()] = root; final int cmp = compare(key, node.getKey()); node = node.getLeft(KEY); } else { final Node<K, V> newNode = new Node<>(key, value); node = node.getRight(KEY); } else { final Node<K, V> newNode = new Node<>(key, value);
/** * Returns the key to which this map maps the specified value. * Returns null if the map contains no mapping for this value. * <p> * The value must implement <code>Comparable</code>. * * @param value value whose associated key is to be returned. * @return the key to which this map maps the specified value, * or null if the map contains no mapping for this value. * @throws ClassCastException if the value is of an inappropriate type * @throws NullPointerException if the value is null */ @Override public K getKey(final Object value) { checkValue(value); final Node<K, V> node = lookupValue(value); return node == null ? null : node.getKey(); }
/** * Gets the value to which this map maps the specified key. * Returns null if the map contains no mapping for this key. * <p> * The key must implement <code>Comparable</code>. * * @param key key whose associated value is to be returned * @return the value to which this map maps the specified key, * or null if the map contains no mapping for this key * @throws ClassCastException if the key is of an inappropriate type * @throws NullPointerException if the key is null */ @Override public V get(final Object key) { checkKey(key); final Node<K, V> node = lookupKey(key); return node == null ? null : node.getValue(); }
/** * Gets the next key after the one specified. * <p> * The key must implement <code>Comparable</code>. * * @param key the key to search for next from * @return the next key, null if no match or at end */ @Override public K nextKey(final K key) { checkKey(key); final Node<K, V> node = nextGreater(lookupKey(key), KEY); return node == null ? null : node.getKey(); }
/** * Gets the previous key before the one specified. * <p> * The key must implement <code>Comparable</code>. * * @param key the key to search for previous from * @return the previous key, null if no match or at start */ @Override public K previousKey(final K key) { checkKey(key); final Node<K, V> node = nextSmaller(lookupKey(key), KEY); return node == null ? null : node.getKey(); }
/** * Gets the last (highest) key currently in this map. * * @return the last (highest) key currently in this sorted map * @throws NoSuchElementException if this map is empty */ @Override public K lastKey() { if (nodeCount == 0) { throw new NoSuchElementException("Map is empty"); } return greatestNode(rootNode[KEY.ordinal()], KEY).getKey(); }
private K doRemoveValue(final Object value) { final Node<K, V> node = lookupValue(value); if (node == null) { return null; } doRedBlackDelete(node); return node.getKey(); }
private V doRemoveKey(final Object key) { final Node<K, V> node = lookupKey(key); if (node == null) { return null; } doRedBlackDelete(node); return node.getValue(); }
/** * Gets the first (lowest) key currently in this map. * * @return the first (lowest) key currently in this sorted map * @throws NoSuchElementException if this map is empty */ @Override public K firstKey() { if (nodeCount == 0) { throw new NoSuchElementException("Map is empty"); } return leastNode(rootNode[KEY.ordinal()], KEY).getKey(); }