Java中是否有JavaScript Object.assign()类似物?

Java中是否有JavaScript Object.assign()类似物?,java,object,merge,Java,Object,Merge,因此,在JavaScript中,Object.assign()可以“合并”两个(或者我所知道的更多)对象 例如,我们有{a:“b”,c:“d”}和{c:“a”,p:“e”}。合并后,我们将得到一个新对象{a:b,c:a,p:e}。 如果两个对象都有具有相同键的子对象,则函数将合并子对象 在Java中如何才能做到这一点?与JavaScript不同,Java是严格类型化的。您不能创建特殊对象,如问题中的示例 在您希望按照这些思路做一些事情的情况下,您可以使用一个实现(HashMap和类似的实现)映射

因此,在JavaScript中,Object.assign()可以“合并”两个(或者我所知道的更多)对象

例如,我们有
{a:“b”,c:“d”}
{c:“a”,p:“e”}
。合并后,我们将得到一个新对象
{a:b,c:a,p:e}
。 如果两个对象都有具有相同键的子对象,则函数将合并子对象


在Java中如何才能做到这一点?

与JavaScript不同,Java是严格类型化的。您不能创建特殊对象,如问题中的示例

在您希望按照这些思路做一些事情的情况下,您可以使用一个实现(
HashMap
和类似的实现)<代码>映射有几种方法用于执行类似于
对象的操作。分配
,例如

Map target=newhashmap();
target.putAll(source1);
target.putAll(source2);

Map target=newhashmap(source1);
target.putAll(source2);
完整示例:

import java.util.*;

public class Example {
    public static void main(String args[]){
        Map<String, String> source1 = new HashMap<>();
        source1.put("a", "b");
        source1.put("c", "d");
        Map<String, String> source2 = new HashMap<>();
        source2.put("c", "a");
        source2.put("p", "e");

        Map<String, String> target = new HashMap<>();
        target.putAll(source1);
        target.putAll(source2);

        for (var entry : target.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}
import java.util.*;
公开课范例{
公共静态void main(字符串参数[]){
Map source1=新的HashMap();
资料来源1.put(“a”、“b”);
资料来源1.put(“c”、“d”);
Map source2=新的HashMap();
资料来源2.put(“c”、“a”);
资料来源2.put(“p”、“e”);
Map target=new HashMap();
target.putAll(source1);
target.putAll(source2);
for(变量条目:target.entrySet()){
System.out.println(entry.getKey()+“:”+entry.getValue());
}
}
}

与JavaScript不同,Java是严格类型化的。您不能创建特殊对象,如问题中的示例

在您希望按照这些思路做一些事情的情况下,您可以使用一个实现(
HashMap
和类似的实现)<代码>映射
有几种方法用于执行类似于
对象的操作。分配
,例如

Map target=newhashmap();
target.putAll(source1);
target.putAll(source2);

Map target=newhashmap(source1);
target.putAll(source2);
完整示例:

import java.util.*;

public class Example {
    public static void main(String args[]){
        Map<String, String> source1 = new HashMap<>();
        source1.put("a", "b");
        source1.put("c", "d");
        Map<String, String> source2 = new HashMap<>();
        source2.put("c", "a");
        source2.put("p", "e");

        Map<String, String> target = new HashMap<>();
        target.putAll(source1);
        target.putAll(source2);

        for (var entry : target.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}
import java.util.*;
公开课范例{
公共静态void main(字符串参数[]){
Map source1=新的HashMap();
资料来源1.put(“a”、“b”);
资料来源1.put(“c”、“d”);
Map source2=新的HashMap();
资料来源2.put(“c”、“a”);
资料来源2.put(“p”、“e”);
Map target=new HashMap();
target.putAll(source1);
target.putAll(source2);
for(变量条目:target.entrySet()){
System.out.println(entry.getKey()+“:”+entry.getValue());
}
}
}

即使您引用的是JavaScript的
对象.assign
,问题也不是关于JavaScript,因此标记不合适。您没有显示JS代码,Object.assign()有些微妙之处,即使您引用的是JavaScript的
对象.assign
,问题不在于JavaScript,因此标记不合适。您没有显示JS代码,Object.assign()有一些微妙之处
import java.util.*;

public class Example {
    public static void main(String args[]){
        Map<String, String> source1 = new HashMap<>();
        source1.put("a", "b");
        source1.put("c", "d");
        Map<String, String> source2 = new HashMap<>();
        source2.put("c", "a");
        source2.put("p", "e");

        Map<String, String> target = new HashMap<>();
        target.putAll(source1);
        target.putAll(source2);

        for (var entry : target.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}