Java:无法使用Java Streams对ArrayList进行筛选

huangapple go评论117阅读模式
英文:

Java: Cannot filter ArrayList using Java-Streams

问题

以下是翻译好的内容:

我遇到的错误

我得到了错误信息:“在类型Stream中,方法map(Function<? super Car, ? extends R>)不适用于参数”

我想要做的事情

我试图筛选“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 &quot;The method map(Function&lt;? super Car,? extends R&gt;) in the type Stream&lt;Car&gt; 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&lt;Car&gt; xx = new ArrayList&lt;Car&gt;();

    String newline = System.getProperty(&quot;line.separator&quot;);
    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 -&gt; Integer.parseInt(i.departureTime) &lt; Integer.parseInt(time)).map(i-&gt; 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 -&gt; Integer.parseInt(i.departureTime) &lt; 
   Integer.parseInt(time)).forEach(System.out::println);

</details>



huangapple
  • 本文由 发表于 2020年10月21日 19:00:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64462133.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定