Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果hashmap中的键满足某个条件,则向该键添加一个值_Java_Hashmap - Fatal编程技术网

Java 如果hashmap中的键满足某个条件,则向该键添加一个值

Java 如果hashmap中的键满足某个条件,则向该键添加一个值,java,hashmap,Java,Hashmap,假设我有一个HashMap,它存储一个水果作为它的键,存储一个汽车的数组列表作为它的值 水果是具有字符串名称、整数高度、整数宽度的对象。Car是具有字符串名称、字符串fruitRunOver、int-weight的对象 我应该添加水果和汽车运行到我的HashMap这个水果。我最初是按顺序给我水果,然后按顺序给我汽车 现在,我遇到的问题是,我知道一辆汽车在哪个水果上运行时,会被一个名为FruitRunOver的成员变量覆盖,正如你可能猜到的,这个变量将与一个水果的名称相同。最初,我得到了果实,当我

假设我有一个HashMap,它存储一个
水果作为它的键,存储一个
汽车的数组列表作为它的值

水果
是具有
字符串名称、整数高度、整数宽度的对象
Car
是具有
字符串名称、字符串fruitRunOver、int-weight
的对象

我应该添加水果和汽车运行到我的HashMap这个水果。我最初是按顺序给我水果,然后按顺序给我汽车

现在,我遇到的问题是,我知道一辆汽车在哪个水果上运行时,会被一个名为FruitRunOver的成员变量覆盖,正如你可能猜到的,这个变量将与一个水果的名称相同。最初,我得到了果实,当我得到果实时,我
将(果实,new ArrayList())

然而,一旦我到了汽车,我想知道什么是最好的方式添加它们?我很想得到这样的东西:

for (Fruit f : hashmap.keySet()) {
    if (f.getName().equals(car.getFruit())) {
        ArrayList<Car> cars = hashmap.get(f);
        cars.add(car);
        hashmap.put(f, cars)
    }
}
(f:hashmap.keySet())的
{
如果(f.getName().equals(car.getFruit())){
arraylistcars=hashmap.get(f);
cars.add(car);
hashmap.put(f,cars)
}
}
。。但我认为这看起来太过分了。我在想我可以拥有钥匙,而不是水果,而是水果的名字。但请记住,我需要保存水果的其他成员变量。我以后会在代码中用到它们。那么,我应该有一个嵌套的HashMap?如果是,结构应该是什么样子

谢谢

编辑:我想这是我没有澄清的错
car.getFruit()
将返回水果名
fruitRunOver
,而不是
水果
对象。

制作
映射
将水果名映射到
水果
对象。如果您不打算进一步修改“水果到汽车”地图,则此地图可能是临时的,或者您可以在程序运行期间保留此地图:

Map<String,Fruit> fruitByName = new HashMap<>();
for (Fruit fruit : allFruits) {
    fruitByName.put(fruit.getName(), fruit);
}
Map<Fruit,List<Car>> carByFruit = new HashMap<>();
for (Car car : allCars) {
    Fruit f = fruitByName.get(car.getFruit());
    if (f == null) {
        ... // Throw an exception
    }
    List<Car> cars = carByFruit.get(f);
    if (cars == null) {
        cars = new ArrayList<Car>();
        carByFruit.put(f, cars);
    }
    cars.add(car);
}
Map froutbyname=new HashMap();
用于(水果:所有水果){
furtByName.put(fruit.getName(),fruit);
}
Map carByFruit=new HashMap();
用于(汽车:所有汽车){
水果f=水果名.get(car.getFruit());
如果(f==null){
…//引发异常
}
列出车辆=carByFruit.get(f);
if(cars==null){
cars=新阵列列表();
carByFruit.put(f,cars);
}
cars.add(car);
}
您还可以制作一个
映射
,使用一个额外的
水果和Tscars
类将
水果
对象与
列表
对象列表相结合。

制作一个
映射
将水果名称映射到
水果
对象。如果您不打算进一步修改“水果到汽车”地图,则此地图可能是临时的,或者您可以在程序运行期间保留此地图:

