0 Replies - 335 Views - Last Post: 03 November 2010 - 07:54 AM

#1 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12742
  • View blog
  • Posts: 45,924
  • Joined: 27-December 08

StackMap

Posted 03 November 2010 - 07:54 AM

Description: This snippet combines Stack and HashTable functionality. Elements are still ordered last-in-first-out and without random-access of any element (like in a Stack), but allows for association of Values with Keys for HashTable functionality.
import java.util.*;

/**
 * @author Macosxnerd101
 * @version 1.0
 * @date 11/02/2010
 *
 * This class is a combination of a Stack
 * and a HashTable, allowing for LIFO ordering
 * of elements. Like a Stack, elements are not required
 * to be unique. Unlike a HashTable, this applies to both the
 * Key and Value elements. The benefit of using a StackMap
 * is that it allows for associating a Value with the pushed
 * Key, which is helpful in instances where place-holding is
 * a must.
 *
 * @param K: Key
 * @param V: Value
 * */
public class StackMap<K,V>{

    //I use an ArrayList rather than the java.util.Stack
    //because the Stack class extends java.util.Vector,
    //which is synchronized. As ArrayList is not synchronized
    //it will be more efficient as serving as the Stack
    private ArrayList<EntryClass<K,V>> stack;

    public StackMap(){
        stack = new ArrayList<EntryClass<K,V>>();
    }

    //The EntryClass class which couples the Key-Value pairs
    //It implements the Map.Entry<K,V> interface, which basically
    //defines getter/setter methods for the Key and Value.
    //No additional functionality is provided beyond the getter/setter
    //methods except for an equals() method.
    public class EntryClass<K,V> implements Map.Entry<K,V>{
        private K key;
        private V value;

        public EntryClass(K key, V value){
            this.key = key;
            this.value = value;
        }
        public K getKey() {return key;}

        public V getValue() {return value;}

        public V setValue(V value) {
            this.value = value;
            return this.value;
        }

        public boolean equals(EntryClass<K,V> c){
            return key.equals(c.key) && value.equals(c.value);
        }
    }

    //The number of entries in the Stack
    public int size() {return stack.size();}

    //Whether or not there are any entries in the Stack
    public boolean isEmpty() {return stack.size() == 0;}

    //Removes all elements from the Stack
    public void clear() {stack.clear();}

    /**
     * @param key: The key or identifying element in the Map
     * @param value: The associated information with the key
     * This function pushes onto the Stack a new instance of the EntryClass
     * relating the Key and Value parameters. This new element is the top
     * element on the Stack, and will be returned by peek() and pop() calls.
     * */
	public void push(K key, V value){stack.add(new EntryClass<K,V>(key,value));}

    /**
     * @return EntryClass<K,V>: The top instance of EntryClass on the Stack
     * This method does not remove the top element from the Stack
     * */
    public EntryClass<K,V> peek(){return stack.get(stack.size()-1);}


    /**
     * @return EntryClass<K,V>: The top instance of EntryClass on the Stack
     * This method removes the top element from the Stack
     * */
    public EntryClass<K,V> pop(){return stack.remove(stack.size()-1);}


    /**
     * @param key: The key to search for in the Stack.
     * @return: true if the key is found, false otherwise
     * */
    public boolean containsKey(Object key){
        for(EntryClass<K,V> x:stack)
            if(x.getKey().equals(key))
                return true;
        return false;
    }


    /**
     * @param key: The key in the relationship
     * @paraam value: The value associated with the given key
     * @return: true if the relationship is found in the Stack, false otherwise
     * */
    public boolean containsPairing(K key, V value){
        EntryClass<K,V> temp = new EntryClass<K,V>(key,value);
        for(EntryClass<K,V> x:stack)
            if(x.equals(temp))
                return true;
        return false;
    }

    /**
     * @param value: The value to search for in the Stack
     * @return: true if the value is found in the Stack, false otherwise
     * */
    public boolean containsValue(Object value){
        for(EntryClass<K,V> x:stack)
            if(x.getValue().equals(value))
                return true;
        return false;
    }

}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1