Java Doc for TreeMap.java in  » 6.0-JDK-Modules » j2me » java » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » java.util 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   java.util.AbstractMap
      java.util.TreeMap

TreeMap
public class TreeMap extends AbstractMap implements SortedMap,Cloneable,java.io.Serializable(Code)
Red-Black tree based implementation of the SortedMap interface. This class guarantees that the map will be in ascending key order, sorted according to the natural order for the key's class (see Comparable), or by the comparator provided at creation time, depending on which constructor is used.

This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations. Algorithms are adaptations of those in Cormen, Leiserson, and Rivest's Introduction to Algorithms.

Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation, but a map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. The behavior of a sorted map is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Map interface.

Note that this implementation is not synchronized. If multiple threads access a map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with an existing key is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:

 Map m = Collections.synchronizedMap(new TreeMap(...));
 

The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator throws a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

This class is a member of the Java Collections Framework.
author:
   Josh Bloch and Doug Lea
version:
   1.43, 02/02/00
See Also:   Map
See Also:   HashMap
See Also:   Hashtable
See Also:   Comparable
See Also:   Comparator
See Also:   Collection
See Also:   Collections.synchronizedMap(Map)
since:
   1.2


Inner Class :static class Entry implements Map.Entry


Constructor Summary
public  TreeMap()
     Constructs a new, empty map, sorted according to the keys' natural order.
public  TreeMap(Comparator c)
     Constructs a new, empty map, sorted according to the given comparator. All keys inserted into the map must be mutually comparable by the given comparator: comparator.compare(k1, k2) must not throw a ClassCastException for any keys k1 and k2 in the map.
public  TreeMap(Map m)
     Constructs a new map containing the same mappings as the given map, sorted according to the keys' natural order.
public  TreeMap(SortedMap m)
     Constructs a new map containing the same mappings as the given SortedMap, sorted according to the same ordering.

Method Summary
 voidaddAllForTreeSet(SortedSet set, Object defaultVal)
    
public  voidclear()
     Removes all mappings from this TreeMap.
public  Objectclone()
     Returns a shallow copy of this TreeMap instance.
public  Comparatorcomparator()
     Returns the comparator used to order this map, or null if this map uses its keys' natural order.
public  booleancontainsKey(Object key)
     Returns true if this map contains a mapping for the specified key.
Parameters:
  key - key whose presence in this map is to be tested.
public  booleancontainsValue(Object value)
     Returns true if this map maps one or more keys to the specified value.
public  SetentrySet()
     Returns a set view of the mappings contained in this map.
public  ObjectfirstKey()
     Returns the first (lowest) key currently in this sorted map.
public  Objectget(Object key)
     Returns the value to which this map maps the specified key.
public  SortedMapheadMap(Object toKey)
     Returns a view of the portion of this map whose keys are strictly less than toKey.
public  SetkeySet()
     Returns a Set view of the keys contained in this map.
public  ObjectlastKey()
     Returns the last (highest) key currently in this sorted map.
public  Objectput(Object key, Object value)
     Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.
Parameters:
  key - key with which the specified value is to be associated.
Parameters:
  value - value to be associated with the specified key.
public  voidputAll(Map map)
     Copies all of the mappings from the specified map to this map.
 voidreadTreeSet(int size, java.io.ObjectInputStream s, Object defaultVal)
    
public  Objectremove(Object key)
     Removes the mapping for this key from this TreeMap if present.
Parameters:
  key - key for which mapping should be removed previous value associated with specified key, or nullif there was no mapping for key.
public  intsize()
     Returns the number of key-value mappings in this map.
public  SortedMapsubMap(Object fromKey, Object toKey)
     Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
public  SortedMaptailMap(Object fromKey)
     Returns a view of the portion of this map whose keys are greater than or equal to fromKey.
public  Collectionvalues()
     Returns a collection view of the values contained in this map.


Constructor Detail
TreeMap
public TreeMap()(Code)
Constructs a new, empty map, sorted according to the keys' natural order. All keys inserted into the map must implement the Comparable interface. Furthermore, all such keys must be mutually comparable: k1.compareTo(k2) must not throw a ClassCastException for any elements k1 and k2 in the map. If the user attempts to put a key into the map that violates this constraint (for example, the user attempts to put a string key into a map whose keys are integers), the put(Object key, Object value) call will throw a ClassCastException.
See Also:   Comparable



TreeMap
public TreeMap(Comparator c)(Code)
Constructs a new, empty map, sorted according to the given comparator. All keys inserted into the map must be mutually comparable by the given comparator: comparator.compare(k1, k2) must not throw a ClassCastException for any keys k1 and k2 in the map. If the user attempts to put a key into the map that violates this constraint, the put(Object key, Object value) call will throw a ClassCastException.
Parameters:
  c - the comparator that will be used to sort this map. Anull value indicates that the keys' naturalordering should be used.



TreeMap
public TreeMap(Map m)(Code)
Constructs a new map containing the same mappings as the given map, sorted according to the keys' natural order. All keys inserted into the new map must implement the Comparable interface. Furthermore, all such keys must be mutually comparable: k1.compareTo(k2) must not throw a ClassCastException for any elements k1 and k2 in the map. This method runs in n*log(n) time.
Parameters:
  m - the map whose mappings are to be placed in this map.
throws:
  ClassCastException - the keys in t are not Comparable, orare not mutually comparable.
throws:
  NullPointerException - if the specified map is null.



TreeMap
public TreeMap(SortedMap m)(Code)
Constructs a new map containing the same mappings as the given SortedMap, sorted according to the same ordering. This method runs in linear time.
Parameters:
  m - the sorted map whose mappings are to be placed in this map,and whose comparator is to be used to sort this map.
throws:
  NullPointerException - if the specified sorted map is null.




Method Detail
addAllForTreeSet
void addAllForTreeSet(SortedSet set, Object defaultVal)(Code)
Intended to be called only from TreeSet.addAll *



clear
public void clear()(Code)
Removes all mappings from this TreeMap.



clone
public Object clone()(Code)
Returns a shallow copy of this TreeMap instance. (The keys and values themselves are not cloned.) a shallow copy of this Map.



comparator
public Comparator comparator()(Code)
Returns the comparator used to order this map, or null if this map uses its keys' natural order. the comparator associated with this sorted map, ornull if it uses its keys' natural sort method.



containsKey
public boolean containsKey(Object key)(Code)
Returns true if this map contains a mapping for the specified key.
Parameters:
  key - key whose presence in this map is to be tested. true if this map contains a mapping for thespecified key.
throws:
  ClassCastException - if the key cannot be compared with the keyscurrently in the map.
throws:
  NullPointerException - key is null and this map usesnatural ordering, or its comparator does not toleratenull keys.



containsValue
public boolean containsValue(Object value)(Code)
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 Map.
Parameters:
  value - value whose presence in this Map is to be tested. true if a mapping to value exists;false otherwise.
since:
   1.2



entrySet
public Set entrySet()(Code)
Returns a set view of the mappings contained in this map. The set's iterator returns the mappings in ascending key order. Each element in the returned set is a Map.Entry. The set is backed by this map, so changes to this map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from the TreeMap, through the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations. a set view of the mappings contained in this map.
See Also:   Map.Entry



firstKey
public Object firstKey()(Code)
Returns the first (lowest) key currently in this sorted map. the first (lowest) key currently in this sorted map.
throws:
  NoSuchElementException - Map is empty.



get
public Object get(Object key)(Code)
Returns the value to which this map maps the specified key. Returns null if the map contains no mapping for this key. 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.
Parameters:
  key - key whose associated value is to be returned. the value to which this map maps the specified key, ornull if the map contains no mapping for the key.
throws:
  ClassCastException - key cannot be compared with the keyscurrently in the map.
throws:
  NullPointerException - key is null and this map usesnatural ordering, or its comparator does not toleratenull keys.