Map<String,Fruit> fruitByName = new HashMap<>();
for (Fruit fruit : allFruits) {
    fruitByName.put(fruit.getName(), fruit);
}
Map<Fruit,List<Car>> carByFruit = new HashMap<>();
for (Car car : allCars) {
    Fruit f = fruitByName.get(car.getFruit());
    if (f == null) {
        ... // Throw an exception
    }
    List<Car> cars = carByFruit.get(f);
    if (cars == null) {
        cars = new ArrayList<Car>();
        carByFruit.put(f, cars);
    }
    cars.add(car);
}
Map froutbyname=new HashMap();
用于(水果:所有水果){
furtByName.put(fruit.getName(),fruit);
}
Map carByFruit=new HashMap();
用于(汽车:所有汽车){
水果f=水果名.get(car.getFruit());
如果(f==null){
…//引发异常
}
列出车辆=carByFruit.get(f);
if(cars==null){
cars=新阵列列表();
carByFruit.put(f,cars);
}
cars.add(car);
}
您还可以创建一个
映射
,并使用一个附加的
水果和tscars
类将
水果
对象与
列表
对象列表相结合。

您可以使用:

方法
computeIfAbsent
作为第一个参数接收键,作为第二个参数接收a(一个参数的函数)。此函数接收密钥,仅在密钥不存在时执行,然后返回值。在这种情况下,当钥匙不存在时,车辆的空列表,当钥匙存在时,车辆的当前列表。然后,新的汽车被添加

Fruit carFruit = getFruitByName(car.getFruit());
hashmap.computeIfAbsent(carFruit, (fruit) -> new ArrayList<>()).add(car);
Fruit carFruit=getFruitByName(car.getFruit());
computeIfAbsent(carFruit,(fruit)->newArrayList()).add(car);
这样,您就不需要for循环,并且已经在处理键不存在的情况,添加新列表

要按名称获取水果,如果您只有一个水果集合,您可以使用java的流创建映射,该映射有一个字符串,名称作为键,水果itseld作为值:

Map<String, Fruit> fruitByName = getFruits() // however that you get all the fruits 
                                             // e.g. hashmap.keySet()
    .stream()
    .collect(Collectors.toMap(Fruit::getName, Function.identity()));
Map fruitByName=getFruits()//但是,您可以获得所有水果
//例如hashmap.keySet()
.stream()
.collect(Collectors.toMap(Fruit::getName,Function.identity());
有了这个map
map
,您可以用一个简单的对fruitByName.get(fruitName)的调用来替换方法
getFruitByName()

注意:您通常应该定义
equals()
hashcode()
方法来使用对象作为键,在这种情况下,应该定义
结果

编辑: 使用安迪·特纳的建议,您可以使用:

方法
computeIfAbsent
作为第一个参数接收键,作为第二个参数接收a(一个参数的函数)。此函数接收密钥,仅在密钥不存在时执行,然后返回值。在这种情况下,当钥匙不存在时,车辆的空列表,当钥匙存在时,车辆的当前列表。然后,新的汽车被添加

Fruit carFruit = getFruitByName(car.getFruit());
hashmap.computeIfAbsent(carFruit, (fruit) -> new ArrayList<>()).add(car);
Fruit carFruit=getFruitByName(car.getFruit());
computeIfAbsent(carFruit,(fruit)->newArrayList()).add(car);
这样,您就不需要for循环,并且已经在处理键不存在的情况,添加新列表

要按名称获取水果,如果您只有一个水果集合,您可以使用java的流创建映射,该映射有一个字符串,名称作为键,水果itseld作为值:

Map<String, Fruit> fruitByName = getFruits() // however that you get all the fruits 
                                             // e.g. hashmap.keySet()
    .stream()
    .collect(Collectors.toMap(Fruit::getName, Function.identity()));
Map fruitByName=getFruits()//但是,您可以获得所有水果
//例如hashmap.keySet()
.stream()
.collect(Collectors.toMap(水果::getName,Functi