What's the purpose of partitioningBy

前端 未结 5 594
粉色の甜心
粉色の甜心 2020-12-03 04:20

For example, if I intend to partition some elements, I could do something like:

Stream.of(\"I\", \"Love\", \"Stack Overflow\")
      .collect(Collectors.part         


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 05:17

    As denoted by the other answers, segregating a collection into two groups is useful in some scenarios. As these two partitions would always exist, it would be easier to utilize it further. In JDK, to segregate all the class files and config files, partitioningBy is used.

        private static final String SERVICES_PREFIX = "META-INF/services/";
        
        // scan the names of the entries in the JAR file
        Map> map = jf.versionedStream()
                .filter(e -> !e.isDirectory())
                .map(JarEntry::getName)
                .filter(e -> (e.endsWith(".class") ^ e.startsWith(SERVICES_PREFIX)))
                .collect(Collectors.partitioningBy(e -> e.startsWith(SERVICES_PREFIX),
                                                   Collectors.toSet()));
    
        Set classFiles = map.get(Boolean.FALSE);
        Set configFiles = map.get(Boolean.TRUE);
    

    Code snippet is from jdk.internal.module.ModulePath#deriveModuleDescriptor

提交回复
热议问题