英文:
Java: Cannot filter ArrayList using Java-Streams
问题
以下是翻译好的内容:
我遇到的错误
我得到了错误信息:“在类型Stream
我想要做的事情
我试图筛选“xx ArrayList”,但我一直收到这个错误,我不知道为什么。我对Java不太熟悉,所以这可能是我犯的错误。
这是我的代码
import java.util.HashMap;
import java.util.ArrayList;
import java.util.stream.*;
import java.util.Arrays;
public class BusCar
{
// 时间表
String time;
int platform;
ArrayList<Car> xx = new ArrayList<Car>();
String newline = System.getProperty("line.separator");
public BusCar(String depTime, int plat)
{
// 初始化实例变量
time = depTime;
platform = plat;
}
// 文件名,一个包含火车记录的CSV文件。
public void addFromFile(String filename)
{
TrainReader reader = new TrainReader();
xx.addAll(reader.getTrains(filename));
}
public void printBefore(String time)
{
// 打印在给定时间之前出发的所有火车
// 每行一个火车
xx.stream().filter(i -> Integer.parseInt(i.departureTime) < Integer.parseInt(time)).forEach(i -> System.out.println(i + newline));
}
}
请注意,我已经进行了必要的修正,使代码在翻译后保持正确的语法和结构。
英文:
The error I am getting
I am getting the error "The method map(Function<? super Car,? extends R>) in the type Stream<Car> is not applicable for the arguments
What I am trying to do
I am trying to filter the xx ArrayList
but I keep getting this error and I don not or cannot tell why. I am new to Java so this is probably an error I have made
This is my code
import java.util.HashMap;
import java.util.ArrayList;
import java.util.stream.*;
import java.util.Arrays;
public class BusCar
{
// timetable
String time;
int platform;
ArrayList<Car> xx = new ArrayList<Car>();
String newline = System.getProperty("line.separator");
public BusCar(String depTime, int plat)
{
// initialise instance variables
depTime = time;
plat = platform;
}
//filename A CSV file of Train records.
public void addFromFile(String filename)
{
TrainReader reader = new TrainReader();
xx.addAll(reader.getTrains(filename));
}
public void printBefore(String time)
{
// prints all trains which depart before the given time
// one train per line
xx.stream().filter(i -> Integer.parseInt(i.departureTime) < Integer.parseInt(time)).map(i-> System.out.println(i),newline);
}
}
答案1
得分: 1
看起来您想要这样做。您的Car
类需要适当地覆盖toString()
方法:
xx.stream().filter(i -> Integer.parseInt(i.departureTime) <
Integer.parseInt(time)).forEach(System.out::println);
英文:
It appears you want to do this. Your Car
class needs to properly override toString()
xx.stream().filter(i -> Integer.parseInt(i.departureTime) <
Integer.parseInt(time)).forEach(System.out::println);
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论