首页 > 解决方案 > 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.asListArrays.stream创建流,我该如何访问这些方法。我知道我可以首先将数组转换为集合。但我想要一种更有效的方法来做到这一点。

我错过了关于流的一些东西吗?或者其他的东西。另外,向我建议list.stream有什么方法可以改善吗?

标签: javajava-8java-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


推荐阅读