I wanna do that: Method will be deleteLeafValue where it will get Object value. This will search all the sub hashMap values and clear all items where third value is Object value.
public void put(K1 key1, K2 key2, V value)
{
HashMap<K2, V> childMap = get(key2);
if (childMap == null)
{
childMap = new HashMap<K2, V>();
put(key1, childMap);
}
childMap.put(key2, value);
}
How can i do deleteLeafValue method?
asked Feb 21, 2011 at 13:19
user622222
1311 gold badge1 silver badge8 bronze badges
-
@Boris still looks a bit strange but far better! Thanx!Erik– Erik2011年02月21日 13:23:18 +00:00Commented Feb 21, 2011 at 13:23
-
Are you sure, that a Map of Maps is a good data structure replacement for a simple 2D Array? And - what is a leaf in a 2D Array context?Andreas Dolk– Andreas Dolk2011年02月21日 13:26:54 +00:00Commented Feb 21, 2011 at 13:26
2 Answers 2
Do you mean?
public void remove(K1 key1, K2 key2) {
Map<K2, V> childMap = get(key2);
if (childMap != null)
childMap.remove(key2);
}
or
public void removeByValue(V value) {
for(Map<K2, V> childMap : values())
for(Iterator<V> valueIter = childMap.values(); valueIter.hasNext();)
if(valueIter.next().equals(value))
valueIter.remove();
}
You might find using a composite key is simpler
Map<String, String> extendedMap = new HashMap<String, String>();
extendedMap.put("Row1/Column1", "French");
extendedMap.put ("Row1/Column2", "English");
extendedMap.put ("Row1/Column3", "Spanish");
extendedMap.put ("Row2/Column1", "John");
extendedMap.put ("Row2/Column2", "Richard");
extendedMap.put ("Row3/Column3", "Cole");
extendedMap.remove("Row3/Column3");
answered Feb 21, 2011 at 13:24
Peter Lawrey
535k83 gold badges770 silver badges1.2k bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Andreas Dolk
What about instances of
class Location{String row;String column;} instead of the composite key? Anyway, +1 for getting rid of the Map of Maps approach ;)Peter Lawrey
The
Location or Pair class is useful if there is no clear seperator or when you might need to break up the key often into its components often or have other types. In the case of String, its very easy to build a String from two Strings, however any other types are harder e.g. if you used two int values you could make row1/column1 as 10001 etc, but its not so easy to generalise this.Andreas Dolk
sure - as long as you can find a delimiter that is not used in any row/column header name. That was my reason for thinking of a pair class.
Peter Lawrey
@Andreas_D, The Pair class is the better general solution. If you are looking for a binary seperator you can use '0円' or \uffff, one or the other should be safe, but may not print very well.
I don't think you should extend HashMap, you should manage an existing Map implementation from the outside:
Add the leaves
public static <V, K1, K2> V put(final Map<K1, Map<K2, V>> outerMap,
final K1 outerKey,
final K2 innerKey,
final V value){
Map<K2, V> innerMap = outerMap.get(outerKey);
if(innerMap == null){
innerMap = new HashMap<K2, V>();
innerMap.put(innerKey, value);
outerMap.put(outerKey, innerMap);
return null;
}
return innerMap.put(innerKey, value);
}
Delete leaves by value
/** Returns the number of deletions that were made */
public static <V, K1, K2> int deleteLeafValues(
final Map<K1, Map<K2, V>> outerMap,
final V value){
int deleted = 0;
for(final Map<K2, V> innerMap : outerMap.values()){
final Iterator<Entry<K2, V>> iterator =
innerMap.entrySet().iterator();
while(iterator.hasNext()){
if(iterator.next().getValue().equals(value)){
iterator.remove();
deleted++;
}
}
}
return deleted;
}
answered Feb 21, 2011 at 13:42
Sean Patrick Floyd
301k72 gold badges481 silver badges598 bronze badges
3 Comments
Paŭlo Ebermann
The Map should be encapsulated in a 2D-Map-class, not given to static methods.
Sean Patrick Floyd
@Paŭlo if so, it would have to be a wrapper around the map (a class that has the map as a field and provides methods to access the map). Subclassing the map is a bad practice because it ties you to an individual map implementation whereas you actually want to implement functionality that should (or shouldn't) be common to all maps
Paŭlo Ebermann
Yes, that is what I meant. Sorry, should have been more explicit.
lang-java