Manioc

hashTable de merda

Jun 15th, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.60 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class solve {
  9.    
  10.     static class Pair implements Comparable<Pair>{
  11.         private int key;
  12.         private String value;
  13.         public Pair(int key, String value) {
  14.             this.key = key;
  15.             this.value = value;
  16.         }
  17.        
  18.         public int getKey(){ return this.key;}
  19.        
  20.         public String getValue() { return this.value;}
  21.        
  22.         public void setValue(String value) { this.value = value;}
  23.         @Override
  24.         public boolean equals(Object obj) {
  25.             if(obj != null) {
  26.                 if(obj instanceof Pair) {
  27.                     return ((Pair)obj).getKey() == this.getKey();
  28.                 }
  29.             }
  30.             return false;
  31.         }
  32.        
  33.         @Override
  34.         public String toString() {
  35.             return "<"+this.key+", "+this.value+">";
  36.         }
  37.  
  38.         @Override
  39.         public int compareTo(Pair o) {
  40.             if (this.getKey() < o.getKey()) {
  41.                 return -1;
  42.             }
  43.             if (this.getKey() > o.getKey()) {
  44.                 return 1;
  45.             }
  46.             return this.getValue().compareTo(o.getValue());
  47.         }
  48.     }
  49.     static class hashTable{
  50.        
  51.         private List table[];
  52.         private HashMap<Integer, String> persons;
  53.         private int sz;
  54.        
  55.         public hashTable(int size) {
  56.             table = new ArrayList[size];
  57.             persons = new HashMap<>();
  58.             sz = size;
  59.             for(int i = 0; i < size; i++) {
  60.                 table[i] = new ArrayList<Pair>();
  61.             }
  62.         }
  63.        
  64.         private int getIndex(int key) {
  65.             return key%sz;
  66.         }
  67.        
  68.         private void print() {
  69.             System.out.print("[");
  70.             for(int i = 0; i < sz; i++) {
  71.                
  72.                 System.out.print("[");
  73.                 for(int j = 0; j < table[i].size(); j++) {
  74.                     System.out.print(table[i].get(j).toString());
  75.                     if(j != table[i].size()-1) System.out.print(", ");
  76.                 }
  77.                 System.out.print("]");
  78.                 if(i != sz-1) System.out.print(", ");
  79.             }
  80.             System.out.println("]");
  81.         }
  82.        
  83.         public void put(int key, String value) {
  84.             int idx = this.getIndex(key);
  85.            
  86.             Pair novo = new Pair(key, value);
  87.             if(table[idx].contains(novo)) {
  88.                 int pos = table[idx].indexOf(novo);
  89.                 ((Pair)table[idx].get(pos)).setValue(value);
  90.             } else table[idx].add(new Pair(key, value));
  91.            
  92.             persons.put(key, value);
  93.             //Collections.sort(table[idx]);
  94.             this.print();
  95.         }
  96.        
  97.         public void remove(int key) {
  98.             int idx = this.getIndex(key);
  99.            
  100.             Pair novo = new Pair(key, "");
  101.             if(table[idx].contains(novo)) {
  102.                 persons.remove(key);
  103.                 table[idx].remove(novo);
  104.             }
  105.            
  106.             this.print();
  107.         }
  108.        
  109.         public void keys() {
  110.            
  111.             ArrayList aux = new ArrayList();
  112.             aux.addAll(persons.keySet());
  113.             Collections.sort(aux);
  114.             System.out.println(Arrays.toString(aux.toArray()));
  115.         }
  116.        
  117.         public void values() {
  118.             ArrayList aux = new ArrayList();
  119.             aux.addAll(persons.values());
  120.             Collections.sort(aux);
  121.             System.out.println(Arrays.toString(aux.toArray()));
  122.         }
  123.        
  124.     }
  125.     private static Scanner sc;
  126.     public static void main(String[] args) {
  127.         sc = new Scanner(System.in);
  128.        
  129.         int sz = sc.nextInt();
  130.         hashTable table = new hashTable(sz);
  131.         while(true) {
  132.             String action = sc.nextLine();
  133.             String[] choice = action.split(" ");
  134.            
  135.             if(choice[0].equals("end")) break;
  136.             switch(choice[0]) {
  137.                 case "keys":
  138.                     table.keys();
  139.                     break;
  140.                 case "remove":
  141.                     table.remove(Integer.parseInt(choice[1]));
  142.                     break;
  143.                 case "put":
  144.                     table.put(Integer.parseInt(choice[1]), choice[2]);
  145.                     break;
  146.                 case "values":
  147.                     table.values();
  148.                     break;
  149.    
  150.             }
  151.         }
  152.        
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment