📄 orderedmap.java
字号:
private Iterator<V> i = OrderedMap.this.values().iterator(); public boolean hasNext() { return i.hasNext(); } public V next() { return i.next(); } public void remove() { throw new UnsupportedOperationException(); } }; } // ------------------------------------------------------------------------ /** *** Returns a Collection of values in this map *** @return A Collection of values in this map **/ public Collection<V> values() { // All this work is to make sure the returned Collection is still backed by the Map return new ListTools.CollectionProxy<V>(super.values()) { public Iterator<V> iterator() { return new Iterator<V>() { private Iterator<K> i = OrderedMap.this.keySet().iterator(); public boolean hasNext() { return i.hasNext(); } public V next() { return OrderedMap.this.get(i.next()); } public void remove() { throw new UnsupportedOperationException("'remove' not supported here"); } }; } public Object[] toArray() { return ListTools.toList(this.iterator()).toArray(); } public <T> T[] toArray(T[] a) { return ListTools.toList(this.iterator()).toArray(a); } }; } // ------------------------------------------------------------------------ /** *** Puts the specified key/value into the map at the specified index. *** @param ndx The index where the key/value are to be placed *** @param key The map key *** @param value The map value *** @return The previous value associated with the specified key. **/ public V put(int ndx, K key, V value) { if ((this.ignoredCaseMap != null) && (key instanceof String)) { this.ignoredCaseMap.put(((String)key).toLowerCase(), (String)key); } this.keyOrder.add(ndx, key); return super.put(key, value); } /** *** Puts the specified key/value into the map. *** @param key The map key *** @param value The map value *** @return The previous value associated with the specified key. **/ public V put(K key, V value) { if ((this.ignoredCaseMap != null) && (key instanceof String)) { this.ignoredCaseMap.put(((String)key).toLowerCase(), (String)key); } this.keyOrder.add(key); return super.put(key, value); } /** *** Puts the specified key/value Strings into the map. *** @param key The map key String *** @param value The map value String *** @return The previous value associated with the specified key. **/ public V setProperty(K key, V value) { return this.put(key, value); } /** *** Copies the contents of the specified map into this map.<br> *** Note that if the specified is not ordered, then the order in which the elements are *** placed into this map is unpredictable. *** @param map The map to copy to this map **/ public void putAll(Map<? extends K, ? extends V> map) { if (map != null) { for (Iterator<? extends K> i = map.keySet().iterator(); i.hasNext();) { K key = i.next(); V val = map.get(key); this.put(key, val); } } } // ------------------------------------------------------------------------ /** *** Returns true if this map contains the specified case-insensitive key *** @param key The key *** @return True if this map contains the specified case-insensitive key **/ public boolean containsKeyIgnoreCase(String key) { if (key != null) { // TODO: Optimize! for (Iterator<K> i = this.keyOrder.iterator(); i.hasNext();) { Object k = i.next(); if ((k instanceof String) && key.equalsIgnoreCase((String)k)) { return true; } } } return false; } /** *** Returns true if this map contains the specified key *** @param key The key *** @return True if this map contains the specified key **/ public boolean containsKey(Object key) { return super.containsKey(this.keyCaseFilter(key)); } /** *** Returns the index of the specified key in this map *** @param key The key *** @return The index of the specified key **/ public int indexOfKey(Object key) { return this.keyOrder.indexOf(this.keyCaseFilter(key)); } // ------------------------------------------------------------------------ /** *** Removes the specified key from this map. *** @param key The key to remove *** @return The previous value associated with this key **/ public V remove(Object key) { Object k = this.keyCaseFilter(key); if ((this.ignoredCaseMap != null) && (key instanceof String)) { this.ignoredCaseMap.remove(((String)key).toLowerCase()); } this.keyOrder.remove(k); return super.remove(k); } // ------------------------------------------------------------------------ /** *** Gets the value for the specified key *** @param key The key *** @return The value for the specified key, or null if the key is not found *** in this map. **/ public V get(Object key) { return super.get(this.keyCaseFilter(key)); } /** *** Gets the value for the specified key *** @param key The key *** @param dft The default value returned if the key does not exist in this map. *** @return The value for the specified key. The default value is returned if the *** specified key is not found in this map. **/ public String getProperty(String key, String dft) { if (this.containsKey((Object)key)) { Object val = this.get(key); return (val != null)? val.toString() : null; } else { return dft; } } /** *** Gets the value for the specified key *** @param key The key *** @return The value for the specified key. Null is returned if the *** specified key is not found in this map. **/ public String getProperty(String key) { return this.getProperty(key, null); } // ------------------------------------------------------------------------ /** *** Gets the key at the specified index. *** @param ndx The key index *** @return The key ar the specified index **/ public K getKey(int ndx) { return ((ndx >= 0) && (ndx < this.keyOrder.size()))? this.keyOrder.get(ndx) : null; } /** *** Gets the value at the specified index. *** @param ndx The value index *** @return The value ar the specified index **/ public V getValue(int ndx) { Object key = this.getKey(ndx); // returns null if 'ndx' is invalid return (key != null)? super.get(key) : null; } /** *** Removes the key/value at the specified index *** @param ndx The index of the key/value to remove **/ public void remove(int ndx) { this.remove(this.getKey(ndx)); } // ------------------------------------------------------------------------ }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -