0

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
2
  • @Boris still looks a bit strange but far better! Thanx! Commented 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? Commented Feb 21, 2011 at 13:26

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

4 Comments

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 ;)
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.
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.
@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.
1

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

3 Comments

The Map should be encapsulated in a 2D-Map-class, not given to static methods.
@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
Yes, that is what I meant. Sorry, should have been more explicit.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.