如何读取 Java 中的多值 ArrayList
How to read MultiValue ArrayList in Java
我定义了一个 arrayList 如下
List<List<RiskyPersons>> dataArray = new ArrayList<>();
这里是 RiskyPersons Class
public class RiskyPersons {
private SA3Tenant sa3tenant;
private int NumberofPersonInCategory;
public RiskyPersons(){
}
public RiskyPersons(SA3Tenant sa3tenant, int NumberofPersonInCategory) {
this.sa3tenant = sa3tenant;
this.NumberofPersonInCategory = NumberofPersonInCategory;
}
}
那么我已经成功添加数据并保存在dataArray
ArrayList中。
以下输出显示使用 SOP(dataArray);
保存的 ArrayList
[[RiskyPersons{sa3tenant=Homeless.SA3Tenant@3a7cc6b0, NumberofPersonInCategory=99}]]
我想读取这个 dataArray
ArrayList 并分别显示值。如何访问 "NumberofPersonInCategory" 值?
您需要将其迭代两次
for (List<RiskyPersons> rp : dataArray) {
for (RiskyPersons o : rp) {
System.out.println(o.NumberofPersonInCategory); // unrelated : but its bad naming convention
}
}
从Java-8及以上可以使用stream:
dataArray.stream()
.flatMap(List::stream)
.map(RiskyPersons::NumberofPersonInCategory)
.forEach(System.out::println)
希望对您有所帮助!
public class RiskyPersons {
private SA3Tenant sa3tenant;
private int NumberofPersonInCategory;
public int getNumberofPersonInCategory() {
return NumberofPersonInCategory;
}
public RiskyPersons(){
}
public RiskyPersons(SA3Tenant sa3tenant, int NumberofPersonInCategory) {
this.sa3tenant = sa3tenant;
this.NumberofPersonInCategory = NumberofPersonInCategory;
}
}
List<Integer> values = dataArray.parallelStream().flatMap(Collection::stream).map(RiskyPersons::getNumberofPersonInCategory)
.collect(Collectors.toCollection(ArrayList::new));
我定义了一个 arrayList 如下
List<List<RiskyPersons>> dataArray = new ArrayList<>();
这里是 RiskyPersons Class
public class RiskyPersons {
private SA3Tenant sa3tenant;
private int NumberofPersonInCategory;
public RiskyPersons(){
}
public RiskyPersons(SA3Tenant sa3tenant, int NumberofPersonInCategory) {
this.sa3tenant = sa3tenant;
this.NumberofPersonInCategory = NumberofPersonInCategory;
}
}
那么我已经成功添加数据并保存在dataArray
ArrayList中。
以下输出显示使用 SOP(dataArray);
ArrayList
[[RiskyPersons{sa3tenant=Homeless.SA3Tenant@3a7cc6b0, NumberofPersonInCategory=99}]]
我想读取这个 dataArray
ArrayList 并分别显示值。如何访问 "NumberofPersonInCategory" 值?
您需要将其迭代两次
for (List<RiskyPersons> rp : dataArray) {
for (RiskyPersons o : rp) {
System.out.println(o.NumberofPersonInCategory); // unrelated : but its bad naming convention
}
}
从Java-8及以上可以使用stream:
dataArray.stream()
.flatMap(List::stream)
.map(RiskyPersons::NumberofPersonInCategory)
.forEach(System.out::println)
希望对您有所帮助!
public class RiskyPersons {
private SA3Tenant sa3tenant;
private int NumberofPersonInCategory;
public int getNumberofPersonInCategory() {
return NumberofPersonInCategory;
}
public RiskyPersons(){
}
public RiskyPersons(SA3Tenant sa3tenant, int NumberofPersonInCategory) {
this.sa3tenant = sa3tenant;
this.NumberofPersonInCategory = NumberofPersonInCategory;
}
}
List<Integer> values = dataArray.parallelStream().flatMap(Collection::stream).map(RiskyPersons::getNumberofPersonInCategory)
.collect(Collectors.toCollection(ArrayList::new));