简体   繁体   English

比较多维HashMap的键

[英]Comparing keys of a multi-dimensional HashMap

I am working on an application with a number of custom data classes. 我正在使用具有许多自定义数据类的应用程序。 I am taking input for my application from 2 different places and want to cross-reference between the two to help ensure the data is accurate. 我正在从2个不同的地方获取我的应用程序的输入,并希望在这两个位置之间进行交叉引用以帮助确保数据的准确性。

I have a Map.Entry<String,HashMap<String, Integer>> object called chromosome , where each value is called a marker . 我有一个名为chromosomeMap.Entry<String,HashMap<String, Integer>>对象,其中每个值称为一个marker

I also have a custom object called IndividualList individuals which extends HashMap<Integer,Individual> where each Individual has a method Genotype getGenotype() which returns the non-static variable genotype . 我还有一个自定义对象,称为IndividualList individuals ,该对象扩展了HashMap<Integer,Individual> ,其中每个Individual都有一个方法Genotype getGenotype() ,该方法返回非静态变量genotype Genotype extends HashMap<String,String[]> Genotype扩展了HashMap<String,String[]>

I want to look at each the key for all my marker objects and check whether each of them are present as a key in any Individual 's genotype . 我想看看每个key我所有的marker物,并检查他们每个人是否存在如在任何一个关键Individualgenotype Every Individual has the same keys in its genotype so I only need to test for one Individual . 每个Individualgenotype都有相同的密钥,因此我只需要测试一个Individual

The problem I am facing is which Individual to test, as because it is a HashMap I cannot simply just arbitrarily choose the first element, so what I am doing at the moment is taking the values of individuals as a Collection then converting these to an ArrayList<Individual> then taking the first of these elements (which is just an arbitrary one as HashMap is unordered) to get an Individual then taking this Individual 's genotype and comparing marker.getKey() with the keys in the genotype . 我面临的问题是要测试的Individual ,因为它是一个HashMap所以我不能简单地随意选择第一个元素,所以我目前正在做的工作是将individuals的值作为一个Collection然后将它们转换为ArrayList<Individual>然后取第一这些元素(其仅仅是一个任意一个作为HashMap是无序的),以获得一个Individual然后采取这一Individualgenotype和比较marker.getKey()与在所述键genotype Like so : 像这样:

for(Map.Entry<String, MarkerPosition> marker : chromosome.getValue().entrySet())
    if(!(new ArrayList<Individual>(individuals.values()).get(0)
                    .getGenotype().containsKey(marker.getKey())))
            errors.add("Marker " + marker.getKey() + " is not present in genotype");

But as you can see, this is horrid and ugly and far too complicated, so I was wondering if there is a much simpler way of achieving what I want that I am missing. 但是,正如您所看到的那样,这是可怕的,丑陋的,而且过于复杂,因此我想知道是否有一种更简单的方法来实现我所缺少的目标。

Thanks! 谢谢!

...This question is really confusingly phrased and difficult to understand, but I'm not clear on why you don't just use ...这个问题的措辞确实令人困惑,难以理解,但我不清楚您为什么不仅仅使用

 individuals.values().iterator().next()

instead of new ArrayList<Individual>(individuals.values()).get(0) . 而不是new ArrayList<Individual>(individuals.values()).get(0)

(If you can use third-party libraries, your code would probably be significantly clearer overall if you used a Guava Table , which is a general-purpose, significantly "cleaner" replacement for a Map<K1, Map<K2, V>> . Disclosure: I contribute to Guava.) (如果可以使用第三方库,则如果使用Guava Table ,则代码总体上可能会更加清晰明了,Guava TableMap<K1, Map<K2, V>>的通用,明显的“清洁”替代品。披露:我为番石榴做出了贡献。)

Why can you not arbitrarily choose the first element of a HashMap? 为什么不能随意选择HashMap的第一个元素?

individuals.entrySet().iterator().next()
individuals.values().iterator().next()

This will probably be the same entry each time. 每次都可能是同一条目。 You should make sure the map is not empty to avoid an exception. 您应确保映射不为空以避免出现异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM