📜  Java中的 SimpleBindings put() 方法及示例

📅  最后修改于: 2022-05-13 01:55:16.259000             🧑  作者: Mango

Java中的 SimpleBindings put() 方法及示例

SimpleBindings 类put()方法用于在 SimpleBindings 对象中添加指定的键值对。键和值都作为参数传递。将键重命名为要在 SimpleBindings 对象中设置的值的名称。设置 value 方法后,返回指定键的先前值,如果先前未设置键,它将返回 null。
句法:

public Object put(String name, Object value)

参数:此方法接受两个参数:

  • name是值的名称和
  • value这是要设置的值。

返回值:此方法返回指定键的先前值,如果键先前未设置,它将返回null
异常:此方法抛出以下异常:

  • NullPointerException : 如果指定的名称为 null
  • IllegalArgumentException : 如果指定的名称为空 String

说明 put() 方法工作的Java程序:
方案一:

Java
// Java program to demonstrate get() method
 
import javax.script.SimpleBindings;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Create simpleBindings object
        SimpleBindings bindings
            = new SimpleBindings();
 
        // Add key value pair using put()
        bindings.put("key1", "value1");
        bindings.put("key2", "value2");
        bindings.put("key3", "value3");
 
        // Print them
        System.out.println("Key1: "
                           + bindings.get("key1"));
        System.out.println("Key2: "
                           + bindings.get("key2"));
        System.out.println("Key3: "
                           + bindings.get("key3"));
    }
}


Java
// Java program to demonstrate get() method
 
import javax.script.SimpleBindings;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Create simpleBindings object
        SimpleBindings asiaTeamList
            = new SimpleBindings();
 
        // Add team in asiaTeamList using put()
        asiaTeamList.put("team1", "India");
        asiaTeamList.put("team2", "Sri Lanka");
        asiaTeamList.put("team3", "Pakistan");
        asiaTeamList.put("team4", "Bangladesh");
 
        // Print
        System.out.println(
            asiaTeamList.get("team1"));
        System.out.println(
            asiaTeamList.get("team2"));
        System.out.println(
            asiaTeamList.get("team3"));
        System.out.println(
            asiaTeamList.get("team4"));
    }
}


输出:
Key1: value1
Key2: value2
Key3: value3

方案二:

Java

// Java program to demonstrate get() method
 
import javax.script.SimpleBindings;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Create simpleBindings object
        SimpleBindings asiaTeamList
            = new SimpleBindings();
 
        // Add team in asiaTeamList using put()
        asiaTeamList.put("team1", "India");
        asiaTeamList.put("team2", "Sri Lanka");
        asiaTeamList.put("team3", "Pakistan");
        asiaTeamList.put("team4", "Bangladesh");
 
        // Print
        System.out.println(
            asiaTeamList.get("team1"));
        System.out.println(
            asiaTeamList.get("team2"));
        System.out.println(
            asiaTeamList.get("team3"));
        System.out.println(
            asiaTeamList.get("team4"));
    }
}
输出:
India
Sri Lanka
Pakistan
Bangladesh

参考资料: https://docs.oracle.com/javase/10/docs/api/javax/script/SimpleBindings.html#put(Java.lang.String, Java.lang.Object)