java - 如何根据最小数量从 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"
解决方案
首先,我创建了一个简单的 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());
}
});
推荐阅读
- linux - FFMPEG + V4L2 选项
- c++ - 如何返回向量的 2 个索引?
- sql - 如果先前日期中存在新值,则计算重复项
- python - Python请求获取nse印度网站的返回响应代码401
- python - 在我的情况下,是否有在 python 中使用多重继承的替代方法?
- security - 用于国际验证码的字母表
- azure - 将具有属性的不同类型的文件从 Gen1 Azur 湖复制到 Azur Gen2 湖(如上次更新)
- spring-boot - JpaObjectRetrievalFailureException: Unable to find myapp.server.layer3persistence.UserData with id xxx
- python - AssertionError: 404 != 200 在 Django 中失败
- react-native - React Native 中不同屏幕之间的导航