E - the type of keys in the mappublic class BinaryMap<E> extends AbstractMap<E,Integer> implements Serializable
BinaryMap class implements a map from objects to
integer objects where the only value is the integer with value 1.
Instances of this class are typically returned by boolean feature
extractors, who return a map with only 1 values, because the 0
values are implicit.
Binary maps are based on a set of keys that map to 1. Thus they are more space efficient than Java's utility maps such as tree maps or hash maps. The underlying set implementation is pluggable, but must be mutable if the resulting binary map is to be mutable.
Modifiability through the entry set, key set, and values collection is fully supported through their respective iterators and through the collections themselves. The map entries making up the entry set may not have their values modified.
Map.
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>| Modifier and Type | Field and Description |
|---|---|
static Integer |
ONE
The constant used for the
Integer with value 1. |
| Constructor and Description |
|---|
BinaryMap()
Construct a binary map with an initial capacity of zero.
|
BinaryMap(int initialCapacity)
Construct a binary map with the specified initial capacity.
|
BinaryMap(Set<E> positiveSet)
Construct a binary map based on the specified set of positive
values.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E e)
Adds the specified element to the map with value 1.
|
void |
clear()
Removes all of the mappings from this map.
|
boolean |
containsKey(Object o)
Returns
true if this mapping contains a mapping
for the specified object. |
boolean |
containsValue(Object o)
Returns
true if this map contains a mapping
from some key to the specified value. |
Set<Map.Entry<E,Integer>> |
entrySet()
Return a set view of the mappings in this map.
|
Integer |
get(Object key)
Returns the
Integer with value 1 if the specified
argument is mapped to 1 by this map, and returns null
otherwise. |
boolean |
isEmpty()
Returns
true if this mapping is empty. |
Set<E> |
keySet()
Returns the set of keys mapping to 1.
|
Integer |
put(E e,
Integer n)
Adds the mapping of the specified object to the specified
value if the specified number is the
Integer with
value 1. |
Integer |
remove(Object key)
Remove the mapping with the specified key if it is present, returning
the previous value, or
null if it was previously undefined. |
int |
size()
Returns the size of this mapping.
|
Collection<Integer> |
values()
Returns an immutable collection view of the values for this
map.
|
public static final Integer ONE
Integer with value 1. The
value is defined by Integer.valueOf(1), so may be the
same object as returned by other calls to valueOf().public BinaryMap()
This is a convenience method delegating to BinaryMap(int) with an initial capacity argument of
1; see that constructor's documentation for more
information
public BinaryMap(int initialCapacity)
This is a convenience method delegating to BinaryMap(Set) with a new instance of CompactHashSet
of the specified initial capacity. Compact hash sets support
the full range of add and remove operations, but does not
support null elements.
initialCapacity - Initial size of backing array for the
binary map's entries.IllegalArgumentException - If the initial capacity is negative.OutOfMemoryError - If the JVM cannot allocate an object array with
the specified initial capacity.public boolean add(E e)
Note that this method is not part of the Map
interface.
e - Element added to the map with value 1.true if the map didn't already contain the
element.UnsupportedOperationException - If the underlying set of
positive elements does not support Collection.add.public Set<E> keySet()
If the underlying set is not modifiable, attempts to modify the key set will raise unsupported operation exceptions.
Note that results are undefined in the middle of an iterator, which will likely throw concurrent modification, null pointer, or array index out of bounds exceptions. Therefore, access to the key set must be synchronized in the same way access to the underlying set.
Further note that unlike the specification in Map,
the returned keyset supports the Collection.add(Object) and
Collection.addAll(Collection). Adding elements to the key set
is the same as adding them through the add(Object)
method of this class.
public Set<Map.Entry<E,Integer>> entrySet()
Iterator.remove, Collection.remove,
Collection.removeAll, Collection.retainAll,
and Collection.clear operations, but does not support
the add operations.
If the underlying set of positive elements does not support these modification operations, they will throw an unsupported operation exception.
Implementation Note: The Map.Entry elements
are created as necessary by the entry set using a relatively
efficient implementation of entries that only stores the key.
Accessing the key set is more efficient.
public Integer get(Object key)
Integer with value 1 if the specified
argument is mapped to 1 by this map, and returns null
otherwise.
The constant ONE is used for the return
value.
public Integer remove(Object key)
null if it was previously undefined.remove in interface Map<E,Integer>remove in class AbstractMap<E,Integer>key - Key of mapping to remove.null otherwise.UnsupportedOperationException - If the underlying set for
this map does not support the Set.remove(Object) operation.public int size()
public Collection<Integer> values()
ONE if the map is
not empty.public void clear()
The implementation just delegates the clear operation to the contained set.
clear in interface Map<E,Integer>clear in class AbstractMap<E,Integer>UnsupportedOperationException - If the clear operation
is not supported by the contained set.public boolean containsKey(Object o)
true if this mapping contains a mapping
for the specified object.
This method delegates to the underlying positive set's
Collection.contains method.
containsKey in interface Map<E,Integer>containsKey in class AbstractMap<E,Integer>o - Object to teset.true if it is mapped by this mapping.ClassCastException - If the underlying set throws
a class cast when checking the specified object for
membership.public boolean containsValue(Object o)
true if this map contains a mapping
from some key to the specified value.
Note that the only object for which this map may
return true is the Integer with value 1.
containsValue in interface Map<E,Integer>containsValue in class AbstractMap<E,Integer>o - Object to test.true if this map contains a mapping
from some object to this value.public boolean isEmpty()
true if this mapping is empty.public Integer put(E e, Integer n)
Integer with
value 1.put in interface Map<E,Integer>put in class AbstractMap<E,Integer>e - Key for the mapping.n - Value for the mapping.IllegalArgumentException - If the specified integer
does not have value 1.Copyright © 2016 Alias-i, Inc.. All rights reserved.