Java 8 Lambda function that throws exception?

后端 未结 26 2032
臣服心动
臣服心动 2020-11-22 03:14

I know how to create a reference to a method that has a String parameter and returns an int, it\'s:

Function         


        
26条回答
  •  独厮守ぢ
    2020-11-22 03:32

    public void frankTest() {
        int pageId= -1;
    
        List users= null;
        try {
            //Does Not Compile:  Object page=DatabaseConnection.getSpringConnection().queryForObject("SELECT * FROM bookmark_page", (rw, n) -> new Portal(rw.getInt("id"), "", users.parallelStream().filter(uu -> uu.getVbid() == rw.getString("user_id")).findFirst().get(), rw.getString("name")));
    
            //Compiles:
            Object page= DatabaseConnection.getSpringConnection().queryForObject("SELECT * FROM bookmark_page", (rw, n) -> { 
                try {
                    final Book bk= users.stream().filter(bp -> { 
                        String name= null;
                        try {
                            name = rw.getString("name");
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        return bp.getTitle().equals(name); 
                    }).limit(1).collect(Collectors.toList()).get(0);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return new Portal(rw.getInt("id"), "", users.get(0), rw.getString("name")); 
            } );
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    

提交回复
热议问题