Java集合工具:列表查找之position和find

操作集合时,查找列表中的元素是常见操作,本文记录两个平常使用的List查找工具方法。

使用示例

    @Test
    public void testPositionAndFind() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        //查找list中2的下标,不存在则为-1
        Integer twoIndex = CollectionUtil.position(list, 2).orElse(-1);
        //查找list中index为3(包含)到5(不包含)之间是否存在0
        boolean zeroPresent = CollectionUtil.position(list, 0, 3, 5).isPresent();
        //同上
        zeroPresent = CollectionUtil.find(list, 0, 3,5 ).isPresent();
        //查找list中第一个偶数,不存在就是-1
        Integer evenNum = CollectionUtil.findIf(list, i -> i % 2 == 0).orElse(-1);

        assertEquals((int) CollectionUtil.position(list, 1).get(), 0);
        assertEquals((int) CollectionUtil.position(list, 2).get(), 1);
        assertEquals((int) CollectionUtil.position(list, 20).orElse(-1), -1);
        assertTrue(CollectionUtil.position(list, 2).isPresent());
        assertFalse(CollectionUtil.position(list, 20).isPresent());

        assertTrue(CollectionUtil.position(list, 2, 1).isPresent());
        assertFalse(CollectionUtil.position(list, 2, 3).isPresent());
        assertTrue(CollectionUtil.position(list, 6, 0, 6).isPresent());
        assertFalse(CollectionUtil.position(list, 6, 0, 5).isPresent());

        assertEquals((int) CollectionUtil.positionIf(list, i -> Objects.equals(i, 1)).get(), 0);
        assertEquals((int) CollectionUtil.positionIf(list, i -> Objects.equals(i, 2)).get(), 1);
        assertEquals((int) CollectionUtil.positionIf(list, i -> Objects.equals(i, 20)).orElse(-1), -1);
        assertTrue(CollectionUtil.positionIf(list, i -> Objects.equals(i, 2)).isPresent());
        assertFalse(CollectionUtil.positionIf(list, i -> Objects.equals(i, 20)).isPresent());

        assertTrue(CollectionUtil.positionIf(list, i -> Objects.equals(i, 2), 1).isPresent());
        assertFalse(CollectionUtil.positionIf(list, i -> Objects.equals(i, 2), 3).isPresent());
        assertTrue(CollectionUtil.positionIf(list, i -> Objects.equals(i, 6), 0, 6).isPresent());
        assertFalse(CollectionUtil.positionIf(list, i -> Objects.equals(i, 6), 0, 5).isPresent());

        assertEquals((int) CollectionUtil.find(list, 1).get(), 1);
        assertEquals((int) CollectionUtil.find(list, 2).get(), 2);
        assertEquals((int) CollectionUtil.find(list, 20).orElse(-1), -1);
        assertTrue(CollectionUtil.find(list, 2).isPresent());
        assertFalse(CollectionUtil.find(list, 20).isPresent());

        assertTrue(CollectionUtil.find(list, 2, 1).isPresent());
        assertFalse(CollectionUtil.find(list, 2, 3).isPresent());
        assertTrue(CollectionUtil.find(list, 6, 0, 6).isPresent());
        assertFalse(CollectionUtil.find(list, 6, 0, 5).isPresent());

        assertEquals((int) CollectionUtil.findIf(list, i -> Objects.equals(i, 1)).get(), 1);
        assertEquals((int) CollectionUtil.findIf(list, i -> Objects.equals(i, 2)).get(), 2);
        assertEquals((int) CollectionUtil.findIf(list, i -> Objects.equals(i, 20)).orElse(-1), -1);
        assertTrue(CollectionUtil.findIf(list, i -> Objects.equals(i, 2)).isPresent());
        assertFalse(CollectionUtil.findIf(list, i -> Objects.equals(i, 20)).isPresent());

        assertTrue(CollectionUtil.findIf(list, i -> Objects.equals(i, 2), 1).isPresent());
        assertFalse(CollectionUtil.findIf(list, i -> Objects.equals(i, 2), 3).isPresent());
        assertTrue(CollectionUtil.findIf(list, i -> Objects.equals(i, 6), 0, 6).isPresent());
        assertFalse(CollectionUtil.findIf(list, i -> Objects.equals(i, 6), 0, 5).isPresent());
    }

