📜  实现 SimpleBindings API 的Java程序

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

实现 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 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 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 anotherMap = new HashMap();
       
        anotherMap.put("4", "Geeta");
        anotherMap.put("5", "Tina");
        map.putAll(anotherMap);
 
        System.out.println("The key set of the map is : ");
       
        Set keySet = map.keySet();
        Iterator itr = keySet.iterator();
       
        while (itr.hasNext()) {
            System.out.print(itr.next() + "\n");
        }
       
        System.out.println();
 
        System.out.println("The values of map is :  ");
       
        Collection collectionOfValues = map.values();
        Iterator 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 > itrOfEntrySet;
       
        Set > 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();
    }
}

输出
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