实现 SimpleBindings API 的Java程序
SimpleBindings 是 HashMap 或程序员指定的任何其他映射支持的绑定的实现。
SimpleBindings API 的构造函数:
- SimpleBindings():它是一个默认构造函数,它使用 HashMap 来存储值。
- SimpleBindings(Map
m):重载构造函数使用现有的 HashMap 来存储值。
句法:
public class SimpleBindings extends Object implements Bindings
SimpleBindings API 的实现:
Java
// Java Program to Implement SimpleBindings API
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.script.SimpleBindings;
public class SimpleBindingsAPIExample {
// reference of SimpleBindings class
private SimpleBindings simpleBindings;
// Default constructor will use a HashMap
public SimpleBindingsAPIExample()
{
// object creating of SimpleBindings class
simpleBindings = new SimpleBindings();
}
// Overloaded constructor uses an existing HashMap to
// store the values
public SimpleBindingsAPIExample(Map map)
{
simpleBindings = new SimpleBindings(map);
}
// Clear all the values from the map
public void clear() { simpleBindings.clear(); }
// Returns true if the map contains value for the
// specified key
public boolean containsKey(Object key)
{
return simpleBindings.containsKey(key);
}
// Return true if the map contains values as specified
public boolean containsValue(Object value)
{
return simpleBindings.containsValue(value);
}
// Returns the set of values contained in the map
public Set > entrySet()
{
return simpleBindings.entrySet();
}
// Returns the values if specified key is exist in the
// map else will return null
public Object get(Object key)
{
return simpleBindings.get(key);
}
// Returns whether the map is empty or not
public boolean isEmpty()
{
return simpleBindings.isEmpty();
}
// Returns a set of the keys
public Set keySet()
{
return simpleBindings.keySet();
}
// Insert the specified value associated with the
// specified key
public Object put(String key, Object value)
{
return simpleBindings.put(key, value);
}
// Copy all the values from another map into this map
public void putAll(Map extends String, ? extends Object> map)
{
simpleBindings.putAll(map);
}
// Removes the value associated with the specified key
public Object remove(Object key)
{
return simpleBindings.remove(key);
}
// Return the number of key-value pairs exist in the map
public int size() { return simpleBindings.size(); }
// Returns a collection of the values contained in map
public Collection
输出
The key set of the map is :
1
2
3
4
5
The values of map is :
Ram
Shyam
Sita
Geeta
Tina
The entry set of the map is
1=Ram
2=Shyam
3=Sita
4=Geeta
5=Tina
The map contains key 2 ? true
The map contains value Tina? true
The number of key-value pairs in the map are : 5
After clear the map, the map is empty ? true