我是java 8的新手。
我想使用java8并希望将下面的内容转换为java8。
<?php
// require ReCaptcha class
require('autoload.php');
// configure
$from = 'tgdtom@gmail.com';
$sendTo = 'tgdtom@gmail.com';
$subject = 'Bericht via Cates contactformulier';
$fields = array('name' => 'Naam', 'email' => 'Email', 'phone' => 'Tel.nr.', 'message' => 'Bericht'); // array variable name => Text to appear in the email
$okMessage = 'Uw bericht is met succes verzonden, u krijgt zo snel mogelijk reactie!';
$errorMessage = 'Er is iets fout gegaan met het versturen van uw bericht, probeer het later nog eens of neem telefonisch contact op.';
$recaptchaSecret = '6Lf9ZzUUAAAAAL7qstYlXD8pE8LCvpsDWWlvsW5-';
// let's do the sending
try
{
if (!empty($_POST)) {
// validate the ReCaptcha, if something is wrong, we throw an Exception,
// i.e. code stops executing and goes to catch() block
if (!isset($_POST['g-recaptcha-response'])) {
throw new \Exception('ReCaptcha is not set.');
}
// do not forget to enter your secret key in the config above
// from https://www.google.com/recaptcha/admin
$recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost());
// we validate the ReCaptcha field together with the user's IP address
$response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$response->isSuccess()) {
throw new \Exception('ReCaptcha was not validated.');
}
// everything went well, we can compose the message, as usually
$emailText = "You have new message from contact form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
模型类::
<?php
/* An autoloader for ReCaptcha\Foo classes. This should be required()
* by the user before attempting to instantiate any of the ReCaptcha
* classes.
*/
spl_autoload_register(function ($class) {
if (substr($class, 0, 10) !== 'ReCaptcha\\') {
/* If the class does not lie under the "ReCaptcha" namespace,
* then we can exit immediately.
*/
return;
}
/* All of the classes have names like "ReCaptcha\Foo", so we need
* to replace the backslashes with frontslashes if we want the
* name to map directly to a location in the filesystem.
*/
$class = str_replace('\\', '/', $class);
/* First, check under the current directory. It is important that
* we look here first, so that we don't waste time searching for
* test classes in the common case.
*/
$path = dirname(__FILE__).'/'.$class.'.php';
if (is_readable($path)) {
require_once $path;
}
/* If we didn't find what we're looking for already, maybe it's
* a test class?
*/
$path = dirname(__FILE__).'/../tests/'.$class.'.php';
if (is_readable($path)) {
require_once $path;
}
});
我的问题我如何在上面的列表中应用java 8功能,我想迭代并调用另一个函数。
答案 0 :(得分:4)
你可以使用forEach
:
listModel.forEach(model -> {
try {
new UpDateData().bankData(model.getCust_id(), model.getBank_id(), model.getDate());
} catch(){
.... handle
}
})
答案 1 :(得分:1)
forEach在java 8中用于迭代。您可以使用Iterable.forEach()方法遍历所有元素。
List<String> words = new ArrayList<>(Arrays.asList("the", "this",
"that", "there"));
alphabets.forEach(s -> { System.out.println(s) }); //using lamda
列表&lt;&gt;是迭代类型,因此您可以直接在列表上使用forEach。与旧的外部迭代相比,这称为内部迭代。这由java自动转换为外部迭代。如果只有一个语句是强制性的,则lambda括号在lambda中是可选的。 与循环中的旧if块相比,您可以在迭代器上使用过滤器。
alphabets.stream()
.filter(s -> s.startsWith("the"))
.forEach(System.out::println);
alphabets.stream()
.filter(s -> s.length() > 3)
.forEach(System.out::println);
以上语法使用方法参考来打印元素。
https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html