英文:
Finding specific range of number in java
问题
这是我的代码,用于获取分数在70及以上的学生。
public static List<List<String>> higher(List<List<String>> data, String high) {
return data.stream()
.filter(e -> Integer.parseInt(e.get(6)) >= Integer.parseInt(high))
.collect(Collectors.toList());
}
这是我的结果:
[70]
我可以知道如何获取所有分数达到70及以上的学生吗?
英文:
Here is my code to get the students who get 70 and above marks
public static List<List<String>> higher (List<List<String>> data, String high) {
return data.stream()
.filter(e-> e.get(6).equalsIgnoreCase(high))
.collect(Collectors.toList());
}
This is my result
[70]
May I know how to get all the student who gets 70 and above
答案1
得分: 1
这是要翻译的内容:
测试这个:
public static List<List<String>> distinction(List<List<String>> data, Integer high) {
return data.stream()
.filter(e -> Integer.parseInt(e.get(6)) >= high)
.collect(Collectors.toList());
}
high 应该是 70
英文:
Test this:
public static List<List<String>> distinction(List<List<String>> data, Integer high) {
return data.stream()
.filter(e-> Integer.parseInt(e.get(6)) >= high)
.collect(Collectors.toList());
}
high should be 70
答案2
得分: 1
尝试这样做。您需要将字符串转换为整数以进行比较。最好也将高值作为整数传递。
public static List<List<String>> Distinction(List<List<String>> data, int high) {
return data.stream()
.filter(e -> Integer.parseInt(e.get(2)) >= high)
.collect(Collectors.toList());
}
英文:
Try it like this. You need to convert the string to an int for comparison. It would also be best to pass the high as an int.
public static List<List<String>> Distinction(List<List<String>> data, int high) {
return data.stream()
.filter(e-> Integer.parseInt(e.get(2)) >= high)
.collect(Collectors.toList());
}
</details>
# 答案3
**得分**: 1
由于您有一个列表的列表,您需要在`data`上进行flatmap操作,以将列表的列表转换为单个列表。
```java
int threshold = Integer.parseInt(high);
return data.stream()
.flatMap(d -> d.stream()) // 将列表的列表转换为具有任意长度的字符串流
.filter(e -> Integer.parseInt(e.get(2)) >= threshold)
.collect(Collectors.toList());
在这里,我假设您的列表的列表是一系列具有3个元素的列表(根据您提供的输出),其中第3个索引元素(e.get(2))是分数。如果您使用Integer.parseInt将此值转换为Integer,则可以进行比较和过滤操作。
英文:
As you have a list of lists, you need to flatmap over the data to get the list of lists down to a single list.
int threshold = Integer.parseInt(high);
return data.stream()
.flatMap(d -> d.stream()) // converts list of lists to a stream of Strings of arbitrary length
.filter(e -> Integer.parseInt(e.get(2)) >= threshold)
.collect(Collectors.toList());
Here I'm assuming your list of lists is a bunch of 3 element lists (due to the output you provide) where the 3rd indexed element (e.get(2)) is the score. If you convert this value to an Integer using Integer.parseInt you'll be able to compare it for filtering.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论