SimpleBindings类的put()方法用于在SimpleBindings对象中添加指定的键和值对。键和值都作为参数传递。将键重命名为要在SimpleBindings对象中设置的值的名称。设置value方法后,将返回指定键的先前值,如果先前未设置键,则它将返回null。
Synatx:
public Object put(String name, Object value)
参数:此方法接受两个参数:
- name这是值的名称和
- value这是要设置的值。
返回值:此方法返回指定键的先前值,如果先前未设置键,则此方法将返回null。
异常:此方法引发以下异常:
- NullPointerException :如果指定的名称为null
- IllegalArgumentException:如果指定的名称为空String
Java程序来说明put()方法的用法:
程序1:
// Java program to demosntrate 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"));
}
}
输出:
Key1:value1 Key2:value2 Key3:value3
程序2:
// Java program to demosntrate 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
相关用法
- Java SimpleBindings get()用法及代码示例
- Java SimpleBindings remove()用法及代码示例
- Java SimpleBindings containsKey()用法及代码示例
- Java SimpleBindings putAll()用法及代码示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代码示例
- Java Java lang.Long.builtcount()用法及代码示例
- Java Java lang.Long.reverse()用法及代码示例
- Java Java.util.Collections.disjoint()用法及代码示例
- Java Java.util.Collections.rotate()用法及代码示例
- Java Java lang.Long.byteValue()用法及代码示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代码示例
- Java Java lang.Long.lowestOneBit()用法及代码示例
- Java Java lang.Long.highestOneBit()用法及代码示例
- Java Clock tickMinutes()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 SimpleBindings put() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。