java - 使用 Collections 或我的函数计算 ArrayList 中对象的出现次数

标签 java object collections arraylist find-occurrences

我有一个 FlowerClass 对象的 ArrayList。每个 FlowerClass 对象都有一个名称。我想遍历 ArrayList 并对它们进行计数。我想显示每个的金额。因此,如果我有三个名为 Rose 的 FlowerClass 对象、两个名为 Daffodil 的对象和一个名为 Tulip 的对象...我想显示以下内容:

  • 找到 3 朵玫瑰
  • 找到 3 朵水仙花
  • 找到 3 朵郁金香

到目前为止,我已经使用我制作的两个函数正确计数了。问题是我迭代整个 ArrayList...所以它会多次向我显示结果。例如,如果用户添加 3 朵玫瑰和 2 朵水仙花...输出如下:

  • 找到 3 朵玫瑰
  • 找到 3 朵玫瑰
  • 找到 3 朵玫瑰
  • 找到 2 朵水仙花
  • 找到 2 朵水仙花

我知道代码为什么这样做,但我不知道如何删除重复的输出。我也不知道如何正确实现集合。我之前在字符串的 ArrayList 上使用过 Collections...并且它有效。但这次我将在对象的 ArrayList 上使用集合,并且我想检查每个特定名称的频率。这是主类:

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;

public class MainClass {

    static ArrayList<FlowerClass> flowerPack = new ArrayList<FlowerClass>();


    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        while(true){
            System.out.println("1. Add flower to flowerpack.");
            System.out.println("2. Remove flower from the flowerpack.");
            System.out.println("3. Search for a flower in the flowerpack."); 
            System.out.println("4. Display the flowers in the flowerpack.");
            System.out.println("5. Exit the program.");

            int userChoice = input.nextInt();

            switch(userChoice){
            case 1:
                addFlower();
                break;
            case 2:
                searchFlower();
                break;
            case 3:
                displayFlowers();
                break;
            case 4:
                    System.out.println("Goodbye!");
                System.exit(0);
            }
        }
    }

    public static void addFlower(){
        if (FlowerClass.numberFlowers() == 25){
            System.out.println("There are 25 flowers in the flowerpack. Remove at least one in order to add more.");
            return;
        }
        Scanner input = new Scanner(System.in);
        System.out.println("What is the flower's name?");
        String desiredName = input.nextLine();
        System.out.println("What is the flower's color?");
        String desiredColor = input.nextLine();
        System.out.println("How many thorns does it have?");
        Scanner input2 = new Scanner(System.in);
        int desiredThorns = input2.nextInt();
        System.out.println("What does it smell like?");
        String desiredSmell = input.nextLine();
        flowerPack.add(new FlowerClass(desiredName, desiredColor, desiredThorns, desiredSmell));
    }

    public static void searchFlower(){
        System.out.println("Enter the flower you want to search for.");
        Scanner input = new Scanner(System.in);
        String userChoice = input.nextLine();
        int occurrences  = 0;

        for (FlowerClass flower: flowerPack){
            String name = flower.getName();
            if (userChoice.equals(name)){
                occurrences++;
            }

            else if(occurrences == 0){
                System.out.println("Match not found.");
                return;
            }
        }

        System.out.println("Found " + occurrences + " " + userChoice);
    }

    public static void searchFlower(String desiredFlower){
        int occurrences = 0;

        String userChoice = desiredFlower;
        for (FlowerClass flower: flowerPack){
            String name = flower.getName();
            if (userChoice.equals(name)){
            occurrences++;
            }
        }

        System.out.println("Found " + occurrences + " " + userChoice);
    }

    public static void displayFlowers(){
        int repeats = 0;

        /*for (FlowerClass flower: flowerPack){
            System.out.println(flower.getName());
        }
        System.out.println("Number of flowers in pack: " + FlowerClass.numberFlowers());*/

            //int occurrences = Collections.frequency(flowerPack, name);
            //System.out.println(name + ": " + occurrences);
        for (FlowerClass flower: flowerPack){
            String name = flower.getName();
            searchFlower(name);
        }
    }
}

