gpt4 book ai didi

java - 使用流获取整数形成字符串 (Java)

转载 作者:行者123 更新时间:2023-12-02 01:09:16 29 4
gpt4 key购买 nike

我目前正在执行一项任务,我应该编写一个非常简单的“聊天机器人”。目标是一个公共(public) int 方法,它获取一个包含问题作为参数的列表。

每个问题可能包含也可能不包含数字,并且根据聊天机器人应该回答的数字而有所不同。我事先不知道是否存在以及有多少个数字,但可以安全地假设它们将在整数值范围内。如果同一问题中有多个,我还需要创建一个平均数。

现在我需要使用流提取所有不同的数字,然后进行相应的回复。

问题可能如下所示:“今天我的办公室里会有 14 封还是 27 封邮件吗?”

现在我需要一种方法来将该列表中的所有问题中的所有数字分别提取到例如使用流的数组。关于我如何实现这一目标有什么建议吗?代码行数并不重要。它应该相当快,并且由于某些原因我需要使用 java.util.stream 中的流。

最佳答案

您可以按如下方式进行:

import java.util.ArrayList;
import java.util.List;
import java.util.OptionalDouble;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
public static void main(String[] args) {
String str = "Will there be 14 or perhaps 27 mails in my office today?";
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(str);
List<Integer> list = new ArrayList<Integer>();
while (m.find()) {
list.add(Integer.parseInt(m.group()));
}

int sum = 0;
for (int n : list) {
sum += n;
}
System.out.println("Average: " + sum / list.size());

// Using Stream
OptionalDouble average = list.stream().mapToDouble(a -> a).average();
System.out.println("Average: " + (int) average.getAsDouble());

// Another way of doing it using Stream
int sumInts = list.stream().mapToInt(Integer::intValue).sum();
System.out.println("Average: " + sumInts / list.size());
}
}

输出:

Average: 20
Average: 20
Average: 20

更新:发布以下更新以在不使用任何循环的情况下执行此操作:

import java.util.Arrays;
import java.util.List;
import java.util.OptionalDouble;
import java.util.stream.Collectors;

public class Main {
public static void main(String[] args) {
String str = "Will there be 14 or perhaps 27 mails in my office today?";
String newStr = str.replaceAll("[^-0-9]+", " ");
List<String> strList = Arrays.asList(newStr.trim().split(" "));
List<Integer> list = strList.stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList());

// One way
OptionalDouble average = list.stream().mapToDouble(a -> a).average();
System.out.println("Average: " + (int) average.getAsDouble());

// Another way
int sumInts = list.stream().mapToInt(Integer::intValue).sum();
System.out.println("Average: " + sumInts / list.size());
}
}

输出:

Average: 20
Average: 20

另一个更新:根据您在评论中的请求发布另一个更新

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("Will there be 14 or perhaps 27 mails in my office today?");
list.add("There are 40 candidates competing for 4 places");
list.add("There are 6 teams each with 4 different channels");

System.out.println("Numbers in each string:");
list.stream().map(s -> Arrays.asList(s.replaceAll("[^-0-9]+", " ").trim().split(" ")))
.collect(Collectors.toList()).stream()
.map(lst -> lst.stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList()))
.forEach(System.out::println);

System.out.println("Average of numbers in each string:");
list.stream().map(s -> Arrays.asList(s.replaceAll("[^-0-9]+", " ").trim().split(" ")))
.collect(Collectors.toList()).stream()
.map(lst -> lst.stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList()))
.forEach(lst -> System.out.println(lst.stream().mapToInt(Integer::intValue).sum() / lst.size()));
}
}

输出:

Numbers in each string:
[14, 27]
[40, 4]
[6, 4]
Average of numbers in each string:
20
22
5

关于java - 使用流获取整数形成字符串 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59604999/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com