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.HashMap;
- import java.util.List;
- import java.util.Scanner;
- public class solve {
- static class Pair implements Comparable<Pair>{
- private int key;
- private String value;
- public Pair(int key, String value) {
- this.key = key;
- this.value = value;
- }
- public int getKey(){ return this.key;}
- public String getValue() { return this.value;}
- public void setValue(String value) { this.value = value;}
- @Override
- public boolean equals(Object obj) {
- if(obj != null) {
- if(obj instanceof Pair) {
- return ((Pair)obj).getKey() == this.getKey();
- }
- }
- return false;
- }
- @Override
- public String toString() {
- return "<"+this.key+", "+this.value+">";
- }
- @Override
- public int compareTo(Pair o) {
- if (this.getKey() < o.getKey()) {
- return -1;
- }
- if (this.getKey() > o.getKey()) {
- return 1;
- }
- return this.getValue().compareTo(o.getValue());
- }
- }
- static class hashTable{
- private Pair table[];
- private HashMap<Integer, String> persons;
- private int sz;
- private int elements;
- public hashTable(int size) {
- table = new Pair[size];
- persons = new HashMap<>();
- sz = size;
- this.elements = 0;
- }
- private int getIndex(int key) {
- return key%sz;
- }
- private void print() {
- System.out.print("[");
- for(int i = 0; i < sz; i++) {
- if(table[i] != null) System.out.print(table[i].toString());
- else System.out.print("null");
- if(i != sz-1) System.out.print(", ");
- }
- System.out.println("]");
- }
- public void put(int key, String value) {
- Pair novo = new Pair(key, value);
- if (this.elements < this.sz) {
- int idx = this.getIndex(key);
- while (this.table[idx] != null) {
- if (this.table[idx].equals(novo)) {
- this.elements--;
- persons.remove(key);
- break;
- }
- idx++;
- }
- persons.put(key, value);
- this.table[idx] = novo;
- this.elements++;
- }
- this.print();
- }
- public void remove(int key) {
- int idx = this.getIndex(key);
- Pair aux = new Pair(key, "");
- int pos = -1;
- while(idx < sz) {
- if(table[idx] != null && table[idx].equals(aux)) pos = idx;
- idx++;
- }
- if(pos != -1) {
- persons.remove(key);
- table[pos] = null;
- this.elements--;
- }
- this.print();
- }
- public void keys() {
- ArrayList aux = new ArrayList();
- aux.addAll(persons.keySet());
- Collections.sort(aux);
- System.out.println(Arrays.toString(aux.toArray()));
- }
- public void values() {
- ArrayList aux = new ArrayList();
- aux.addAll(persons.values());
- Collections.sort(aux);
- System.out.println(Arrays.toString(aux.toArray()));
- }
- }
- private static Scanner sc;
- public static void main(String[] args) {
- sc = new Scanner(System.in);
- int sz = sc.nextInt();
- hashTable table = new hashTable(sz);
- while(true) {
- String action = sc.nextLine();
- String[] choice = action.split(" ");
- if(choice[0].equals("end")) break;
- switch(choice[0]) {
- case "keys":
- table.keys();
- break;
- case "remove":
- table.remove(Integer.parseInt(choice[1]));
- break;
- case "put":
- table.put(Integer.parseInt(choice[1]), choice[2]);
- break;
- case "values":
- table.values();
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement