java - Java流访问List的方法
问题描述
考虑以下代码段
List<Integer> list = new ArrayList<>();
int res = list.stream()
.filter(e-> list.indexOf(e) == list.lastIndexOf(e))
.findFirst()
.get();
// here I'm creating stream on list, and to access methods of list itself I'm using a reference
但是如果我正在使用Arrays.asList
或Arrays.stream
创建流,我该如何访问这些方法。我知道我可以首先将数组转换为集合。但我想要一种更有效的方法来做到这一点。
我错过了关于流的一些东西吗?或者其他的东西。另外,向我建议list.stream
有什么方法可以改善吗?
解决方案
总结评论中的讨论,可能有一种使用数组的方法,但当然不使用 List 方法:
public class Test {
private int[] array; // no setter
public int findNonRepeating(int... arr) {
this.array = arr;
return Arrays.stream(array).filter(this::isNonRepeating).findFirst().orElse(-1);
}
private boolean isNonRepeating(int a) {
int fix = IntStream.range(0, array.length)
.filter(i -> array[i] == a)
.findFirst().orElse(-1);
int lix = IntStream.range(0, array.length)
.map(i -> array.length - i - 1)
.filter(i -> array[i] == a)
.findFirst().orElse(-1);
return fix == lix && fix != -1;
}
}
虽然,更可取的实现方式似乎是这样的:
public int findFirstNonRepeating(int...arr) {
return Arrays.stream(arr)
.boxed()
.collect(
Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet().stream()
.filter(e -> e.getValue() == 1)
.mapToInt(e -> e.getKey())
.findFirst().orElse(-1);
}
更新
为了尽可能地重用List
功能,您可以扩展ArrayList
并提供谓词方法:
public class MyList<T> extends ArrayList<T> {
public static <T> MyList<T> of(Collection<? extends T> collection) {
return new MyList<>(collection);
}
public MyList() { super(); }
public MyList(int initialCapacity) { super(initialCapacity); }
public MyList(Collection<? extends T> collection) { super(collection); }
public boolean isNonRepeating(T x) {
int fx = this.indexOf(x);
return fx > -1 && fx == this.lastIndexOf(x);
}
public T findFirstNonRepeating() {
return stream()
.filter(this::isNonRepeating)
.findFirst()
.orElseThrow(() ->
new NoSuchElementException("This list does not contain non-repeating elements")
);
}
}
int x = MyList.of(Arrays.asList(1, 2, 3, 1)).findFirstNonRepeating().intValue();
// x = 2
推荐阅读
- api - 后端和 API 是一样的吗?什么是后端 Web API?
- sql - 将 Json 表中的数据插入到已经在 SQL 中创建的视图中
- javascript - 如何检查对象数组中的数组中是否存在值?
- firebase - 参数类型“QueryDocumentSnapshot”不能分配给参数类型“AsyncSnapshot”
' - node.js - 当我在创建该文件后使用命令 gatsby new filename 但出现错误时
- javascript - socket.broadcast.to().emit() vs socket.to().emit() vs io.to().emit()
- opengl - 如何在片段着色器中正确绘制其他纹理?
- php - 更新数据 API 在 Postman 中不起作用,错误说 Column 'nama_pelanggan' cannot be null
- database - 添加了验证但没有输入输入数据 Laravel
- flutter - 带有 Flutter 的 JSON API 未获取数据