See Also:   TreeMap.containsKey(Object)



headMap
public SortedMap headMap(Object toKey)(Code)
Returns a view of the portion of this map whose keys are strictly less than toKey. The returned sorted map is backed by this map, so changes in the returned sorted map are reflected in this map, and vice-versa. The returned sorted map supports all optional map operations.

The sorted map returned by this method will throw an IllegalArgumentException if the user attempts to insert a key greater than or equal to toKey.

Note: this method always returns a view that does not contain its (high) endpoint. If you need a view that does contain this endpoint, and the key type allows for calculation of the successor a given key, merely request a headMap bounded by successor(highEndpoint). For example, suppose that suppose that m is a sorted map whose keys are strings. The following idiom obtains a view containing all of the key-value mappings in m whose keys are less than or equal to high:

 SortedMap head = m.headMap(high+"\0");
 

Parameters:
  toKey - high endpoint (exclusive) of the headMap. a view of the portion of this map whose keys are strictlyless than toKey.
throws:
  ClassCastException - if toKey is not compatiblewith this map's comparator (or, if the map has no comparator,if toKey does not implement Comparable).
throws:
  IllegalArgumentException - if this map is itself a subMap,headMap, or tailMap, and toKey is not within thespecified range of the subMap, headMap, or tailMap.
throws:
  NullPointerException - if toKey is null andthis map uses natural order, or its comparator does nottolerate null keys.



keySet
public Set keySet()(Code)
Returns a Set view of the keys contained in this map. The set's iterator will return the keys in ascending order. The map is backed by this TreeMap instance, so changes to this map are reflected in the Set, and vice-versa. 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. a set view of the keys contained in this TreeMap.



lastKey
public Object lastKey()(Code)
Returns the last (highest) key currently in this sorted map. the last (highest) key currently in this sorted map.
throws:
  NoSuchElementException - Map is empty.



put
public Object put(Object key, Object value)(Code)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.
Parameters:
  key - key with which the specified value is to be associated.
Parameters:
  value - value to be associated with the specified key. previous value associated with specified key, or nullif there was no mapping for key. A null return canalso indicate that the map previously associated nullwith the specified key.
throws:
  ClassCastException - key cannot be compared with the keyscurrently in the map.
throws:
  NullPointerException - key is null and this map usesnatural order, or its comparator does not toleratenull keys.



putAll
public void putAll(Map map)(Code)
Copies all of the mappings from the specified map to this map. These mappings replace any mappings that this map had for any of the keys currently in the specified map.
Parameters:
  map - mappings to be stored in this map.
throws:
  ClassCastException - class of a key or value in the specifiedmap prevents it from being stored in this map.
throws:
  NullPointerException - if the given map is null orthis map does not permit null keys and a key in the specified map is null.



readTreeSet
void readTreeSet(int size, java.io.ObjectInputStream s, Object defaultVal) throws java.io.IOException, ClassNotFoundException(Code)
Intended to be called only from TreeSet.readObject *



remove
public Object remove(Object key)(Code)
Removes the mapping for this key from this TreeMap if present.
Parameters:
  key - key for which mapping should be removed previous value associated with specified key, or nullif there was no mapping for key. A null return canalso indicate that the map previously associatednull with the specified key.
throws:
  ClassCastException - key cannot be compared with the keyscurrently in the map.
throws:
  NullPointerException - key is null and this map usesnatural order, or its comparator does not toleratenull keys.



size
public int size()(Code)
Returns the number of key-value mappings in this map. the number of key-value mappings in this map.



subMap
public SortedMap subMap(Object fromKey, Object toKey)(Code)
Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive. (If fromKey and toKey are equal, the returned sorted map is empty.) The returned sorted map is backed by this map, so changes in the returned sorted map are reflected in this map, and vice-versa. The returned sorted map supports all optional map operations.

The sorted map returned by this method will throw an IllegalArgumentException if the user attempts to insert a key less than fromKey or greater than or equal to toKey.

