Translate To Preferred Language

Search Obioku's Thoughts

Please Read Today's Featured Post

Things I Would Like To Happen Soon

For all those who believe in speaking ideas into existence and prayer in the universe will return to a person.  I simply want to write a lis...

Template for Map Algorithm in Java (some bugs may occur)

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package huffman;

import java.util.*;

/**
 *
 * @author obobotette0
 */
public class AbstractMap {

    int amount = 0;
    K key;
    V value;
    ArrayList array = new ArrayList<>();

    //Constructor to assign values
    public AbstractMap(K k, V v) {
        key = k;
        value = v;
    }

    public K getKey(int i) {
        return array.get(i).getKey();
    }

    //return key
    public K getKey() {
        return key;
    }

    public V getValue(int i) {
        return array.get(i).getValue();
    }

    //return value
    public V getValue() {
        return value;
    }

    protected void setKey(K k) {
        key = k;
    }

    protected V setValue(V v) {
        V show = value;
        value = v;

        return show;
    }

    //Find input key
    public int findIndex(K k) {
        for (int i = 0; i < array.size(); i++) {
            if (array.get(i).getKey().equals(k)) {
                return i;
            }
        }
        return -1;
    }

    //Find input key
    public V addFreq(K k) {
        for (int i = 0; i < array.size(); i++) {
            if (array.get(i).getKey().equals(k)) {
                return array.get(i).getValue();
            }
        }
        return null;
    }

    //find input value
    public boolean findValue(V v) {
        for (int i = 0; i < array.size(); i++) {
            if (array.get(i).getValue().equals(v)) {
                return true;
            }
        }
        return false;
    }
 
    //check for key
    public boolean findKey(K k) {
        for (int i = 0; i < array.size(); i++) {
            if (array.get(i).getKey().equals(k)) {
                return true;
            }
        }
        return false;
    }

    //return key for input value
    public K retValue(V v) {
        for (int i = 0; i < array.size(); i++) {
            if (array.get(i).getValue().equals(v)) {
                return array.get(i).getKey();
            }
        }
        return null;
    }
 
    //return value for input key
    public V retKey(K k) {
        for (int i = 0; i < array.size(); i++) {
            if (array.get(i).getKey().equals(k)) {
                return array.get(i).getValue();
            }
        }
        return null;
    }

    public int size() {
        return array.size();
    }

    //put method
    public void input(K k, V v) {
        int temp = findIndex(k);
        if (temp == -1) {
            array.add(new AbstractMap(k, v));

        } else {
            array.get(temp).setValue(v);
        }

    }

    //swap location in map
    public void swap(int x, int y) {
        if (x <= array.size() && y <= array.size()) {
            ArrayList temp = new ArrayList<>();
            ArrayList temp1 = new ArrayList<>();
            temp.add(array.get(x));
            array.remove(x);
            temp1.add(array.get(y - 1));
            array.remove(y - 1);
            array.add(x, temp1.get(0));
            array.add(y, temp.get(0));
        }

    }

    //delete at given index
    public void remove(int x) {
        array.remove(x);
    }

    public AbstractMap clone() {

        return this;
    }

    //sorted map
    public void sort() {
        for (int i1 = 0; i1 < this.size(); i1++) {
            for (int i = i1 + 1; i < this.size(); i++) {
                if (Integer.valueOf(this.getValue(i1).toString()) > Integer.valueOf(this.getValue(i).toString())) {
                    this.swap(i1, i);

                }
            }
        }
    }

    //print contents of Map
    public void print() {
        StringBuilder temp = new StringBuilder();
        for (int i = 0; i < array.size(); i++) {
            temp.append(array.get(i).getKey() + " <> " + array.get(i).getValue() + " ");
        }
        System.out.println(temp);
    }
 
    //return string to print
    public StringBuilder filePrint() {
        StringBuilder temp = new StringBuilder();
        for (int i = 0; i < array.size(); i++) {
            temp.append(array.get(i).getKey() + " <> " + array.get(i).getValue() + " ");
        }
        return temp;
    }

    //helps assign string to char
    public StringBuffer mapping(String node, char binary) {
        String[] array = node.split(" ");
        StringBuffer temp = new StringBuffer();
        for (int i = 0; i < array.length; i++) {
            temp.append(" " + binary + String.valueOf(array[i]));

        }

        return temp;
    }

 

}

No comments:

Post a Comment

Thank you very much for viewing this entry and I hope you are able to return soon to continue to enjoy more of the site.
Please share your thoughts in the comment section.
Be blessed and enjoy life!