Abstract class map java

Java AbstractMap tutorial with examples

This class provides a skeletal implementation of the Map interface, to minimize the effort required to implement this interface.

Introduction

This class provides a skeletal implementation of the Map interface, to minimize the effort required to implement this interface.

To implement an unmodifiable map, the programmer needs only to extend this class and provide an implementation for the entrySet method, which returns a set-view of the map’s mappings.

Typically, the returned set will, in turn, be implemented atop AbstractSet.

This set should not support the add or remove methods, and its iterator should not support the remove method.

To implement a modifiable map, the programmer must additionally override this class’s put method (which otherwise throws an UnsupportedOperationException), and the iterator returned by entrySet().iterator() must additionally implement its remove method.

The programmer should generally provide a void (no argument) and map constructor, as per the recommendation in the Map interface specification.

The documentation for each non-abstract method in this class describes its implementation in detail.

Each of these methods may be overridden if the map being implemented admits a more efficient implementation.

Example

The following code shows how to use AbstractMap from java.util.

import java.util.AbstractMap; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map.Entry; import java.util.Set; class SlowMap extends AbstractMap private List keys = new ArrayList(), values = new ArrayList(); public Object put(Object key, Object value) < Object result = get(key); if (!keys.contains(key)) < keys.add(key);// w w w . d e m o 2 s . c o m values.add(value); > else values.set(keys.indexOf(key), value); return result; > public Object get(Object key) < if (!keys.contains(key)) return null; return values.get(keys.indexOf(key)); > public Set entrySet() < Set entries = new HashSet(); Iterator ki = keys.iterator(), vi = values.iterator(); while (ki.hasNext()) entries.add(new MPair(ki.next(), vi.next())); return entries; > public String toString() < StringBuffer s = new StringBuffer("); Iterator ki = keys.iterator(), vi = values.iterator(); while (ki.hasNext()) < s.append(ki.next() + " , "); > s.append(">"); return s.toString(); > public static void main(String[] args) < SlowMap m = new SlowMap(); m.put("Adobe", "Mountain View, CA"); m.put("IBM", "White Plains, NY"); m.put("Learning Tree", "Los Angeles, CA"); m.put("Microsoft", "Redmond, WA"); m.put("Netscape", "Mountain View, CA"); m.put("O'Reilly", "Sebastopol, CA"); m.put("Sun", "Mountain View, CA"); System.out.println(m); > > ///:~ class MPair implements Entry, Comparable < Object key, value; MPair(Object k, Object v) < key = k; value = v; >public Object getKey() < return key; > public Object getValue() < return value; > public Object setValue(Object v) < Object result = value; value = v; return result; > public boolean equals(Object o) < return key.equals(((MPair) o).key); > public int compareTo(Object rv) < return ((Comparable) key).compareTo(((MPair) rv).key); > > ///:~ 

  • Java AbstractList iterator() Returns an iterator over the elements in this list in proper sequence.
  • Java AbstractList addAll(Collection
  • Java java.util AbstractMap
  • Java AbstractMap tutorial with examples
  • Java AbstractMap put(K key, V value)
  • Java AbstractMap clear()
  • Java AbstractMap containsKey(Object key)

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

Abstract class map java

This class provides a skeletal implementation of the Map interface, to minimize the effort required to implement this interface. To implement an unmodifiable map, the programmer needs only to extend this class and provide an implementation for the entrySet method, which returns a set-view of the map’s mappings. Typically, the returned set will, in turn, be implemented atop AbstractSet. This set should not support the add or remove methods, and its iterator should not support the remove method. To implement a modifiable map, the programmer must additionally override this class’s put method (which otherwise throws an UnsupportedOperationException), and the iterator returned by entrySet().iterator() must additionally implement its remove method. The programmer should generally provide a void (no argument) and map constructor, as per the recommendation in the Map interface specification. The documentation for each non-abstract method in this class describes its implementation in detail. Each of these methods may be overridden if the map being implemented admits a more efficient implementation. This class is a member of the Java Collections Framework.

Nested Class Summary

Nested classes/interfaces inherited from interface java.util.Map

Constructor Summary

Method Summary

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Methods inherited from class java.lang.Object

Methods inherited from interface java.util.Map

Constructor Detail

AbstractMap

Method Detail

size

Returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

isEmpty

containsValue

Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of the Map interface.

containsKey

Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)

get

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. More formally, if this map contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)) , then this method returns v ; otherwise it returns null . (There can be at most one such mapping.) If this map permits null values, then a return value of null does not necessarily indicate that the map contains no mapping for the key; it’s also possible that the map explicitly maps the key to null . The containsKey operation may be used to distinguish these two cases.

put

Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)

remove

Removes the mapping for a key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that (key==null ? k==null : key.equals(k)) , that mapping is removed. (The map can contain at most one such mapping.) Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key. If this map permits null values, then a return value of null does not necessarily indicate that the map contained no mapping for the key; it’s also possible that the map explicitly mapped the key to null. The map will not contain a mapping for the specified key once the call returns.

putAll

Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.

clear

Removes all of the mappings from this map (optional operation). The map will be empty after this call returns.

keySet

Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator’s own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

values

Returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress (except through the iterator’s own remove operation), the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

entrySet

Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator’s own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

equals

Compares the specified object with this map for equality. Returns true if the given object is also a map and the two maps represent the same mappings. More formally, two maps m1 and m2 represent the same mappings if m1.entrySet().equals(m2.entrySet()). This ensures that the equals method works properly across different implementations of the Map interface.

hashCode

Returns the hash code value for this map. The hash code of a map is defined to be the sum of the hash codes of each entry in the map’s entrySet() view. This ensures that m1.equals(m2) implies that m1.hashCode()==m2.hashCode() for any two maps m1 and m2, as required by the general contract of Object.hashCode() .

toString

clone

protected Object clone() throws CloneNotSupportedException

Источник

Читайте также:  Add html into html
Оцените статью