ifPresent Stream的其他方法

时间:2015-09-21 21:04:28

标签: java lambda java-8 java-stream optional

我想知道在过滤流后,如果某些值不存在,该怎么做。

我们假设代码:

$webelephants_company_dir = webelephants_make_folder_name($webelephants_company_name);

$webelephants_ftp_setting = get_option( 'webelephants_ftp_setting' );
$ftp_server = $webelephants_ftp_setting['ftp_server'];
$ftp_user_name = $webelephants_ftp_setting['ftp_user_name'];
$ftp_user_pass = $webelephants_ftp_setting['ftp_user_pass'];

$check_dir = false;
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$current_dir = ftp_pwd($conn_id);

if ($current_dir !== false && @ftp_chdir($conn_id, $webelephants_company_dir))
{
    $check_dir = true;
    ftp_chdir($conn_id, $current_dir);   

}else {
    ftp_mkdir($conn_id, $webelephants_company_dir);
    ftp_chmod($conn_id, 0777, $webelephants_company_dir);
}

if($check_dir == false && @ftp_chdir($conn_id, $webelephants_company_dir)){
    $check_dir = true;
}   

ftp_close($conn_id);

if($check_dir == false) {
    $webelephants_form_error = 'Could not create directory.';
}

if($webelephants_form_error == '')
{
    /*setcookie('webelephants_company_dir', $webelephants_company_dir, 0, COOKIEPATH, COOKIE_DOMAIN,false,true);
    if ( SITECOOKIEPATH != COOKIEPATH ){
        setcookie('webelephants_company_dir', $webelephants_company_dir, 0, SITECOOKIEPATH, COOKIE_DOMAIN,false,true);
    }*/

    //$_SESSION['webelephants_company_dir'] = $webelephants_company_dir;

    if($file_count > 0) {

        $conn_id = ftp_connect($ftp_server);
        $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

        if(@ftp_chdir($conn_id, $webelephants_company_dir))

        {
            ftp_pasv($conn_id, true);

            for ($i = 0; $i <= $file_count; $i++) {

                if(isset($_FILES['webelephants_file']['name'][$i])) {
                    $remote_file = $_FILES['webelephants_file']['name'][$i];
                    $local_file = $_FILES['webelephants_file']['tmp_name'][$i];
                    $ret = ftp_nb_put($conn_id, $remote_file, $local_file, FTP_BINARY);

                    while ($ret == FTP_MOREDATA) {
                       set_time_limit(0);

                       //flush();

                       //sleep(1);

                       //usleep(20000);

                       $ret = ftp_nb_continue($conn_id);
                    }

                    //fclose($fo);

                    if ($ret != FTP_FINISHED) {

                       $webelephants_form_error .= $_FILES['webelephants_file']['name'][$i].": There was an error uploading the file...<br>";
                    }
                }
            }

如果价值不存在,foo.stream().filter(p -> p.someField == someValue).findFirst().ifPresent(p -> {p.someField = anotherValue; someBoolean = true;}); 之后我如何设置某种Else

我可以在ifPresent之后调用Stream上的一些orElse方法,但我无法看到使用findFirst

的方法

1 个答案:

答案 0 :(得分:9)

findFirst返回描述此流的第一个元素的Optional,如果流为空则返回空的可选。

如果您想在Optional不为空时应用某项功能,则应使用map。如果orElseGet为空,Optional可以调用另一个lambda,例如

foo.stream()
   .filter(p -> p.someField == someValue)
   .findFirst().map(p -> {
       p.someField = anotherValue;
       someBoolean = true;
       return p;
   }).orElseGet(() -> {
       P p = new P();
       p.someField = evenAnotherValue;
       someBoolean = false;
       return p;
   });