Yad*_*Yad 1 java arrays arraylist
我有一个包含用户信息数组的ArrayList(~900条目):
[swaschit, Sophia Waschitz, Dormant, Inactive, 1/1/2018]
[kolanday, Kyle Olanday, Dormant, Inactive, 1/1/2018]
[npowers, Neil Powers, Assigned, Active, 2/11/2018]
Run Code Online (Sandbox Code Playgroud)
我想从这个列表中生成一个只包含每个对象的第一个元素的数组:
[swaschit, kolanday, npowers, ...]
Run Code Online (Sandbox Code Playgroud)
这样做最有效的方法是什么?
一种方法是将a Stream和map每个内部数组用于其第一个元素:
List<String> firstElements = yourList.stream()
.map(x -> x[0].toString()) // you might need toString() here if your array is an Object[]
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
如果你想要一个字符串数组:
String[] firstElements = yourList.stream()
.map(x -> x[0].toString()) // you might need toString() here if your array is an Object[]
.toArray(String[]::new);
Run Code Online (Sandbox Code Playgroud)
我还建议你不要使用像这样的嵌套数组.您应该创建一个包含要存储的属性的类,并创建一个List类.