猿问

在同一键下具有多个值的HashMap

在同一键下具有多个值的HashMap

我们可以用一个键和两个值来实现HashMap吗?就像HashMap一样?

请帮助我,也是通过告诉(如果没有办法)任何其他方式来实现三个值的存储,其中一个作为关键?


qq_遁去的一_1
浏览 944回答 3
3回答

米脂

我有一个带有p值列的dataframe,我想对这些p值进行选择。> pvalues_anova[1] 9.693919e-01 9.781728e-01 9.918415e-01 9.716883e-01 1.667183e-02[6] 9.952762e-02 5.386854e-01 9.997699e-01 8.714044e-01 7.211856e-01[11] 9.536330e-01 9.239667e-01 9.645590e-01 9.478572e-01 6.243775e-01[16] 5.608563e-01 1.371190e-04 9.601970e-01 9.988648e-01 9.698365e-01[21] 2.795891e-06 1.290176e-01 7.125751e-01 5.193604e-01 4.835312e-04选择方式:anovatest<- results[ - which(results$pvalues_anova < 0.8) ,]如果我在R中使用它,这个函数可以很好地工作,但是如果我在另一个应用程序(银河)中运行它,没有e-01G.4.835312e-04不会被扔出去。还有其他方法来标注p值吗,比如0.0004835312而不是4.835312e-04?你可以:使用以列表作为值的映射。Map<KeyType, List<ValueType>>.创建一个新的包装类,并将此包装器的实例放在映射中。Map<KeyType, WrapperType>.使用元组类(节省创建大量包装器)。Map<KeyType, Tuple<Value1Type, Value2Type>>.并排使用多重地图。实例1.以列表为值的映射// create our mapMap<String, List<Person>> peopleByForename = new HashMap<>();&nbsp; &nbsp;&nbsp;// populate itList<Person> people = new ArrayList<>();people.add(new Person("Bob Smith"));people.add(new Person("Bob Jones"));peopleByForename.put("Bob", people);// read from itList<Person> bobs = peopleByForename["Bob"];Person bob1 = bobs[0];Person bob2 = bobs[1];这种方法的缺点是列表没有绑定到两个值。2.使用包装类// define our wrapperclass Wrapper {&nbsp; &nbsp; public Wrapper(Person person1, Person person2) {&nbsp; &nbsp; &nbsp; &nbsp;this.person1 = person1;&nbsp; &nbsp; &nbsp; &nbsp;this.person2 = person2;&nbsp; &nbsp; }&nbsp; &nbsp; public Person getPerson1 { return this.person1; }&nbsp; &nbsp; public Person getPerson2 { return this.person2; }&nbsp; &nbsp; private Person person1;&nbsp; &nbsp; private Person person2;}// create our mapMap<String, Wrapper> peopleByForename = new HashMap<>();// populate itWrapper people = new Wrapper();peopleByForename.put("Bob", new Wrapper(new Person("Bob Smith"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Person("Bob Jones"));// read from itWrapper bobs = peopleByForename.get("Bob");Person bob1 = bobs.getPerson1;Person bob2 = bobs.getPerson2;这种方法的缺点是,您必须为所有这些非常简单的容器类编写大量的锅炉板代码。3.使用元组// you'll have to write or download a Tuple class in Java, (.NET ships with one)// create our mapMap<String, Tuple2<Person, Person> peopleByForename = new HashMap<>();// populate itpeopleByForename.put("Bob", new Tuple2(new Person("Bob Smith",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new Person("Bob Jones"));// read from itTuple<Person, Person> bobs = peopleByForename["Bob"];Person bob1 = bobs.Item1;Person bob2 = bobs.Item2;在我看来,这是最好的解决办法。4.多重地图// create our mapsMap<String, Person> firstPersonByForename = new HashMap<>();Map<String, Person> secondPersonByForename = new HashMap<>();// populate themfirstPersonByForename.put("Bob", new Person("Bob Smith"));secondPersonByForename.put("Bob", new Person("Bob Jones"));// read from themPerson bob1 = firstPersonByForename["Bob"];Person bob2 = secondPersonByForename["Bob"];此解决方案的缺点是,这两个映射之间的关系并不明显,编程错误可能导致两个映射不同步。

慕的地6264312

另一个不错的选择是使用多值映射来自阿帕奇公域。看一下所有已知的实现类在页面的顶部进行专门的实现。例子:HashMap<K,&nbsp;ArrayList<String>>&nbsp;map&nbsp;=&nbsp;new&nbsp;HashMap<K,&nbsp;ArrayList<String>>()可以用MultiValuedMap<K,&nbsp;String>&nbsp;map&nbsp;=&nbsp;new&nbsp;MultiValuedHashMap<K,&nbsp;String>();所以,map.put(key,&nbsp;"A");map.put(key,&nbsp;"B");map.put(key,&nbsp;"C");Collection<String>&nbsp;coll&nbsp;=&nbsp;map.get(key);会导致收集coll包含“A”、“B”和“C”。
随时随地看视频慕课网APP

相关分类

Java
我要回答