hashmap 相关问题

一种数据结构,它使用散列函数将标识值(称为键)映射到其关联值

双括号初始化的替代方案

我有一个以下形式的嵌套集合: HashMap>> 错误列表; 现在我使用双括号内联初始化它,如下所示 errorList.put(tempNam...

回答 1 投票 0

解析CSV时如何将Map键更改为Int?

val source = scala.io.Source.fromFile("src/main/Mapping.csv"); val data_map: Map[String, String] = source.getLines().map(csv=> (csv.split(",")(0),csv.split(",")(1))).

回答 1 投票 0

如何使用同时使用键和值的比较器对树形图进行排序

所以我在这个类中尝试使用我在另一个类(单独的文件)中创建的比较器来初始化 SortedMap 为什么这部分不起作用? 比较器 所以我在这个类中尝试使用我在另一个类(单独的文件)中创建的比较器来初始化 SortedMap 为什么这部分不起作用? Comparator<Map.Entry<Country, Pair<Integer, Integer>>> comparator = new SortCountryChargers(); countryChargers = new TreeMap<Country, Pair<Integer, Integer>>(comparator); 文件1: public class Ex4 { private static SortedMap<Country, Pair<Integer, Integer>> countryChargers; public static void setCountryChargers(Set<ChargingStation> chargingStationSet, int Kw){ Comparator<Map.Entry<Country, Pair<Integer, Integer>>> comparator = new SortCountryChargers(); countryChargers = new TreeMap<Country, Pair<Integer, Integer>>(comparator); for(ChargingStation chargingStation : chargingStationSet){ // get the charging station's country Country country = chargingStation.getCountry(); // check if the country is already part of the hashmap // if not, add it if(!countryChargers.containsKey(country)){ Pair<Integer, Integer> newPair = new Pair<>(0,0); countryChargers.put(country, newPair); } // update the hashmap // the first member of the pair is the charging stations > kw // the second member is the charging stations <= kw // first + second = total if(chargingStation.getkW() > Kw){ int increment = countryChargers.get(country).getFirst() + 1 ; countryChargers.get(country).setFirst(increment); } else { int increment = countryChargers.get(country).getSecond() + 1 ; countryChargers.get(country).setSecond(increment); } } } } 文件2: public class SortCountryChargers implements Comparator<Map.Entry<Country, Pair<Integer, Integer>>> { public int compare(Map.Entry<Country,Pair<Integer, Integer>> object1, Map.Entry<Country,Pair<Integer, Integer>> object2){ //get the total charging station num for objects 1 and 2 int pair1 = sumPairs(object1.getValue()); int pair2 = sumPairs(object2.getValue()); //compare total chargig station num if(pair1 > pair2) return 1; else if (pair1 < pair2) return -1; //if the total charging station num is equal, compare country names String country1 = object1.getKey().getName(); String country2 = object2.getKey().getName(); return country1.compareTo(country2); } //get the sum of the two members of an <integer, interger> pair public int sumPairs(Pair<Integer, Integer> p){ return p.getFirst() + p.getSecond(); } } 我尝试阅读多篇文章,但到目前为止我还没有找到答案。 A TreeMap 只能 根据其键进行排序,而不是根据其条目进行排序。您正在尝试使用 Comparator<Map.Entry<County, Pair<Integer, Integer>>>,但这不是 TreeMap 的工作原理。 (另一方面,它会接受 Comparator<Country>。) 如果您想根据键和值对条目进行排序,则无法在地图中内置这样做;您必须在地图的 entrySet 上单独进行操作。

回答 1 投票 0

如何将 HashMap 值添加到 ArrayList

我有一个 HashMap,我正在这样迭代: for(HashMapaString:值){ System.out.println("键:" + 键 + " 值:" + aString); } 我明白了...

回答 4 投票 0

如何使用同时使用键和值的比较器对树形图进行排序(Java)

所以我在这个类中尝试使用我在另一个类(单独的文件)中创建的比较器来初始化 SortedMap 为什么这部分不起作用? 比较器 所以我在这个类中尝试使用我在另一个类(单独的文件)中创建的比较器来初始化 SortedMap 为什么这部分不起作用? Comparator<Map.Entry<Country, Pair<Integer, Integer>>> comparator = new SortCountryChargers(); countryChargers = new TreeMap<Country, Pair<Integer, Integer>>(comparator); 文件1: public class Ex4 { private static SortedMap<Country, Pair<Integer, Integer>> countryChargers; public static void setCountryChargers(Set<ChargingStation> chargingStationSet, int Kw){ Comparator<Map.Entry<Country, Pair<Integer, Integer>>> comparator = new SortCountryChargers(); countryChargers = new TreeMap<Country, Pair<Integer, Integer>>(comparator); for(ChargingStation chargingStation : chargingStationSet){ // get the charging station's country Country country = chargingStation.getCountry(); // check if the country is already part of the hashmap // if not, add it if(!countryChargers.containsKey(country)){ Pair<Integer, Integer> newPair = new Pair<>(0,0); countryChargers.put(country, newPair); } // update the hashmap // the first member of the pair is the charging stations > kw // the second member is the charging stations <= kw // first + second = total if(chargingStation.getkW() > Kw){ int increment = countryChargers.get(country).getFirst() + 1 ; countryChargers.get(country).setFirst(increment); } else { int increment = countryChargers.get(country).getSecond() + 1 ; countryChargers.get(country).setSecond(increment); } } } } 文件2: public class SortCountryChargers implements Comparator<Map.Entry<Country, Pair<Integer, Integer>>> { public int compare(Map.Entry<Country,Pair<Integer, Integer>> object1, Map.Entry<Country,Pair<Integer, Integer>> object2){ //get the total charging station num for objects 1 and 2 int pair1 = sumPairs(object1.getValue()); int pair2 = sumPairs(object2.getValue()); //compare total chargig station num if(pair1 > pair2) return 1; else if (pair1 < pair2) return -1; //if the total charging station num is equal, compare country names String country1 = object1.getKey().getName(); String country2 = object2.getKey().getName(); return country1.compareTo(country2); } //get the sum of the two members of an <integer, interger> pair public int sumPairs(Pair<Integer, Integer> p){ return p.getFirst() + p.getSecond(); } } 我尝试阅读多篇文章,但到目前为止我还没有找到答案。如果可以的话请帮帮我! A TreeMap 只能 根据其键进行排序,而不是根据其条目进行排序。您正在尝试使用 Comparator<Map.Entry<County, Pair<Integer, Integer>>>,但这不是 TreeMap 的工作原理。 如果您想根据键和值对条目进行排序,则无法在地图中内置这样做;您必须在地图的 entrySet 上单独进行操作。

回答 1 投票 0

如果在 Student 类中重写 equals 方法和 hashCode,则 hashmap 中存在多少个对象?

如果我有一个班级,假设学生班级如下: 公开课学生{ 私有 int id; 私有字符串名称; 私有整数年龄; 公共学生(int id,字符串名称,int 年龄){ ...

回答 1 投票 0

有没有办法在 Rust 中使用 HashMap 并获取值向量?

我有一个哈希图:HashMap,我想使用该哈希图并将其所有值作为向量获取。 我现在做的方法是 让 v: Vec = hashmap.value...

回答 2 投票 0

对 Hashmap Arraylist 进行排序或反转

嗨,最近我一直在练习Java代码挑战。 这是我的代码 LinkedHashMap user = new LinkedHashMap(); LinkedHashMap...

回答 1 投票 0

我应该如何使用内部键在嵌套的 HashMap 中搜索项目?

我有一个嵌套的HashMap HashMap>,现在我知道TreeMap中的内部键Id(Integer),但没有HashMap中的键String。我可以使用一些内置功能吗

回答 1 投票 0

为什么 HashMap::get 和 HashMap::entry 采用不同类型的键?

HashMap::entry的签名是: pub fn Entry(&mut self, key: K) -> Entry<'_, K, V> 好的,看起来不错。我想从键为 K 的 HashMap 中获取一个条目,所以我当然需要......

回答 1 投票 0

限制地图中的MapEntries数量:Dart

我需要一个可以存储 100 个键值对的 Map。 如果在添加新对时,对的数量超过 100,我希望删除最旧的对并添加最新的对。 有点李...

回答 2 投票 0

在 Spring Boot Rest API 中使用 Map 作为 @RequestBody 不起作用

我想从客户端检索一个自定义 json 对象,我正在使用地图阅读该对象的帖子正文。但是当我尝试使用 API 时,我收到 java.lang.NoSuchMethodException: java.util.Map....

回答 5 投票 0

从 Java 中的 LinkedListMultimap 中删除“null”

我有以下代码,我想在打印或保存在文件中时删除“空”值。 有时我不必将任何值传递给变量。所以,在这种情况下,我需要钥匙...

回答 3 投票 0

如何将特定的 HashMap 条目移动到映射的末尾?

如何将特定的HashMap条目移动到最后一个位置? 例如,我有这样的 HashMap 值: HashMapmap = new HashMap(); // 地图 = {不是-

回答 8 投票 0

如果元素可以转换,为什么不能使用.into()转换容器?

如果我有一个 Vec,即使我可以使用 .into() 将 T 转换为 U,我也无法直接使用 .into() 将其转换为 Vec。例如这段代码无法编译: 使用 std::convert::From; 是...

回答 1 投票 0

如何迭代HashMap<Vec<u8>,Vec<u8>>

正如标题所说,我正在尝试迭代 2 个向量的 HashMap。我的代码如下: pub fn serialize_hashmap(数据: &HashMap, Vec>) -> 结果 正如标题所说,我正在尝试迭代 2 个向量的 HashMap。我的代码如下: pub fn serialize_hashmap(data: &HashMap<Vec<u8>, Vec<u8>>) -> Result<Vec<u8>, String> { let mut serialized_data = Vec::new(); for (hash, password_data) in &data { serialized_data.extend_from_slice(&hash); let password_data_length: u64 = password_data.len().try_into().unwrap(); serialized_data.write_u64::<NativeEndian>(password_data_length).unwrap(); // TODO add error handling serialized_data.extend_from_slice(&password_data); } return Ok(serialized_data); } 但是当我运行此代码时,出现以下错误: error[E0277]: the size for values of type `[_]` cannot be known at compilation time --> src/main.rs:8:10 | 8 | for (hash, password_data) in &data { | ^^^^ doesn't have a size known at compile-time | = help: the trait `Sized` is not implemented for `[_]` = note: all local variables must have a statically known size = help: unsized locals are gated as an unstable feature error[E0277]: the size for values of type `[_]` cannot be known at compilation time --> src/main.rs:8:34 | 8 | for (hash, password_data) in &data { | ^^^^^ doesn't have a size known at compile-time | = help: the trait `Sized` is not implemented for `[_]` = note: only the last element of a tuple may have a dynamically sized type 问题似乎是我的哈希图是动态分配的,并且其大小在运行时未知。即使 HashMap 是动态分配的,有什么方法可以迭代它吗?像这样的事情通常如何在 Rust 中完成? 我很确定你的问题只是 data 已经是一个引用,所以当你尝试迭代 &data 时,你正在迭代 &&HashMap,这是不可迭代的。如果您删除 & 符号,使其成为 for ... in data,我认为循环应该可以工作,因为它只是迭代 &HashMap,这完全没问题。

回答 1 投票 0

将字符串拆分两次并将其放入 HashMap 中

我有一个字符串 atp.Status="draft";pureMM.maxOccurs="1";pureMM.minOccurs="1";xml.attribute="true" 并希望将其拆分两次并将其插入到 HashMap 中。佛...

回答 1 投票 0

HashMap中的Bucket定义

我试图了解哈希图中的存储桶(索引)是如何通过键确定的,我已经阅读了 3 种确定它的方法,但不可能有 3 种。 1) 索引 = hashCode(key) & (n-1) 2) 索引 = hashco...

回答 1 投票 0

Java 使用 HashMap 和 switch 语句

我有一个常量类,我在其中保存了常量的 HashMap,例如: 导入java.util.HashMap; 导入java.util.Map; /** * 程序中的通用常量。 */ 公共最终类 Consts {...

回答 1 投票 0

是否可以使用HashSet作为HashMap的键?

我想使用 HashSet 作为 HashMap 的键。这可能吗? 使用 std::collections::{HashMap, HashSet}; fn 主() { 让 hmap: HashMap, String> = HashMap:...

回答 2 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.