Advertisement
C3EQUALZ

For_stack_overflow

Aug 21st, 2023
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1.  
  2.     public interface CarSet {
  3.         boolean add(Car car);
  4.      
  5.         boolean remove(Car car);
  6.      
  7.         int size();
  8.      
  9.         void clear();
  10.     }
  11.  
  12. ___________________________________________________________________________________
  13.  
  14.  
  15.     public class CarHashSet implements CarSet {
  16.      
  17.         private static final int INITIAL_CAPACITY = 16;
  18.         private static final double LOAD_FACTOR = 0.75;
  19.         private int size = 0;
  20.         private Entry[] array = new Entry[INITIAL_CAPACITY];
  21.      
  22.         @Override
  23.         public boolean add(Car car) {
  24.             if (size >= (array.length * LOAD_FACTOR))
  25.                 increaseArray();
  26.  
  27.             boolean added = add(car, array);
  28.  
  29.             if (added)
  30.                 size++;
  31.            
  32.             return added;
  33.         }
  34.      
  35.         private boolean add(Car car, Entry[] dst) {
  36.             int position = getElementPosition(car, dst.length);
  37.             if (dst[position] == null) {
  38.                 Entry entry = new Entry(car, null);
  39.                 dst[position] = entry;
  40.                 return true;
  41.             } else {
  42.             Entry existedElement = dst[position];
  43.             while (true) {
  44.                 if (existedElement.value.equals(car))
  45.                     return false;
  46.                 if (existedElement.next == null) {
  47.                     existedElement.next = new Entry(car, null);
  48.                     return true;
  49.                 }
  50.                 existedElement = existedElement.next;
  51.                
  52.                 }
  53.             }
  54.         }
  55.      
  56.         @Override
  57.         public boolean remove(Car car) {
  58.             int position = getElementPosition(car, array.length);
  59.             if (array[position] == null)
  60.                 return false;
  61.  
  62.             Entry secondLast = array[position];
  63.             Entry last = secondLast.next;
  64.             if (secondLast.value.equals(car)) {
  65.                 array[position] = last;
  66.                 size--;
  67.                 return true;
  68.             }
  69.  
  70.             while (last != null) {
  71.                 if (last.value.equals(car)) {
  72.                     secondLast.next = last.next;
  73.                     size--;
  74.                     return true;
  75.                 } else {
  76.                     secondLast = last;
  77.                     last = last.next;
  78.                     }
  79.                 }
  80.             return false;
  81.             }
  82.      
  83.         @Override
  84.         public int size() {return size;}
  85.        
  86.        
  87.      
  88.         @Override
  89.         public void clear() {
  90.         array = new Entry[INITIAL_CAPACITY];
  91.         size = 0;
  92.         }
  93.      
  94.         private void increaseArray() {
  95.         Entry[] newArray = new Entry[array.length * 2];
  96.         for (Entry entry : array) {
  97.             Entry existedElement = entry;
  98.             while (existedElement != null) {
  99.                 add(existedElement.value, newArray);
  100.                 existedElement = existedElement.next;
  101.                 }
  102.             }
  103.         array = newArray;
  104.         }
  105.      
  106.         private int getElementPosition(Car car, int arrayLength) {
  107.             return Math.abs(car.hashCode() % arrayLength);
  108.             }
  109.      
  110.         private static class Entry {
  111.             private Car value;
  112.             private Entry next;
  113.      
  114.             public Entry(Car value, Entry next) {
  115.             this.value = value;
  116.             this.next = next;
  117.         }
  118.     }
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement