如何使用 Java 流获取存储在 class 对象的 Arraylist 中的字符串和 ArrayList

How to get an String and the ArrayList stored in a Arraylist of objects of a class using Java stream

我修改了代码并尝试在特定条件下(例如 'str' 字符串等于 2)获取 ArrayList 和存储在对象 Arraylist 中的字符串。我无法将 Stream 转换为 ArrayList。请帮助我了解需要做什么才能从此流中获取 ArrayList。

我有一个 class 'SampleClass' 如下所示:

import java.util.ArrayList;

public class SampleClass {
String str;
ArrayList<String> al;
String check;

public SampleClass(String str, ArrayList<String> al, String check) {
    super();
    this.str = str;
    this.al = al;
    this.check = check;
}
public String getStr() {
    return str;
}
public void setStr(String str) {
    this.str = str;
}
public ArrayList<String> getAl() {
    return al;
}
public void setAl(ArrayList<String> al) {
    this.al = al;
}
public String getCheck() {
    return check;
}
public void setCheck(String check) {
    this.check = check;
}

}

我有另一个 class 'GetTheArrayListStoredInAnotherArrayList',如下所示,我试图将 ArrayList 存储在对象的 ArrayList 中。不对的地方请指正

import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;

public class GetTheArrayListStoredInAnotherArrayList{

public static void main(String[] args) {
    String test = "qw,rer,try,try,erh5,wertgw45t,45";
    ArrayList<String> al = new ArrayList<String>();
    al.addAll(new ArrayList<String>(Arrays.asList(test.split(","))));
    System.out.println(al);
    ArrayList<SampleClass> sca = new ArrayList<SampleClass>();
    SampleClass sc1 = new SampleClass("1", al,"ch1");
    SampleClass sc2 = new SampleClass("2", al,"cc2");
    SampleClass sc3 = new SampleClass("3", al,"fr3");
    SampleClass sc4 = new SampleClass("4", al,"fg4");
    sca.add(sc1);
    sca.add(sc2);
    sca.add(sc3);
    sca.add(sc4);
    ArrayList<String> als1 = null;
    ArrayList<String> als = sca.stream().filter( s -> s.getStr().equals("2")).flatMap(sc -> sc.getAl().stream()).collect(Collectors.toCollection(ArrayList::new));
    System.out.println(als);
    String ch = (String) sca.stream().filter(s -> s.getStr().equals("1")).map(ac -> ac.getCheck());
    System.out.println(ch);
}
}

我在执行代码时遇到以下错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Cannot cast from Stream<String> to String

at GetTheArrayListStoredInAnotherArrayList.main(GetTheArrayListStoredInAnotherArrayList.java:24)

改变

ArrayList<String> als = sca.stream().filter( s -> s.getStr().equals("2")).flatMap( sc -> sc.getAl());   

ArrayList<String> als = sca.get(0).getAl();

不完全确定您要做什么,但您需要稍微更改一下代码:

         List<String> als = sca.stream()
             .filter(s -> s.getStr().equals("2"))
             .flatMap(sc -> sc.getAl().stream())
             .collect(Collectors.toList());

一些事情:

flatMap 必须 return 一个 Stream(在你的情况下你 returning 一个 List

Collectors.toList 不保证 return 中的 List,因此赋值给 List,而不是 ArrayList

编辑

这个:

  Stream<String> stream = sca.stream().filter(s -> s.getStr().equals("1"))
                                   .map(ac -> ac.getCheck());

将产生 Stream<String>。您不能简单地将其转换为 String,您必须将其转换为 collect/reduce 任何您想要的。比如说 List:

  List<String> list = sca.stream()
        .filter(s -> s.getStr().equals("1"))
        .map(ac -> ac.getCheck())
        .collect(Collectors.toList());

或单个 String 例如:

  String r = sca.stream()
        .filter(s -> s.getStr().equals("1"))
        .map(ac -> ac.getCheck())
        .collect(Collectors.joining(","));

这实际上是基础知识...您应该真正研究一些示例和文档。

I'm trying to get the ArrayList stored inside the ArrayList of objects.

嗯,基本算法如下:过滤sca所以它只留下str是“2”的元素->从所有剩下的元素中获取一个元素->得到al 存储在该元素内。

您已正确完成第一部分:

sca.stream().filter( s -> s.getStr().equals("2"))

但是现在你需要从过滤后的结果中获取单个元素(过滤后可能会遗留多个元素),所以你调用findFirst:

.findFirst().get()

如果过滤器后没有剩余元素,此 get 调用将抛出异常。如果你不希望它抛出异常,你可以用 orElse 调用替换它:

.findFirst.orElse(new SampleClass("", null))

如果您使用 orElse,如果没有 str 为“2”的元素,方法链将评估为 null

现在你只需要通过调用getAl():

来获取数组列表
.getAl();

现在我们将所有这些结合在一起:

ArrayList<String> als = sca.stream()
    .filter( s -> s.getStr().equals("2"))
    .findFirst().orElse(new SampleClass("", null)).getAl();

首先你必须使用List而不是ArrayList。所以使用 List 你的代码看起来像

List<String> als1 = null;
    List<String> als = sca.stream().
            filter(s -> s.getStr().equals("2")).  //Comparing 
            map(s -> s.getAl())  // Converting List<SampleClass> to list of all al list inside all SampleClass in format List<List<Straing>> 
            .flatMap(ArrayList::stream)  //Creating a flat list from list of list of List ::  List<List<Straing>> --To--> List<String>
            .collect(Collectors.toList());  // Collecting as list

我已经详细评论了这段代码。但是这里如果列表中有两个 SampleCalss 对象 str=2 那么它将合并两个对象的 al 列表。希望对你有帮助。