好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

详解Java8的forEach(...)如何提供index值

Java2遍历集合

遍历Collection的代码,可以是采用Iterator接口,通过next()遍历。如:

?

1

2

3

4

5

6

List<String> list = Arrays.asList( "Hi" , "I" , "am" , "Henry.Yao" );

// 此处已经用到了泛型,不能算是纯粹的Java2代码,仅作Iterator示范

for (Iterator<String> it = list.iterator(); it.hasNext();) {

   String item = it.next();

   System.out.println( "listItem = " + item);

}

输出:

listItem = Hi
listItem = I
listItem = am
listItem = Henry.Yao

Java5遍历集合

在Java5中,提供了增强的for循环,如:

?

1

2

3

4

List<String> list = Arrays.asList( "Hi" , "I" , "am" , "Henry.Yao" );

for (String item : list) {

   System.out.println( "listItem = " + item);

}

Java8遍历集合

在Java8中,通过Lambda表达式提供了更简洁的编程方式,如:

?

1

2

3

list.forEach(item -> {

   System.out.println( "listItem = " + item);

});

需同时提供index,咋办?

操作集合元素item的同时,如果还需要同时提供index值,咋办?
思考后,我们可能大都写出了如下的代码,同时心有不甘:

?

1

2

3

4

5

List<String> list = Arrays.asList( "Hi" , "I" , "am" , "Henry.Yao" );

for ( int index; index<list.size(); index++) {

   String item = list.get(i);

   System.out.println( "list[" +index+ "] = " +item);

}

输出:

list[0] = Hi,
list[1] = I
list[2] = am
list[3] = Henry.Yao

期望的遍历模式

因为,如下的模式才是我们期望的模式

?

1

2

3

list.forEach((item, index) -> {

   System.out.println( "listItem = " + item);

}); // Compile ERROR

这只是期望。实际上,Jdk8并没有提供该函数,直至Jdk11也均没有提供该函数。

通过BiConsumer包装Consumer实现

[没有工具,我们制造工具] 定义如下的工具方法,基于这个工具方法,我们就能在遍历集合,同时提供item和index值:

?

1

2

3

4

5

6

7

8

9

10

11

// 工具方法

public static <T> Consumer<T> consumerWithIndex(BiConsumer<T, Integer> consumer) {

   class Obj {

     int i;

   }

   Obj obj = new Obj();

   return t -> {

     int index = obj.i++;

     consumer.accept(t, index);

   };

}

这样的业务代码,是我期望的!

基于该工具方法,便可轻松编写如下业务代码,清晰、简洁:

?

1

2

3

list.forEach(LambdaUtils.consumerWithIndex((item, index) -> {

   System.out.println( "list[" + index + "]=" + item);

}));

思考过程

这个工具方法的设计过程,也是参考借鉴了distinctByKey,如图:

?

1

2

3

4

5

// 工具方法

public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {

Map<Object, Boolean> seen = new ConcurrentHashMap<>();

return t -> Objects.isNull(seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE));

}

?

1

2

3

// 业务代码

// 从人员列表中过滤出一个子集(每个部门选一个人)

employees.stream().filter(distinctByKey(Employee::getDeptCode)).collect(toList());

我们不仅要会使用工具,更要会制造工具…

我们的程序,不仅仅只是大片的业务代码,更是需要抽象和提取出的诸多工具方法。
使用工具(使用Java和第三方提供的方法)到极致,那是高级程序员,
制造工具(分析和设计出项目的工具方法)到极致,那是高级设计师。

到此这篇关于详解Java8的forEach(...)如何提供index值的文章就介绍到这了,更多相关Java8 forEach提供index值内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/weixin_37297850/article/details/102856739

查看更多关于详解Java8的forEach(...)如何提供index值的详细内容...

  阅读:104次