簡體   English   中英

從一個特定元素中查找所有值

[英]Finding all the values from one specific element

我有一個動物列表和一些關於它們的信息,如果我希望用戶能夠通過輸入動物的名稱來查找動物及其信息,我該怎么辦? 我將動物存儲在這樣的列表中:

List<Animal> an = new ArrayList<Animal>();
Animal a4 = new Animal();
a4.add("Tiffany", 10, "Giraffe", "Grass");
a4.add("Mayo", 30, "Elephant", "Water");
a4.add("Simba", 30, "Turtle", "Leaves");

我假設你有這樣的Animal class :

public class Animal {
    String name;
    int age;
    String breed;
    String eats;

   // getters, setters, constructor with fields

}

與 Java 8:

List<Animal> an = new ArrayList<Animal>();
an.add(new Animal("Tiffany", 10, "Giraffe", "Grass"));
an.add(new Animal("Mayo", 30, "Elephant", "Water"));
an.add(new Animal("Simba", 30, "Turtle", "Leaves"));

// String name = "Mayo"; // commented to get the user input with Scanner
Scanner scan = new Scanner(System.in);
System.out.println("Enter the animal's name, please...");
String name = scan.nextLine();

Optional<Animal> foundAnimal = an.stream().filter(animal -> animal.getName().equals(name)).findFirst();

if (foundAnimal.isPresent()) { // if the animal is in the list
    System.out.println("Animals name: " + foundAnimal.get().getName() + "\n" + "Animals age: "
            + foundAnimal.get().getAge());
}

您也可以通過使用簡單的foreach循環來實現這一點:

List<Animal> an = new ArrayList<Animal>();
an.add(new Animal("Tiffany", 10, "Giraffe", "Grass"));
an.add(new Animal("Mayo", 30, "Elephant", "Water"));
an.add(new Animal("Simba", 30, "Turtle", "Leaves"));

//String name = "Mayo"; // commented to get the user input with Scanner
Scanner scan = new Scanner(System.in);
System.out.println("Enter the animal's name, please...");
String name = scan.nextLine();

for (Animal animal : an) {
    if (animal.getName().equals(name)) {
        System.out.println("Animals name: " + animal.getName() + "\n" + "Animals age: " + animal.getAge());
    }
}

Output:

Animals name: Mayo
Animals age: 30

使用 Map

Map<String,Animal> animalsMap=new HashMap<String,Animal>();
animalsMap.put("Tiffany",new Animal("Tiffany", 10, "Giraffe", "Grass"));
animalsMap.put("Tiffany",new Animal("Mayo", 30, "Elephant", "Water"));
animalsMap.put("Simba",new Animal("Simba", 30, "Turtle", "Leaves"));

然后得到動物用途:

動物地圖.get("辛巴")

如果您將列表與 map 結合使用,這可以與稱為 simba 的多種動物一起使用:

Map<String,List<Animal>> animalsMap=new HashMap<String,Animal>();
List<Animal> animalList = new ArrayList<Animal>();
animalList.add(new Animal()
Animal a4 = new Animal("Tiffany", 10, "Giraffe", "Grass");
animalList.add(a4);
Animal a4 = new Animal("Tiffany", 11, "boar", "meat");
animalList.add(a4);
animalsMap.put("Tiffany",animalList );

List<Animal> animalList = new ArrayList<Animal>();
animalList.add(new Animal()
Animal a4 = new Animal("simba", 10, "Giraffe", "Grass");
animalList.add(a4);
Animal a4 = new Animal("simba", 11, "boar", "meat");
animalList.add(a4);
animalsMap.put("simba",animalList );

最后,如果您堅持使用列表:

Animal result;
list.stream().forEach((a)->{if (a.getName().equals("simba") {result=a});});

你的邏輯有問題。 您已經創建了一個 ArrayList ,它將存儲 Animal class 對象,因此,您需要使用 an.add(new Animal (parameters / details)) 並稍后對其進行迭代。 使用以下代碼作為參考 -

import java.util.ArrayList;
import java.util.List;

public class Animal {

    private String name;
    private int num;
    private String type;
    private String food;

    public Animal(String name, int num, String type, String food) {
        super();
        this.name = name;
        this.num = num;
        this.type = type;
        this.liveon = food;
    }
    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Animal [name=" + name + ", num=" + num + ", type=" + type + ", 
                        food=" + food + "]";
    }

    public static void main(String [] args) {
        List<Animal> an = new ArrayList<Animal>();
        
        // Add Address objects to the ArrayList
        an.add(new Animal("Tiffany", 10, "Giraffe", "Grass"));
        an.add(new Animal("Mayo", 30, "Elephant", "Water"));
        an.add(new Animal("Simba", 30, "Turtle", "Leaves"));

        // Iterate over the ArrayList
        for(Animal a:an){
            if(a.getName() == "Simba"){
                System.out.println(a.toString());
            }
        }
    
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM