用于Map的Java 8 toMap <string,collection <string =“”>&gt;

时间:2016-07-28 17:46:42

标签: java collections filter java-stream

我是Java 8的新手,我无法解决如何将这些数据导入地图的问题 -

我正在从配置文件中读取数据 - 它看起来像这样(示例) -

SomeName = {
   someAttribute = "attributeVal",
   someImportantAttributeList = ("val1", "val2", "val3"),
   ...
}

SomeName2 = {
   someAttribute ....
} 

... 

我正在尝试将数据收集到地图中,因此地图是一个Map,其中Key是SomeName,而Value是一个字符串集合(“someImportantAttributeList”。)

所有其他数据无关紧要。

现在我有这样的事情:

Map<String, Collection<String>> mapping = configReader.getConfig()
    .entrySet()
    .stream()
    .filter(entry -> entry.getValue().containsKey("someImportantAttributeList")
    . ???? 

我无法弄清楚如何正确地将其映射到String,Collection地图?

流本身似乎是条目,其值为Key = SomeName,value = String / Object类型的内部映射。 (所以在这种情况下,条目的键是SomeName,该值将是String key(someAttribute)和Object value(该属性的值)的另一个映射。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

<?php

    mysqli_connect("localhost", "root", "root") or die(mysqli_error());
    mysqli_select_db("registrations") or die(mysqli_error());


    if(isset($_POST['name']) && !empty($_POST['name']) AND isset($_POST['email']) && !empty($_POST['email'])){
        $name = mysqli_escape_string($_POST['name']); 
        $email = mysqli_escape_string($_POST['email']); 
    }

    if(!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
        $msg = 'You have entered a invalid email.';
    }else{
        $msg = 'Your account has been made, please click the link in the email getting sent to you for verifying your account.';
        $hash = md5( rand(0 , 1000) );
        $password = rand(1000,5000);
    }

    mysqli_query("INSERT INTO users (username, password, email, hash) VALUES(
    '". mysqli_escape_string($name) ."', 
    '". mysqli_escape_string(md5($password)) ."', 
    '". mysqli_escape_string($email) ."', 
    '". mysqli_escape_string($hash) ."') ") or die(mysqli_error());
?>