标签: java-8

从Java 8 forEach循环返回一个值

在下面的示例中,someObjects是一个集合.如果条件在循环内匹配,我试图返回true,但是这似乎没有编译.但是,当我只是添加"返回"它工作正常.我需要解决的问题是什么?

public boolean find(){

    someObjects.forEach(obj -> {
       if (some_condition_met) {
          return true;
       }
    });

    return false;
}
Run Code Online (Sandbox Code Playgroud)

编译错误

Iterable类型中的forEach(Consumer)方法不适用于参数((obj) - > {})

java java-8

0
推荐指数
2
解决办法
2万
查看次数

从java列表中提取列表值

public class FetchVarableList {

public static void main(String[] args) {

    List<List<Employee>> empsList = new ArrayList<>();

    Employee e1 = new Employee(1, "Abi", "Fin", 2000);
    Employee e2 = new Employee(2, "Chandu", "OPs", 5000);
    Employee e3 = new Employee(3, "mahesh", "HR", 8000);
    Employee e4 = new Employee(4, "Suresh", "Main", 1000);


    List<Employee> empList = new ArrayList<>();
    empList.add(e1); empList.add(e2); 


    List<Employee> empList2 = new ArrayList<>();

    empList2.add(e3); empList2.add(e4);


    empsList.add(empList);

    empsList.add(empList2);
}
Run Code Online (Sandbox Code Playgroud)

}

上面这段代码是empList2中的emp1,e3,e4中的员工e1,e2列表,这两个员工列表添加到empslist,我想在Integer的单个列表中获取所有员工号码

如何从java8中的一个列表中获取所有员工编号列表

java-8 java-stream

0
推荐指数
1
解决办法
1218
查看次数

JVM Stream API RollBack

当我们向流API提供一些输入时,它将其分成块,JVM创建多个线程以在该块上执行执行.

题 :-

如果我给出了数百万个条目ArrayList作为并行管道的输入,并且在List by JVM上的计算的前半部分之后发生内部线程异常.

JVM如何处理ROll Back.

JVM真的回滚到了原始状态吗?

java java-8

0
推荐指数
1
解决办法
113
查看次数

从java流创建java.util.Enumeration的最佳方法是什么?

我目前最好的解决方案是:

Collections.enumeration(stream.collect(Collectors.toList()))
Run Code Online (Sandbox Code Playgroud)

希望有一个比这更简洁的方法.

java java-8 java-stream

0
推荐指数
1
解决办法
121
查看次数

java8 - 使用Function接口替换Consumer或Supplier

由于Consumer/Supplier/Predicate/UnaryOperator只是Function的一个特例,我如何用Function替换这些interfacces?

T - >功能 - > R.

T - > Consumer - > null

null - >供应商 - > T.

T - >谓词 - >布尔值

T - > UnaryOperator - > T.

null和boolean只是T的一个特例.所以我用函数来编写两个案例来替换Predicate和UnaryOperator.

例如:

private static void replacePredicate() {
    Function<String, Boolean> func = x -> x.startsWith("a");
    Predicate<String> pre = x -> x.startsWith("a");

    System.out.println(func.apply("ape"));
    System.out.println(pre.test("ape"));
}

private static void replaceUnaryOperator() {
    Function<Integer, Integer> func = x -> x * 2;
    UnaryOperator<Integer> uo = x -> x * 2;

    System.out.println(func.apply(6));
    System.out.println(uo.apply(6));
} …
Run Code Online (Sandbox Code Playgroud)

lambda java-8

0
推荐指数
1
解决办法
646
查看次数

Eclipse警告"在Java项目中使用菱形运算符('<>')替换此构造函数调用中的类型规范

当它要求时,Eclipse意味着什么Replace the type specification in this constructor call with the diamond operator ('<>'),因为所述运算符已经存在?

然后我将光标放在带蓝色下划线的内容上,然后按下F2以了解更多内容,我收到此消息(Replace the type specification in this constructor call with the diamond operator ('<>')):

