Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Scanner;
- class HashTableOpen {
- private static Scanner sc;
- public static void main(String[] args) {
- sc = new Scanner(System.in);
- int size = sc.nextInt();
- HashTableAberta hashtable = new HashTableAberta(size);
- String op;
- int key;
- do {
- op = sc.nextLine();
- switch (op.split(" ")[0]) {
- case "keys":
- System.out.println(Arrays.toString(hashtable.keys()));
- break;
- case "values":
- System.out.println(Arrays.toString(hashtable.values()));
- break;
- case "put":
- key = Integer.parseInt(op.split(" ")[1]);
- String value = op.split(" ")[2];
- hashtable.put(key, value);
- System.out.println(hashtable.toString());
- break;
- case "remove":
- key = Integer.parseInt(op.split(" ")[1]);
- hashtable.remove(key);
- System.out.println(hashtable.toString());
- default:
- break;
- }
- } while (!op.equals("end"));
- }
- }
- class Pair {
- private Integer key;
- private String value;
- public Pair(Integer key, String value) {
- this.key = key;
- this.value = value;
- }
- public Integer getKey() {
- return key;
- }
- public String getValue() {
- return value;
- }
- public void setValue(String value) {
- this.value = value;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((key == null) ? 0 : key.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Pair other = (Pair) obj;
- if (key == null) {
- if (other.key != null)
- return false;
- } else if (!key.equals(other.key))
- return false;
- return true;
- }
- public String toString() {
- return "<" + this.getKey() + ", " + this.getValue() + ">";
- }
- }
- class HashTableAberta {
- private static final String DELETED = "DELETED";
- private Pair[] table;
- private int capacity;
- private int elements;
- private ArrayList<Integer> keys;
- public HashTableAberta(int size) {
- this.table = new Pair[size];
- this.capacity = size;
- this.elements = 0;
- keys = new ArrayList<Integer>();
- }
- private int hashFunction(Integer key) {
- return key % capacity;
- }
- private int probeLinear(int key, int probe) {
- return this.hashFunction(key + probe);
- }
- public void put(Integer key, String value) {
- int probe = 0;
- int hash = probeLinear(key, probe);
- while (probe < capacity && table[hash] != null
- && !table[hash].getValue().equals(DELETED) && !table[hash].getKey().equals(key)) {
- hash = probeLinear(key, ++probe);
- }
- if (table[hash] != null && table[hash].getKey().equals(key)) {
- table[hash].setValue(value);
- } else if (table[hash] == null || table[hash].getValue().equals(DELETED)) {
- table[hash] = new Pair(key, value);
- elements += 1;
- keys.add(key);
- }
- }
- public void remove(Integer key) {
- if (keys.remove(key)) {
- int probe = 0;
- int hash = probeLinear(key, probe);
- while (probe < capacity && table[hash] != null && !table[hash].getKey().equals(key)) {
- hash = probeLinear(key, ++probe);
- }
- if (table[hash] != null && table[hash].getKey().equals(key)) {
- table[hash].setValue(DELETED);
- elements -= 1;
- }
- }
- }
- public Object[] keys() {
- Collections.sort(keys);
- return keys.toArray();
- }
- public String[] values() {
- String[] values = new String[elements];
- int index = 0;
- for (Pair element : table) {
- if (element != null) {
- values[index++] = element.getValue();
- }
- }
- Arrays.sort(values);
- return values;
- }
- public String toString() {
- return Arrays.toString(table);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement