如何将 2 个映射合并为一个具有集合值的映射
<?php
include ('navbar.php');
?>
<form class = "container m-5" method = "POST" action = "upload.php" encytype = "multipart/form-data">
<input class = "form-control w-25" type = "file" name = "file">
<button class = "btn" type = "submit" name = "Upload">Upload</button>
</form>
<?php
include'connect.php';
$statusMsg = '';
// File upload path
$targetDir = "image";
$fileName = $_FILES["file"]["name"];
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
// Allow certain file formats
$allowTypes = array('jpg','png','jpeg','pdf');
if(in_array($fileType, $allowTypes)){
// Upload file to server
if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
// Insert image file name into database
$insert = $db->query("INSERT into userid (FileName) VALUES ('".$fileName."'");
if($insert){
$statusMsg = "The file ".$fileName. " has been uploaded successfully.";
}else{
$statusMsg = "File upload failed, please try again.";
}
}else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}else{
$statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.';
}
}else{
$statusMsg = 'Please select a file to upload.';
}
// Display status message
echo $statusMsg;
?>
这种方式将覆盖该值
答案 0 :(得分:2)
您可以像这样使用 merge()
方法。
Map<String, Collection<Integer>> map1 = Map.of(
"a", List.of(0, 1),
"b", List.of(10));
Map<String, Collection<Integer>> map2 = Map.of(
"a", List.of(2, 3),
"c", List.of(20));
Map<String, Collection<Integer>> map3 = new HashMap<>(map1);
for (Entry<String, Collection<Integer>> e : map2.entrySet())
map3.merge(e.getKey(), e.getValue(), (v0, v1) -> {
Collection<Integer> list = new ArrayList<>(v0);
list.addAll(v1);
return list;
});
System.out.println(map3);
输出:
{a=[a0, a1, a2, a3], b=[b0], c=[c1]}
答案 1 :(得分:0)
return Stream.concat(map1.entrySet().stream(), map2.entrySet().stream())
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(ids1, ids2) -> {
final Set<Integer> merged = new HashSet<>(ids1);
merged.addAll(ids2);
return merged;
}));
}