这是 FlowerClass:

public class FlowerClass {

    public static int numberOfFlowers = 0;
    public String flowerName = null;
    public String flowerColor = null;
    public int numberThorns = 0;
    public String flowerSmell = null;

    FlowerClass(){

    }

    FlowerClass(String desiredName, String desiredColor, int desiredThorns, String desiredSmell){
        flowerName = desiredName;
            flowerColor = desiredColor;
        numberThorns = desiredThorns;
        flowerSmell = desiredSmell;
        numberOfFlowers++;
    }

    public void setName(String desiredName){
        flowerName = desiredName;

    }

    public String getName(){
        return flowerName;
    }

    public static int numberFlowers(){
        return numberOfFlowers;
    }
}

如果您查看主类中的最后一个函数,您会发现我注释掉了我尝试实现 Collections.Frequency 的方式。我还尝试制作一个多维字符串数组,并在数组中存储花的名称和花的数量。这正确地计算了所有内容,但我不确定如何在计数旁边显示名称。它变得非常困惑,所以我暂时放弃了这种尝试,转而尝试其他两种选择。如果我能找到一种方法来删除重复的输出行(或者如果我能找到一种让集合工作的方法),那么我就不需要修改多维数组。

任何提示将不胜感激。感谢您的时间。

最佳答案

有趣的代码,但它并不像我想要的那样工作。

在当前的情况下,正如您所做的那样,您需要跟踪已经遇到的花名称:

public static void displayFlowers(){
    //int repeats = 0;
    List<String> displayedFlowerTypes = new ArrayList<String>();
    for (FlowerClass flower: flowerPack){
        String name = flower.getName();
        if(!displayedFlowerTypes.contains(name))
        {
            displayedFlowerTypes.add(name);
            searchFlower(name);
        }
    }
}

我宁愿做的是维护一个 map 来跟踪花类型的计数,并从中获取类型的数字:

public class MainClass {

static List<FlowerClass> flowerPack = new ArrayList<FlowerClass>();
static Map<String, Integer> flowerCount = new HashMap<String, Integer>();

public static void addFlower() {
    if (FlowerClass.numberFlowers() == 25) {
        System.out.println("There are 25 flowers in the flowerpack. Remove at least one in order to add more.");
        return;
    }
    Scanner input = new Scanner(System.in);
    System.out.println("What is the flower's name?");
    String desiredName = input.nextLine();
    System.out.println("What is the flower's color?");
    String desiredColor = input.nextLine();
    System.out.println("How many thorns does it have?");
    Scanner input2 = new Scanner(System.in);
    int desiredThorns = input2.nextInt();
    System.out.println("What does it smell like?");
    String desiredSmell = input.nextLine();
    flowerPack.add(new FlowerClass(desiredName, desiredColor, desiredThorns, desiredSmell));
    if(!flowerCount.containsKey(desiredName))
    {
        flowerCount.put(desiredName, 1);
    }
    else
    {
        int currentCount = flowerCount.get(desiredName);
        flowerCount.put(desiredName, currentCount+1));
    }
}

这样,您就可以按如下方式显示花朵:

    public static void displayFlowers() {
        for (String name : flowerCount.keySet()) {
            //searchFlower(name);
            System.out.println("Found " + flowerCount.get(name) + " " + name);
        }
    }

关于java - 使用 Collections 或我的函数计算 ArrayList 中对象的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24337786/

相关文章:

java - 求和超时如何解决?

javascript - JS Lodash : Sort object by value, 最后为空

java - 为什么它不交出我的东西?

带有 groupby、count 和 sum 的 Laravel 集合

java - 如何在 JAVA 中创建异步 HTTP 请求?

java - string.replaceAll 替换出现的地方

sql-server - 尽管架构是用户的默认架构,但仍出现无效对象错误

C++ 线程安全映射

java - 在 HashMap 中添加到 List 的快捷方式

java - 如何在Web环境下禁用log4j?