![](/img/trans.png)
[英]Convert List<Person> to Map<Integer, List<Integer>> using Lambdas
[英]How To Parse A List Of Strings And Add Values to a Map<String,Integer> Using Lambdas
在我的控制台项目中,我在一行中收到输入 {String resource} {int quantity} (可以收到几对示例:3 Motes 5 stones 5 Shards
我的目标是重构 while 循环的主体以使用尽可能多的 lambda。 我很想减少大量的 if-else 语句并使用谓词,但我是一个新手,我更想寻找其他我可以尝试的指示。
我已经检查了收集器的 javadoc,Arrays.stream() 并通读了 Oracle 的 JDK 8 lambda 和函数式编程中的教程,但显然需要更多实际示例。
目标是为其中一个传奇 Map 密钥获取 250 个资源并停止该程序。 我用 Scanner 解析这些值,用一个空格分隔并按如下方式实现我的逻辑:
private final static int REQUIRED_QUANTITY = 250;
private static boolean gameOver = false;
Map<String, String> legendary = new HashMap<>();
legendary.put("shards", "Shadowmourne");
legendary.put("fragments", "Valanyr");
legendary.put("motes", "Dragonwrath");
Map<String, Integer> resources = new HashMap<>();
while (true) {
String[] loot = scanner.nextLine().toLowerCase().split(" ");
String material = null;
int quantity = 0;
for (int i = 0; i < loot.length; i++) {
if (i % 2 != 0) {
material = loot[i];
} else {
quantity = Integer.parseInt(loot[i]);
}
if (material != null && quantity != 0) {
if (resources.containsKey(material.toLowerCase())) {
resources.replace(material, resources.get(material) + quantity);
}
resources.putIfAbsent(material.toLowerCase(), quantity);
material = null;
quantity = 0;
}
}
resources.forEach((s, integer) -> {
if (integer > REQUIRED_QUANTITY) {
System.out.println("Legendary obtained: " + legendary.get(s));
gameOver = true;
}
});
我做了一些改变。
compute
来处理更新 mapprivate final static int REQUIRED_QUANTITY = 250;
private static boolean gameOver = false;
Map<String, String> legendary = new HashMap<>();
legendary.put("shards", "Shadowmourne");
legendary.put("fragments", "Valanyr");
legendary.put("motes", "Dragonwrath");
Map<String, Integer> resources = new HashMap<>();
Scanner scanner = new Scanner(System.in);
while (!gameOver) {
String[] loot =
scanner.nextLine().toLowerCase().split("\\s+");
for (int i = 0; i < loot.length; i+=2) {
String material = loot[i+1];
if (!legendary.containsKey(material)) {
// do some error processing
}
int quantity = Integer.parseInt(loot[i]);
// you may want to catch exception here.
resources.compute(material.toLowerCase(),
(k, v) -> v == null ? quantity : v + quantity);
}
resources.forEach((s, integer) -> {
if (integer > REQUIRED_QUANTITY) {
System.out.println("Legendary obtained: "
+ legendary.get(s));
gameOver = true;
}
});
}
我已经将您提供的代码封装到一个 class 中。
Map resources
重命名并更改为实例变量Map<String, Integer> resourceToQuantity
。 标志gameOver
也被制作成一个实例变量。
主要的游戏逻辑位于collectResources()
方法中,主要的增强功能在此处进行:
gameOver
用作while loop
的退出条件;nested for loop
的条件已更改;for loop
,删除了if/else
语句;quantity
和material
的scope;quantity
并根据REQUIRED_QUANTITY
检查总数;gameOver
被设置为true
并且游戏循环退出。public class LootHunter {
private final static int REQUIRED_QUANTITY = 250;
private static final Map<String, String> legendary = Map.of(
"shards", "Shadowmourne",
"fragments", "Valanyr",
"motes", "Dragonwrath"
);
private final Map<String, Integer> resourceToQuantity;
private boolean gameOver;
public LootHunter() {
this.resourceToQuantity = new HashMap<>();
}
public void collectResources() {
while (!gameOver) {
Scanner scanner = new Scanner(System.in);
String[] loot = scanner.nextLine().toLowerCase().split(" ");
for (int i = 0; i < loot.length - 1; i += 2) {
int quantity = Integer.parseInt(loot[i]);
String material = loot[i + 1];
int totalQuantity = resourceToQuantity.merge(material, quantity, Integer::sum); // merges quantities and returns the sum
if (totalQuantity > REQUIRED_QUANTITY) {
System.out.printf("You win! %d of %s have been collected.%n", totalQuantity, material);
System.out.println("Legendary item obtained: " + legendary.get(material));
gameOver = true;
break;
}
}
}
printAllResources(); // prints all the collected resources
}
public void printAllResources() {
System.out.println("List of resources collected:");
resourceToQuantity.forEach((material, quantity) ->
System.out.printf("%s: %d%n", material, quantity));
}
public static void main(String[] args) {
LootHunter lootHunter = new LootHunter();
lootHunter.collectResources();
}
}
输入:
"30 shards 10 fragments 50 motes 80 fragments 70 shards 180 fragments"
output
You win! 270 of fragments have been collected.
Legendary item obtained: Valanyr
List of resources collected:
shards: 100
motes: 50
fragments: 270
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.