Java Program to Implement SimpleBindings API

SimpleBindings is an implementation of bindings that are supported by HashMap or any other map which is specified by the programmer.
Constructors of SimpleBindings API:
- SimpleBindings(): It is a default constructor that uses a HashMap to store the values.
- SimpleBindings(Map<String, object> m): Overloaded constructor uses an existing HashMap to store the values.
Syntax:
public class SimpleBindings extends Object implements Bindings
Implementation of 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<String, Object> 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<Map.Entry<String, Object> > 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<String> 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<Object> values()    {        return simpleBindings.values();    }}class SimpleBindingsImpl {    public static void main(String[] args)    {        SimpleBindingsAPIExample map = new SimpleBindingsAPIExample();               map.put("1", "Ram");        map.put("2", "Shyam");        map.put("3", "Sita");Â
        Map<String, Object> anotherMap = new HashMap<String, Object>();               anotherMap.put("4", "Geeta");        anotherMap.put("5", "Tina");        map.putAll(anotherMap);Â
        System.out.println("The key set of the map is : ");               Set<String> keySet = map.keySet();        Iterator<String> itr = keySet.iterator();               while (itr.hasNext()) {            System.out.print(itr.next() + "\n");        }               System.out.println();Â
        System.out.println("The values of map is : ");               Collection<Object> collectionOfValues = map.values();        Iterator<Object> itrOfValues = collectionOfValues.iterator();               while (itrOfValues.hasNext())        {            System.out.print(itrOfValues.next() + "\n");        }        System.out.println();Â
        System.out.println("The entry set of the map is ");        Iterator<Entry<String, Object> > itrOfEntrySet;               Set<Entry<String, Object> > entrySet = map.entrySet();        itrOfEntrySet = entrySet.iterator();               while (itrOfEntrySet.hasNext())        {            System.out.println(itrOfEntrySet.next());        }        System.out.println();Â
        // Returns true if map contains the key 2        boolean check = map.containsKey("2");        System.out.println("The map contains key 2 ? "                           + check);        System.out.println();Â
        // Return true if map contains the value snigdha        check = map.containsValue("Tina");        System.out.println("The map contains value Tina? "                           + check);        System.out.println();Â
        // Return the size of map        int result = map.size();        System.out.println("The number of key-value pairs in the map are : "            + result);        System.out.println();Â
        // Clear the map means delete all the key-value        // pairs from the map        map.clear();Â
        // Return true if map is empty        check = map.isEmpty();        System.out.println(            "After clear the map, the map is empty ? "            + check);        System.out.println();    }} |
Output
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
Â



