首页 > 解决方案 > 如何根据最小数量从 txt 文件中获取 5 个项目

问题描述

我想从文本文件中访问具有最小数量的组的 5 个项目

我可以访问该组的前 5 个项目,但不能访问该特定组的最少项目

        List<String> itemsWithMinQuantity = new ArrayList<String>();
        String lineRead;
        int requiredItemsInGroup = 5;
        FileReader fileReader = null;
        try {
            fileReader = new FileReader("file path");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        BufferedReader bufferedReader = new BufferedReader(fileReader);
        while ((lineRead = bufferedReader.readLine()) != null) {
            if (lineRead.contains(("Group ID : " + groupID))) {

                if (requiredItemsInGroup != 0) {
                    itemsWithMinQuantity.add(lineRead);
                } else {
                    break;

                }
                requiredItemsInGroup--;
            }
        }

        if (itemsWithMinQuantity.isEmpty()) {
            return Collections.singletonList("No items in entered group No.");

        } else {
            return itemsWithMinQuantity;
        }
    }

预期:它应该根据组的最小数量向我们返回 5 个项目及其组 ID 和数量

实际的

"Group ID : 1 Quantity : 5 Item Title : MUCHAE NAMUL (DAIKON)",

"Group ID : 1 Quantity : 0 Item Title : LUSH LEMON DRIZZLE!",

"Group ID : 1 Quantity : 0 Item Title : CHOCOLATE GRAVY",

"Group ID : 1 Quantity : 0 Item Title : MICHAEL SYMON'S CHICKEN CUTLET MILANESE WITH ARUGULA SALAD",

"Group ID : 1 Quantity : 0 Item Title : CLASSIC BEEF BRAISE"

标签: javafile-handling

解决方案


首先,我创建了一个简单的 POJO 来保存数量和项目数据(整行)。

private static class Item {
    private int quantity;
    private String itemData;

    private Item(String itemData, int quantity) {
        this.itemData = itemData;
        this.quantity = quantity;
    }

    public int getQuantity() {
        return quantity;
    }

    public String getItemData() {
        return itemData;
    }
}

阅读每个项目(属于所需组)并提取数量。使用此数据为每一行创建一个 Item 对象。

接下来,按数量对项目进行排序 ( Comparator.comparing(Item::getQuantity))。

这样,您就可以按数量对所有项目进行排序。剩下的就是打印该列表的前 5 个项目。

List<Item> items = new ArrayList<>();
String lineRead;
FileReader fileReader = null;
try {
    fileReader = new FileReader("...");
} catch (FileNotFoundException e) {
    e.printStackTrace();
    throw new RuntimeException(e);
}

BufferedReader bufferedReader = new BufferedReader(fileReader);
Pattern pattern = Pattern.compile("Quantity : (\\d+)");
while ((lineRead = bufferedReader.readLine()) != null) {
    if (lineRead.contains(("Group ID : " + groupId))) {
        Matcher matcher = pattern.matcher(lineRead);
        int quantity;
        if (matcher.find())
        {
            quantity = Integer.parseInt(matcher.group(1));
        } else {
            throw new RuntimeException("Unexpected data format. Quantity not found");
        }
        Item item = new Item(lineRead, quantity);
        items.add(item);
    }
}
items.sort(Comparator.comparing(Item::getQuantity));

items.stream()
        .limit(5)
        .forEach(item -> System.out.println(item.getItemData()));

正则表达式Quantity : (\\d+)匹配单词Quantity后跟数字的字符串。通过获得第一个匹配组,我们仅获得数量的值。

Comparing 方法参考只是传统比较器的优雅表示。

items.sort(new Comparator<Item>() {
    @Override
    public int compare(Item o1, Item o2) {
        return Double.compare(o1.getQuantity(), o2.getQuantity());
    }
});

推荐阅读