Eclipse警告说')"">

如果我Object从内部删除<>,Eclipse仍然不高兴:

Eclipse警告说')"">

编辑

我们确保了

  • java.util.List 是进口的
  • 更改已保存
  • Project -> Clean... 命令已应用

蓝色警告仍然不会消失.

java eclipse java-8 sonarlint sonarlint-eclipse

0
推荐指数
1
解决办法
5508
查看次数

如何杀死CompletableFuture相关的主题?

我有检查CompletableFuture执行时间的方法.如果此类CompletableFuture执行的时间超过2秒,我想要终止此任务.但是,如果我没有执行CompletableFuture方法的控制线程,我该怎么办呢?

       final CompletableFuture<List<List<Student>>> responseFuture = new CompletableFuture<>();
responseFuture.supplyAsync(this::createAllRandomGroups)
        .thenAccept(this::printGroups)
        .exceptionally(throwable -> {
            throwable.printStackTrace();
            return null;
        });
Run Code Online (Sandbox Code Playgroud)

createAllRandomGroups()

private List<List<Student>> createAllRandomGroups() {
    System.out.println("XD");
    List<Student> allStudents = ClassGroupUtils.getActiveUsers();
    Controller controller = Controller.getInstance();
    List<List<Student>> groups = new ArrayList<>();
    int groupSize = Integer.valueOf(controller.getGroupSizeComboBox().getSelectionModel().getSelectedItem());
    int numberOfGroupsToGenerate = allStudents.size() / groupSize;
    int studentWithoutGroup = allStudents.size() % groupSize;
    if (studentWithoutGroup != 0) groups.add(this.getListOfStudentsWithoutGroup(allStudents, groupSize));
    for(int i = 0; i < numberOfGroupsToGenerate; i++) {
        boolean isGroupCreated = false;
        while (!isGroupCreated){
            Collections.shuffle(allStudents);
            List<Student> newGroup = this.createNewRandomGroupOfStudents(allStudents, groupSize);
            groups.add(newGroup);
            if …
Run Code Online (Sandbox Code Playgroud)

java multithreading future java-8 completable-future

0
推荐指数
1
解决办法
2076
查看次数

通过某个键将<Long,List <String >>映射到String Lambda的列表

HashMap喜欢Map<Long, List<String>> map = ....

我也有一个关键列表 List<Long> keyList = Arrays.asList(1L, 3L, 10L);

我想List<String>通过这些提取keyList'sList<String>使用lambda表达式生成一个 .

java lambda java-8

0
推荐指数
1
解决办法
125
查看次数

Java Stream出现次数

我有以下对象的列表.使用StreamAPI,我可以得到User它出现的频率数List吗?

public class User {
    string name;
    int age;
}
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

0
推荐指数
1
解决办法
83
查看次数

Java 8中Lambda嵌套集的错误

我试图使用lambda表达式实现相同但得到错误

for (String id:ids) {
    for (MainGrid ms:grids) {
        for (GridRep innerGrid: ms.getInnerGrid()){
            if(innerGrid.getId().equals(id)){
                finalGrid.add(innerGrid);
                break;
            }
        }
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码在lambda中

for (String id:ids) {
    finalGrid.add(grids.stream().flatMap(ms -> ms.getInnerGrid().stream())
                   .filter(s -> s.getId().equals(id)).findFirst().get());
}
Run Code Online (Sandbox Code Playgroud)

错误:使用传统for()时同样有效,有人可以指导我如何使用lambda实现相同的功能,或者我的代码中是否有一些错过

201-Jan-2018 11:38:29.221 SEVERE [http-apr-9910-exec-8] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service()for servlet [Spring MVC Dispatcher Servlet]与路径的上下文[/ lineManagement]抛出异常[请求处理失败; 嵌套异常是java.util.NoSuchElementException:没有值]带有根本原因java.util.NoSuchElementException:java.util.Optional.get中没有值(Optional.java:135)

java java-8 java-stream

0
推荐指数
1
解决办法
135
查看次数