API介绍

  • Optional find(List list, T item):

    查找list中和item equals的元素,不存在就返回Optional.empty();

  • Optional find(List list, T item, int start):

    同上,从下标为start的位置开始(包含start);

  • Optional find(List list, T item, int start, int end):

    同上,从下标为start的位置开始(包含start),到下标为end的位置结束(不包含end);

  • Optional findIf(List list, Predicate predicate):

    查找list中满足predicate 的元素,不存在就返回Optional.empty();

  • Optional findIf(List list, Predicate predicate, int start):

    同上,从下标为start的位置开始(包含start);

  • Optional findIf(List list, Predicate predicate, int start, int end):

    同上,从下标为start的位置开始(包含start),到下标为end的位置结束(不包含end);

  • Optional position(List list, T item):

    查找list中和item equals的元素的下标,不存在就返回Optional.empty();

  • Optional position(List list, T item, int start):

    同上,从下标为start的位置开始(包含start);

  • Optional position(List list, T item, int start, int end):

    同上,从下标为start的位置开始(包含start),到下标为end的位置结束(不包含end);

  • Optional positionIf(List list, Predicate predicate):

    查找list中满足predicate 的元素的下标,不存在就返回Optional.empty();

  • Optional positionIf(List list, Predicate predicate, int start):

    同上,从下标为start的位置开始(包含start);

  • Optional positionIf(List list, Predicate predicate, int start, int end):

    同上,从下标为start的位置开始(包含start),到下标为end的位置结束(不包含end);

完整实现

public CollectionUtil{
    public static <T> Optional<T> find(List<T> list, T item) {
        return find(list, item, 0);
    }

    public static <T> Optional<T> find(List<T> list, T item, int start) {
        return find(list, item, start, list.size());
    }

    public static <T> Optional<T> find(List<T> list, T item, int start, int end) {
        return findIf(list, t -> Objects.equals(t, item), start, end);
    }

    public static <T> Optional<T> findIf(List<T> list, Predicate<? super T> predicate) {
        return findIf(list, predicate, 0);
    }

    public static <T> Optional<T> findIf(List<T> list, Predicate<? super T> predicate, int start) {
        return findIf(list, predicate, start, list.size());
    }

    public static <T> Optional<T> findIf(List<T> list, Predicate<? super T> predicate, int start, int end) {
        Iterator<T> iterator = list.iterator();
        int index = 0;
        while (iterator.hasNext()) {
            T item = iterator.next();
            if (index >= start) {
                if (predicate.test(item)) {
                    return Optional.of(item);
                }
            }

            index++;
            if (index >= end) {
                return Optional.empty();
            }
        }
        return Optional.empty();
    }

    public static <T> Optional<Integer> position(List<T> list, T item) {
        return position(list, item, 0);
    }

    public static <T> Optional<Integer> position(List<T> list, T item, int start) {
        return position(list, item, start, list.size());
    }

    public static <T> Optional<Integer> position(List<T> list, T item, int start, int end) {
        return positionIf(list, t -> Objects.equals(t, item), start, end);
    }

    public static <T> Optional<Integer> positionIf(List<T> list, Predicate<? super T> predicate) {
        return positionIf(list, predicate, 0);
    }

    public static <T> Optional<Integer> positionIf(List<T> list, Predicate<? super T> predicate, int start) {
        return positionIf(list, predicate, start, list.size());
    }

    public static <T> Optional<Integer> positionIf(List<T> list, Predicate<? super T> predicate, int start, int end) {
        Iterator<T> iterator = list.iterator();
        int index = 0;
        while (iterator.hasNext()) {
            T item = iterator.next();
            if (index >= start) {
                if (predicate.test(item)) {
                    return Optional.of(index);
                }
            }

            index++;
            if (index >= end) {
                return Optional.empty();
            }
        }
        return Optional.empty();
    }
}

你可能感兴趣的:(Java,java,函数式编程,集合操作,集合查找,列表查找)