英文:
How to replace a expression when using a filter in Java?
问题
我正在进行一项关于Android的课程作业,它会在屏幕上返回一份外卖列表,列出它们的地址、距离我的位置的距离以及卫生评分。我在设置筛选器以过滤掉不符合条件的评分(即-1)方面遇到了困难。
因此,在这些情况下,我想在屏幕上返回 "exempt"。这是我目前的代码:
for (int i = 0; i < ja.length(); i++) {
JSONObject JO = (JSONObject) ja.get(i);
// 如果餐厅的评分是-1,则显示 "exempt"
if (ratingValue.get(i).equals("-1")) {
System.out.println("Exempt");
}
ConcatenateSearchResults();
// 其他功能的输出
// ...
}
对于"ifPresent",在Java中它是存在的,但在Android中似乎不存在,我尝试过的教程也没有帮助。
在 doInBackground
方法中:
// 遍历外卖数据数组
for (int i = 0; i < ja.length(); i++) {
JSONObject JO = (JSONObject) ja.get(i);
// 如果餐厅的评分是-1,则显示 "exempt"
if (ratingValue.get(i).equals("-1")) {
System.out.println("Exempt");
}
ConcatenateSearchResults();
// 输出满足基本功能要求的数据
// ...
}
希望对你有帮助!
英文:
I am working on a piece of coursework in android, it returns a list of takeaways to the screen, listing their address, the distance from my location, and a hygiene rating and I struggling to set up the filter to filter out ratings that don't meet the criteria i.e -1
Thus in these cases I want to return "exempt" to the screen. This is what I have so far
for (int i = 0; i < ja.length(); i++) {
JSONObject JO = (JSONObject) ja.get(i);
// if the rating of the restaurant is -1, exempt
// should be displayed
ratingValue.stream()
.filter(x->x.equals("-1"))
<< ifPresent.>> (System.out.println("Exempt"));
{
ConcatenateSearchResults();
}
The ifPresent that seems to exist in Java, doesn't seem to exist in Android and the tutorials I tried don't assist me.
protected JSONArray doInBackground(Void... voids) {
// if the searchUrl conversion is empty
if (!searchUrl.toString().isEmpty()) {
// do this instead
try {
URL url = new URL(searchUrl.toString());
URLConnection tc = url.openConnection();
InputStreamReader isr = new InputStreamReader(tc.getInputStream());
BufferedReader in = new BufferedReader(isr);
String line; // variable
// while line in (read in) is not equal to null
// create a new object and output as line in JSON
while ((line = in.readLine()) != null) {
ja = new JSONArray(line);
// run through the length of the array
for (int i = 0; i < ja.length(); i++) {
JSONObject JO = (JSONObject) ja.get(i);
// if the rating of the restaurant is -1, exempt
// should be displayed
ratingValue.stream()
.filter(x -> x.equals("-1"))
.findFirst()
.ifPresent(v -> System.out.println("Exempt"));
ConcatenateSearchResults();
// output to meet Basic Functionality requirement
businessNames.add(JO.getString("BusinessName"));
postCodes.add(JO.getString("PostCode"));
addressList1.add(JO.getString("AddressLine1"));
addressList2.add(JO.getString("AddressLine2"));
addressList3.add(JO.getString("AddressLine3"));
ratingValue.add(JO.getString("RatingValue"));
calcDistance.add(JO.getString("DistanceKM"));
}
}
isr.close();
in.close();
//return ja;
} catch (Exception e) {
this.exception = e;
return ja;
} finally {
//is.close();
}
}
return ja;
}
Cheers
答案1
得分: 0
ifPresent()
在Android平台中没有关联,它是Java的一部分。
在您的情况下,.filter()
方法会返回另一个经过筛选的值流,不是单个值,而是一个流。
所以,您不能在流上使用ifPresent()
,您需要获取单个值。为了使您的代码正确,您可以在ifPresent()
之前简单地添加.findFirst()
,这将从ratingValue
中返回第一个经过筛选的值:
ratingValue.stream()
.filter(x -> x.equals("-1"))
.findFirst()
.ifPresent(v -> System.out.println("Exempt"));
英文:
ifPresent()
doesn't relate to the Android platform, its a part of Java.
In your case, .filter()
the method returns another stream of filtered values, not the single value, but a stream.
So you can't use ifPresent()
on streams, you need to obtain a single value. To make you code correctly, you can simply add .findFirst()
before ifPresent()
, this will return the first filtered value from ratingValue
:
ratingValue.stream()
.filter(x -> x.equals("-1"))
.findFirst()
.ifPresent(v -> System.out.println("Exempt")
专注分享java语言的经验与见解,让所有开发者获益!
评论