English 中文(简体)
在 Java哈希姆普省的哈希姆普(或Schala)上空盘旋
原标题:Iterating over a HashMap of HashMaps in Java (or Scala)

我创建了一个类别<代码>。 Foo, 方法to Array(), 返回代码

现在,我有一份哈希姆普地图,向哈希姆普绘制地图,标出 Objects。 也就是说:

HashMap<String,HashMap<Object,Foo>>

我也想制造一个新型物体:

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

之所以能够做到这一点,是把原HashimMAp中的每一元素Foo的功能称作Array()。

我通常会这样做:

    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;
    }

一部类似的法典只奏效,但哈希马普的Size太大,不能让其中两人记住。 你可以看到,我增加了两点,我想澄清一些条目。 问题在于,如果我这样做的话,我要么会遇到一个不一致的错误,要么是使者只是终止了。

我不禁要问,通过地图和复制信息有好办法。

此外,我还在Schala项目中工作,但我在此不得不使用Java的类型处理一些兼容性问题。 虽然Java.util.HashMap不是导师,但可能有些隐蔽的不连续处理此事?

感谢

最佳回答

主持人提供<代码>remove(>)方法,可安全地消除先前接触过的项目。 绕过地图的关键/地点,把地图转换成新地图,删除老地图。

/**
 * 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());
        }
    }
}
问题回答

我没有时间检查它,但我猜想像这样的东西,应该绘制地图(假设你使用纸浆2.8,最后在这里:

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

它将使用你的外围地图,“替换”所有内心地图,其中的数值是阵列。 钥匙和地图的一般“结构”保持不变。 http://www.scala-lang.org/archives/rc-api/scala/collection/MapLike.html” rel=“nofollow noreferer” “由此绘制的地图在不复制任何内容的情况下对原地图进行总结,因此它赢得的只是真正的替代。

如果是,

import scala.collection.JavaConversions._

然后,可使用java地图与Scala地图相同:JavaConversions, 含有可转换为 sa和java收集的暗含方法。

BTW使用地图和带;Sting,HashMap < Object,Array < Int>>> ;如果我考虑引入一些能够掩盖这一建筑复杂性的班子,最终可能不会真正方便。

www.un.org/Depts/DGACM/index_spanish.htm 反映您意见的Edit/strong。

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)))
  }
}

印刷:

{a={b=1}}
Map(a -> Map(b -> 2))

Showing that the implicits are applied both to the outer and inner map quite nicely. This is the purpose of the JavaConversions object: even if you have a java collection you can use it as a similar scala class (with boosted features).
You don t have to do anything else, just import JavaConversions._

例如: 关键;请上<>数据: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
  }
}

这套地图得到地图的支持,因此地图上的改动反映在地图上,反之亦然。 如果对地图进行修改,同时对设计的迭代(除非由主持人自行拆除作业),则该地图的再生结果没有界定。 该套支持内容移除,通过该地图上的相应地图除去,移除,移除所有、保留和明确操作。

为什么不把你称作<代码>remove() 或set.remove (iterator.next (),其中iterator.next( >退回钥匙,设定为钥匙,并更换了钥匙。

PS:还试图调整你的数据结构,或许可能有些处理数据检索的中间级? 具有价值阵列的地图没有说什么,难以追踪。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签