不能将两个流合并以一个在图形界面中显示

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

cant combine two streams to display as one in GUI

问题

我正在过滤游戏和游戏机,然后我想在GUI中同时显示它们,一次只显示一个流,两个流都按照我想要的方式运行,我只是不知道如何将游戏和游戏机的集合合并起来。

// 两个流都在循环遍历一个包含了游戏机、游戏、名称等信息的租赁数组

// 过滤掉游戏的名称

filteredListRentals = (ArrayList) listOfRentals.stream().
filter(n -> n instanceof Game)
.collect(Collectors.toList());

// 过滤掉游戏机的名称

filteredListRentals = (ArrayList) listOfRentals.stream().
filter(n -> n instanceof Console)
.collect(Collectors.toList());

// 根据租赁者的名称排序,这与显示的流一起工作

Collections.sort(fileredListRentals, (n1, n2) ->
n1.getNameOfRenter().compareTo(n2.nameOfRenter));

英文:

I am filtering out games and consoles but then I want to display them both in a GUI, only one of the streams will display at a time, both streams do what I want them to I just can't find out how to combine a collection of games and consoles

//both streams cycle through an array of Rentals that have the consoles, games, names, etc

//filters out the name of games

    filteredListRentals = (ArrayList<Rental>) listOfRentals.stream().
                    filter(n -> n instanceof Game)
                    .collect(Collectors.toList());

//filters out the name of the consoles

    filteredListRentals = (ArrayList<Rental>) listOfRentals.stream().
                    filter(n -> n instanceof Console)
                    .collect(Collectors.toList());

//sorts by the name of costumers, this works along with the stream being displayed

   Collections.sort(fileredListRentals, (n1, n2) ->
          n1.getNameOfRenter().compareTo(n2.nameOfRenter));

答案1

得分: 1

你可以在筛选条件中使用 OR 运算符,以便在结果中包含游戏和游戏主机,就像这样:

filteredListRentals = (ArrayList<Rental>) listOfRentals.stream()
    .filter(n -> n instanceof Game || n instanceof Console)
    .sorted((n1, n2) -> n1.getNameOfRenter().compareTo(n2.nameOfRenter))
    .collect(Collectors.toList());

(在这种情况下,删除第二个流和筛选的赋值语句至filteredListRentals。)

英文:

You can use OR inside your filter to have Game and Console in the result, like this:

filteredListRentals = (ArrayList&lt;Rental&gt;) listOfRentals.stream()
    .filter(n -&gt; n instanceof Game || n instanceof Console)
    .sorted((n1, n2) -&gt; n1.getNameOfRenter().compareTo(n2.nameOfRenter))
    .collect(Collectors.toList());

(Remove in this case the second stream & filter assignment to filteredListRentals.)

答案2

得分: 0

你将两个流的结果赋值给同一个变量,所以第二个流将取代第一个流。

如果你想要两个结果,你需要两个变量。然后你可以将一个结果追加到另一个结果上。

逻辑可能是这样的:

allRentals = (ArrayList<Rental>) listOfRentals.stream().filter(n -> n instanceof Game)...
consoleRentals = (ArrayList<Rental>) listOfRentals.stream().filter(n -> n instanceof Console)...
allRentals.addAll(consoleRentals);
英文:

You assign the results of both streams to the same variable, so the second stream replaces the first stream.

If you want two results you need two variables. Then you can append the results of one to the other.

The logic might be something like:

allRentals = (ArrayList&lt;Rental&gt;) listOfRentals.stream().filter(n -&gt; n instanceof Game)...
consoleRentals = (ArrayList&lt;Rental&gt;) listOfRentals.stream().filter(n -&gt; n instanceof Console)...
allRentals.addAll(consoleRentals);

答案3

得分: 0

感谢大家的帮助,这确实都引导我朝着正确的方向去解决它。

fileredListRentals = (ArrayList<Rental>) listOfRentals.stream().
                filter(n -> n instanceof Game)
                .collect(Collectors.toList());
        Collections.sort(fileredListRentals, (n1, n2) ->
                n1.getNameOfRenter().compareTo(n2.nameOfRenter));

filteredConsoles = (ArrayList<Rental>) listOfRentals.stream().
                filter(n -> n instanceof Console)
                .collect(Collectors.toList());
        Collections.sort(filteredConsoles, (n1, n2) ->
                n1.getNameOfRenter().compareTo(n2.nameOfRenter));

        fileredListRentals.addAll(filteredConsoles);
英文:

thank you guys for your help it definitely all lead me in the right direction to figure it out

    fileredListRentals= (ArrayList&lt;Rental&gt;) listOfRentals.stream().
                    filter(n -&gt; n instanceof Game)
                    .collect(Collectors.toList());
            Collections.sort(fileredListRentals, (n1, n2) -&gt;
                    n1.getNameOfRenter().compareTo(n2.nameOfRenter));

    filteredConsoles = (ArrayList&lt;Rental&gt;) listOfRentals.stream().
                    filter(n -&gt; n instanceof Console)
                    .collect(Collectors.toList());
            Collections.sort(filteredConsoles,(n1,n2)-&gt;
                    n1.getNameOfRenter().compareTo(n2.nameOfRenter));

            fileredListRentals.addAll(filteredConsoles);

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

发表评论

匿名网友

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

确定