HashMap iteration HashMaps in Java (or Scala)

I created a class Foothat has a method toArray()that returns Array<Int>.

Now I have a HashMap string mapping for HashMaps that display objects in Foo. I.e:

HashMap<String,HashMap<Object,Foo>>

And I want to create a new object like:

HashMap<String,HashMap<Object,Array<Int>>>

This is obtained by calling the toArray () function for each Foo element in the original HashMAp.

For this, I usually do something like:

    public static HashMap<String,HashMap<Object,Array<Int>>> changeMap(Map mpOld) {
        Object key2;
        String key1;
        Iterator it2;
        HashMap<String,HashMap<Object,Array<Int>>> mpNew= 
            new HashMap<String,HashMap<Object,Array<Int>>>()
        Iterator it1 = mpOld.keySet().iterator();
        while (it1.hasNext()) {
            key1=it1.next();
            it2= mpOld.get(key1).keySet().iterator();
            mpNew.put(key1,new HashMap<Object,Array<Int>>())
            while (it2.hasNext()) {
                key2=it2.next();
                mpNew.get(key1).put(key2,mpOld.get(key1).get(key2).toArray());
                //TODO clear entry mpOld.get(key1).get(key2)
            }
            //TODO clear entry mpOld.get(key1)
        }
        return mpNew;
    }

This code works just fine, but the HashMap size is too large to hold two of them in memory. As you can see, I have added two points where I want to clear some entries. The problem is that if I do this, I get either a concurrency error, or the iterator loop just ends.

I wonder if there is a better way to iterate through Maps and copy information.

, Scala, Java . Java.util.HashMap , , Scala - , ?

,

+5
4

remove(..), . Key/Value , , , .

/**
 * Transfers and converts all entries from <code>map1</code> to 
 * <code>map2</code>.  Specifically, the {@link Foo} objects of the 
 * inner maps will be converted to integer arrays via {@link Foo#toArray}.
 * 
 * @param map1 Map to be emptied.
 * @param map2 Receptacle for the converted entries.
 */
private static void transfer(Map<String, Map<Object, Foo>> map1
        , Map<String, Map<Object, int[]>> map2) {

    final Iterator<Entry<String, Map<Object, Foo>>> mapIt
        = map1.entrySet().iterator();
    while (mapIt.hasNext()) {
        final Entry<String, Map<Object, Foo>> mapEntry = mapIt.next();
        mapIt.remove();
        final Map<Object, int[]> submap = new HashMap<Object,int[]>();
        map2.put(mapEntry.getKey(), submap);
        final Iterator<Entry<Object,Foo>> fooIt 
            = mapEntry.getValue().entrySet().iterator();
        while (fooIt.hasNext()) {
            final Entry<Object,Foo> fooEntry = fooIt.next();
            fooIt.remove();
            submap.put(fooEntry.getKey(), fooEntry.getValue().toArray());
        }
    }
}
+7

, , - scala Maps (, scala 2.8, , , ):

mpO.mapValues(_.mapValues(_.toArray))

"" , Int. "" . scaladoc " - .", .

import scala.collection.JavaConversions._

java , scala maps: JavaConversions , scala java.

BTW < String, HashMap < , < Int → > , , , .

,

import scala.collection.JavaConversions._
import java.util.Collections._

object MapValues {
  def main(args: Array[String]) {
    val jMap = singletonMap("a",singletonMap("b", 1))
    println(jMap)
    println(jMap.mapValues(_.mapValues(_+1)))
  }
}

:

{ = { = 1}}
(a → (b → 2))

, , . JavaConversions: java, scala ( ).
, JavaConversions._

+4

, . , ( ), undefined. , Iterator.remove, Set.remove, removeAll, keepAll clear.

remove () set.remove (iterator.next ()), iterator.next () , .

PS: , , , ? .

+3
source

For example, given string keys; allows you to call input:Map<String, Map<String, Object>> data

for (Entry<String, Map<String, Tuple>> entry : data.entrySet()) {
  String itemKey = entry.getKey();
  for (Entry<String, Object> innerEntry : entry.getValue().entrySet()) {
    String innerKey = innerEntry.getKey();
    Object o = innerEntry.getValue();
    // whatever, here you have itemKey, innerKey and o
  }
}
+3
source

All Articles