/** * Provides a string representation of the entries within the map. The * format of the returned string may change with different releases, so this * method is suitable for debugging purposes only. If a specific format is * required, use {@link #entrySet()}.{@link Set#iterator() iterator()} and * iterate over the entries in the map formatting them as appropriate. **/ public String toString() { StringBuffer buf = new StringBuffer(); buf.append('['); for(Entry pos = sentinel.next; pos != sentinel; pos = pos.next) { buf.append(pos.getKey()); buf.append('='); buf.append(pos.getValue()); if(pos.next != sentinel) { buf.append(','); } } buf.append(']'); return buf.toString(); }
/** * Removes the last element returned from the {@link #next()} method from * the sequenced map. * * @exception IllegalStateException if there isn't a "last element" to be * removed. That is, if {@link #next()} has never been called, or if * {@link #remove()} was already called on the element. * * @exception ConcurrentModificationException if a modification occurs in * the underlying map. **/ public void remove() { if((returnType & REMOVED_MASK) != 0) { throw new IllegalStateException("remove() must follow next()"); } if(modCount != expectedModCount) { throw new ConcurrentModificationException(); } SequencedHashMap.this.removeImpl(pos.getKey()); // update the expected mod count for the remove operation expectedModCount++; // set the removed flag returnType = returnType | REMOVED_MASK; } }
/** * Serializes this map to the given stream. * * @param out the stream to serialize to * @throws IOException if the stream raises it */ public void writeExternal( ObjectOutput out ) throws IOException { out.writeInt(size()); for(Entry pos = sentinel.next; pos != sentinel; pos = pos.next) { out.writeObject(pos.getKey()); out.writeObject(pos.getValue()); } }
switch(returnType) { case KEY: return pos.getKey(); case VALUE: return pos.getValue();
/** * Return the key for the "newest" mapping. That is, return the key for the * mapping that was last put into the map when compared to all the other * objects in the map. This behavior is equivalent to using * <code>getLast().getKey()</code>, but this method provides a slightly * optimized implementation. * * @return The last key in the sequence, or <code>null</code> if the map is * empty. **/ public Object getLastKey() { // sentinel.prev points to the "last" element of the sequence -- the tail // of the list -- and the requisite key is returned from it. An empty list // does not need to be tested. In cases where the list is empty, // sentinel.prev will point to the sentinel itself which has a null key, // which is exactly what we would want to return if the list is empty (a // nice convient way to avoid test for an empty list) return sentinel.prev.getKey(); }
/** * Return the key for the "oldest" mapping. That is, return the key for the * mapping that was first put into the map when compared to all the other * objects in the map. This behavior is equivalent to using * <code>getFirst().getKey()</code>, but this method provides a slightly * optimized implementation. * * @return The first key in the sequence, or <code>null</code> if the * map is empty. **/ public Object getFirstKey() { // sentinel.next points to the "first" element of the sequence -- the head // of the list -- and the requisite key is returned from it. An empty list // does not need to be tested. In cases where the list is empty, // sentinel.next will point to the sentinel itself which has a null key, // which is exactly what we would want to return if the list is empty (a // nice convient way to avoid test for an empty list) return sentinel.next.getKey(); }
public boolean remove(Object value) { // do null comparison outside loop so we only need to do it once. This // provides a tighter, more efficient loop at the expense of slight // code duplication. if(value == null) { for(Entry pos = sentinel.next; pos != sentinel; pos = pos.next) { if(pos.getValue() == null) { SequencedHashMap.this.removeImpl(pos.getKey()); return true; } } } else { for(Entry pos = sentinel.next; pos != sentinel; pos = pos.next) { if(value.equals(pos.getValue())) { SequencedHashMap.this.removeImpl(pos.getKey()); return true; } } } return false; }
public boolean equals(Object obj) { if(obj == null) return false; if(obj == this) return true; if(!(obj instanceof Map.Entry)) return false; Map.Entry other = (Map.Entry)obj; // implemented per api docs for Map.Entry.equals(Object) return((getKey() == null ? other.getKey() == null : getKey().equals(other.getKey())) && (getValue() == null ? other.getValue() == null : getValue().equals(other.getValue()))); } public String toString() {
public String toString() { return "[" + getKey() + "=" + getValue() + "]"; } }
public boolean remove(Object o) { Entry e = findEntry(o); if(e == null) return false; return SequencedHashMap.this.removeImpl(e.getKey()) != null; }