Note: this method always returns a half-open range (which includes its low endpoint but not its high endpoint). If you need a closed range (which includes both endpoints), and the key type allows for calculation of the successor a given key, merely request the subrange from lowEndpoint to successor(highEndpoint). For example, suppose that m is a sorted map whose keys are strings. The following idiom obtains a view containing all of the key-value mappings in m whose keys are between low and high, inclusive:

    SortedMap sub = m.submap(low, high+"\0");
A similar technique can be used to generate an open range (which contains neither endpoint). The following idiom obtains a view containing all of the key-value mappings in m whose keys are between low and high, exclusive:
    SortedMap sub = m.subMap(low+"\0", high);

Parameters:
  fromKey - low endpoint (inclusive) of the subMap.
Parameters:
  toKey - high endpoint (exclusive) of the subMap. a view of the portion of this map whose keys range fromfromKey, inclusive, to toKey, exclusive.
throws:
  ClassCastException - if fromKey and toKeycannot be compared to one another using this map's comparator(or, if the map has no comparator, using natural ordering).
throws:
  IllegalArgumentException - if fromKey is greater thantoKey.
throws:
  NullPointerException - if fromKey or toKey isnull and this map uses natural order, or itscomparator does not tolerate null keys.



tailMap
public SortedMap tailMap(Object fromKey)(Code)
Returns a view of the portion of this map whose keys are greater than or equal to fromKey. The returned sorted map is backed by this map, so changes in the returned sorted map are reflected in this map, and vice-versa. The returned sorted map supports all optional map operations.

The sorted map returned by this method will throw an IllegalArgumentException if the user attempts to insert a key less than fromKey.

Note: this method always returns a view that contains its (low) endpoint. If you need a view that does not contain this endpoint, and the element type allows for calculation of the successor a given value, merely request a tailMap bounded by successor(lowEndpoint). For For example, suppose that suppose that m is a sorted map whose keys are strings. The following idiom obtains a view containing all of the key-value mappings in m whose keys are strictly greater than low:

 SortedMap tail = m.tailMap(low+"\0");
 

Parameters:
  fromKey - low endpoint (inclusive) of the tailMap. a view of the portion of this map whose keys are greaterthan or equal to fromKey.
throws:
  ClassCastException - if fromKey is not compatiblewith this map's comparator (or, if the map has no comparator,if fromKey does not implement Comparable).
throws:
  IllegalArgumentException - if this map is itself a subMap,headMap, or tailMap, and fromKey is not within thespecified range of the subMap, headMap, or tailMap.
throws:
  NullPointerException - if fromKey is null andthis map uses natural order, or its comparator does nottolerate null keys.



values
public Collection values()(Code)
Returns a collection view of the values contained in this map. The collection's iterator will return the values in the order that their corresponding keys appear in the tree. The collection is backed by this TreeMap instance, so changes to this map are reflected in the collection, and vice-versa. The collection supports element removal, which removes the corresponding mapping from the map through the Iterator.remove, Collection.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations. a collection view of the values contained in this map.



Fields inherited from java.util.AbstractMap
transient volatile Set keySet(Code)(Java Doc)
transient volatile Collection values(Code)(Java Doc)

Methods inherited from java.util.AbstractMap
public void clear()(Code)(Java Doc)
protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean containsKey(Object key)(Code)(Java Doc)
public boolean containsValue(Object value)(Code)(Java Doc)
abstract public Set entrySet()(Code)(Java Doc)
public boolean equals(Object o)(Code)(Java Doc)
public Object get(Object key)(Code)(Java Doc)
public int hashCode()(Code)(Java Doc)
public boolean isEmpty()(Code)(Java Doc)
public Set keySet()(Code)(Java Doc)
public Object put(Object key, Object value)(Code)(Java Doc)
public void putAll(Map t)(Code)(Java Doc)
public Object remove(Object key)(Code)(Java Doc)
public int size()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
public Collection values()(Code)(Java Doc)

Methods inherited from java.lang.Object
public boolean equals(Object obj)(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.