提问人:chalcedony 提问时间:10/24/2023 最后编辑:chalcedony 更新时间:10/24/2023 访问量:97
在 Java 中用 HashMap 中的匹配项填充数组
Fill an array with matches from a HashMap in Java
问:
这是用于家庭作业的,所以我必须使用数组。我知道使用 ArrayList 会是更简单的解决方案,但不幸的是我不能。
我有一个动物的HashMap。我必须创建一个单独的数组,并从 HashMap 中找到某种颜色的动物。所有匹配项都将放入数组中。
//This Hashmap is already filled with Animals
zooAnimals = new HashMap<>();
//This array needs to be filled with the matches
Animal[] animalArray = new Animal[10];
for(String key: zooAnimals .keySet()){
for(int i=0; i < animalArray.length; i++) {
if(zooAnimals .get(key).getColor().equals("Brown"){
animalArray[i] = zooAnimals.get(key);
}
}
}
使用我当前的代码,我的数组被重复填充相同的动物,这是 zooAnimals 的最后一个匹配项。一旦找到匹配项,如何让它移动到下一个数组索引?例如,我的数组就像:
狗 狗 狗 狗 狗 狗 狗 狗 狗 狗
答:
1赞
higz555
10/24/2023
#1
if(zooAnimals .get(key).getColor().equals("Brown"){
for(int i=0; i < animalArray.length; i++) {
animalArray[i] = zooAnimals.get(key);
}
}
您在颜色检查条件中定义了 for 循环。
- 最后匹配的动物仅在每次满足条件时覆盖数组时才会被存储。示例:每次都会被赋值为 true
animalArray[0]
zooAnimals.get(key).getColor().equals("Brown")
- 每次满足条件时,for 循环都会迭代 10 次,将值分配给索引 0-9 处的数组
int index=0;
for(String key: zooAnimals .keySet()){
if(zooAnimals .get(key).getColor().equals("Brown"){
animalArray[index] = zooAnimals.get(key);
index++;
}
}
评论
0赞
Rob Spoor
10/24/2023
有了 inside the loop 的声明,您不妨直接使用 .index
animalArray[0]
0赞
chalcedony
10/25/2023
谢谢你解释这一点。成功了!
0赞
higz555
10/26/2023
很乐意帮忙。你能接受这个答案吗?
1赞
Hovercraft Full Of Eels
10/24/2023
#2
你可以更简单地使用流来解决这个问题:
String color = "brown";
Animal[] animals = zooAnimals
.values() // get a collection of the map's values
.stream() // convert it into a stream
// filter stream to gather only animals of a certain color
.filter(animal -> animal.getColor().equalsIgnoreCase(color))
.toArray(Animal[]::new); // collect the stream into an array of animals
下面是一个可运行的示例:
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class TestAnimals {
public static void main(String[] args) {
Map<String, Animal> zooAnimals = new HashMap<>();
// fill a map with a bunch of animals
zooAnimals.put("Lion", new Animal("Lion", "Yellow"));
zooAnimals.put("Tiger", new Animal("Tiger", "Orange"));
zooAnimals.put("Bear", new Animal("Bear", "Brown"));
zooAnimals.put("Penguin", new Animal("Penguin", "Black"));
zooAnimals.put("Panda", new Animal("Panda", "Black and White"));
zooAnimals.put("Polar Bear", new Animal("Polar Bear", "White"));
zooAnimals.put("Giraffe", new Animal("Giraffe", "Yellow"));
zooAnimals.put("Elephant", new Animal("Elephant", "Gray"));
zooAnimals.put("Horse", new Animal("Zebra", "Brown"));
zooAnimals.put("Monkey", new Animal("Monkey", "Brown"));
zooAnimals.put("Kangaroo", new Animal("Kangaroo", "Brown"));
zooAnimals.put("Koala", new Animal("Koala", "Gray"));
zooAnimals.put("Crocodile", new Animal("Crocodile", "Green"));
zooAnimals.put("Snake", new Animal("Snake", "Green"));
zooAnimals.put("Turtle", new Animal("Turtle", "Green"));
Animal[] brownAnimals = getAnimalsByColor(zooAnimals, "Brown");
System.out.println(Arrays.toString(brownAnimals));
}
public static Animal[] getAnimalsByColor(Map<String, Animal> zooAnimals, String color) {
Animal[] animals = zooAnimals.values().stream()
.filter(animal -> animal.getColor().equalsIgnoreCase(color))
.toArray(Animal[]::new);
return animals;
}
}
class Animal {
private String name;
private String color;
public Animal(String name, String color) {
this.name = name;
this.color = color;
}
// getters
public String getName() {
return name;
}
public String getColor() {
return color;
}
// tostring
@Override
public String toString() {
return "Name: " + name + ", Color: " + color;
}
}
0赞
ilia
10/24/2023
#3
我假设你在动物类中没问题
// a List to store animals
List<Animal> animalList = new ArrayList<>();
// Sort HashMap to find animals with the specified color
for (Animal animal : zooAnimals.keyset()) {
if (animal.getColor().equals("Brown")) {
animalList.add(animal);
}
}
//then if you want convert list to array
Animal[] animalArray = animalList.toArray(new Animal[animalList.size()]);
让我知道它是否有效
0赞
Reilas
10/24/2023
#4
"...我有一个动物的HashMap。我必须创建一个单独的数组,并从 HashMap 中找到某种颜色的动物。所有匹配项都将放入数组中。..."
请改用 Map#values 方法。
Animal[] animalArray = new Animal[0];
for (Animal a : zooAnimals.values())
if (a.getColor().equals("Brown")) {
animalArray = Arrays.copyOf(animalArray, animalArray.length + 1);
animalArray[animalArray.length - 1] = a;
}
(可选)使用流。
Animal[] animalArray
= zooAnimals.values()
.stream()
.filter(x -> x.getColor().equals("Brown"))
.toArray(Animal[]::new);
评论
zooAnimals.get(key)