Format code with prettier (#3375)

This commit is contained in:
acbin
2022-10-03 17:23:00 +08:00
committed by GitHub
parent 32b9b11ed5
commit e96f567bfc
464 changed files with 11483 additions and 6189 deletions

View File

@ -5,6 +5,7 @@ import java.util.LinkedList;
// implementation of generic hashmaps using array of Linked Lists
public class GenericHashMapUsingArray<K, V> {
private int size; // n (total number of key-value pairs)
private LinkedList<Node>[] buckets; // N = buckets.length
private float lf = 0.75f;
@ -13,6 +14,7 @@ public class GenericHashMapUsingArray<K, V> {
initBuckets(16);
size = 0;
}
// load factor = 0.75 means if we need to add 100 items and we have added
// 75, then adding 76th item it will double the size, copy all elements
// & then add 76th item.
@ -114,6 +116,7 @@ public class GenericHashMapUsingArray<K, V> {
}
public class Node {
K key;
V value;

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.LinkedList;
public class GenericHashMapUsingArrayList<K, V> {
ArrayList<LinkedList<Node>> buckets;
private float lf = 0.5f;
private int size;
@ -100,6 +101,7 @@ public class GenericHashMapUsingArrayList<K, V> {
}
private class Node {
K key;
V val;
@ -108,5 +110,4 @@ public class GenericHashMapUsingArrayList<K, V> {
this.val = val;
}
}
}

View File

@ -1,6 +1,5 @@
package com.thealgorithms.datastructures.hashmap.hashing;
import java.lang.Math;
import java.util.Objects;
@ -71,19 +70,26 @@ public class HashMapCuckooHashing {
int hash, loopCounter = 0;
if (isFull()) {
System.out.println("Hash table is full, lengthening & rehashing table");
System.out.println(
"Hash table is full, lengthening & rehashing table"
);
reHashTableIncreasesTableSize();
}
if (checkTableContainsKey(key)) {
throw new IllegalArgumentException("Key already inside, no duplicates allowed");
throw new IllegalArgumentException(
"Key already inside, no duplicates allowed"
);
}
while (loopCounter <= thresh) {
loopCounter++;
hash = hashFunction1(key);
if ((buckets[hash] == null) || Objects.equals(buckets[hash], AVAILABLE)) {
if (
(buckets[hash] == null) ||
Objects.equals(buckets[hash], AVAILABLE)
) {
buckets[hash] = wrappedInt;
size++;
checkLoadFactor();
@ -110,7 +116,9 @@ public class HashMapCuckooHashing {
buckets[hash] = wrappedInt;
wrappedInt = temp;
}
System.out.println("Infinite loop occurred, lengthening & rehashing table");
System.out.println(
"Infinite loop occurred, lengthening & rehashing table"
);
reHashTableIncreasesTableSize();
insertKey2HashTable(key);
}
@ -132,7 +140,6 @@ public class HashMapCuckooHashing {
this.thresh = (int) (Math.log(tableSize) / Math.log(2)) + 2;
}
/**
* deletes a key from the hash map and adds an available placeholder
*
@ -157,7 +164,9 @@ public class HashMapCuckooHashing {
size--;
return;
}
throw new IllegalArgumentException("Key " + key + " already inside, no duplicates allowed");
throw new IllegalArgumentException(
"Key " + key + " already inside, no duplicates allowed"
);
}
/**
@ -168,7 +177,9 @@ public class HashMapCuckooHashing {
if ((buckets[i] == null) || Objects.equals(buckets[i], AVAILABLE)) {
System.out.println("Bucket " + i + ": Empty");
} else {
System.out.println("Bucket " + i + ": " + buckets[i].toString());
System.out.println(
"Bucket " + i + ": " + buckets[i].toString()
);
}
}
System.out.println();
@ -191,20 +202,32 @@ public class HashMapCuckooHashing {
if (Objects.equals(buckets[hash], wrappedInt)) return hash;
hash = hashFunction2(key);
if (!Objects.equals(buckets[hash], wrappedInt))
throw new IllegalArgumentException("Key " + key + " not found in table");
else {
if (
!Objects.equals(buckets[hash], wrappedInt)
) throw new IllegalArgumentException(
"Key " + key + " not found in table"
); else {
return hash;
}
}
/**
* checks if key is inside without any output other than returned boolean.
*
* @param key the desired key to be found
* @return int the index where the key is located
*/
public boolean checkTableContainsKey(int key){
return ((buckets[hashFunction1(key)] != null && buckets[hashFunction1(key)].equals(key)) || (buckets[hashFunction2(key)] != null && buckets[hashFunction2(key)] == key));
public boolean checkTableContainsKey(int key) {
return (
(
buckets[hashFunction1(key)] != null &&
buckets[hashFunction1(key)].equals(key)
) ||
(
buckets[hashFunction2(key)] != null &&
buckets[hashFunction2(key)] == key
)
);
}
/**
@ -214,7 +237,10 @@ public class HashMapCuckooHashing {
public double checkLoadFactor() {
double factor = (double) size / tableSize;
if (factor > .7) {
System.out.printf("Load factor is %.2f , rehashing table\n", factor);
System.out.printf(
"Load factor is %.2f , rehashing table\n",
factor
);
reHashTableIncreasesTableSize();
}
return factor;
@ -250,5 +276,8 @@ public class HashMapCuckooHashing {
}
return response;
}
public int getNumberOfKeysInTable(){return size;}
public int getNumberOfKeysInTable() {
return size;
}
}

View File

@ -107,7 +107,9 @@ public class HashMapLinearProbing {
if (buckets[i] == null || buckets[i] == AVAILABLE) {
System.out.println("Bucket " + i + ": Empty");
} else {
System.out.println("Bucket " + i + ": " + buckets[i].toString());
System.out.println(
"Bucket " + i + ": " + buckets[i].toString()
);
}
}
}
@ -133,8 +135,7 @@ public class HashMapLinearProbing {
buckets[hash] = AVAILABLE;
return hash;
}
} catch (Exception E) {
}
} catch (Exception E) {}
if (hash + 1 < hsize) {
hash++;
@ -159,7 +160,9 @@ public class HashMapLinearProbing {
public void checkLoadFactor() {
double factor = (double) size / hsize;
if (factor > .7) {
System.out.println("Load factor is " + factor + ", lengthening table");
System.out.println(
"Load factor is " + factor + ", lengthening table"
);
lengthenTable();
} else {
System.out.println("Load factor is " + factor);

View File

@ -15,7 +15,9 @@ import java.util.Map;
public class Intersection {
public static List<Integer> intersection(int[] arr1, int[] arr2) {
if (arr1 == null || arr2 == null || arr1.length == 0 || arr2.length == 0) {
if (
arr1 == null || arr2 == null || arr1.length == 0 || arr2.length == 0
) {
return Collections.emptyList();
}
Map<Integer, Integer> cnt = new HashMap<>(16);
@ -32,7 +34,5 @@ public class Intersection {
return res;
}
private Intersection() {
}
private Intersection() {}
}

View File

@ -5,7 +5,6 @@ import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int choice, key;
HashMap h = new HashMap(7);
@ -21,27 +20,31 @@ public class Main {
choice = In.nextInt();
switch (choice) {
case 1: {
System.out.println("Enter the Key: ");
key = In.nextInt();
h.insertHash(key);
break;
}
case 2: {
System.out.println("Enter the Key delete: ");
key = In.nextInt();
h.deleteHash(key);
break;
}
case 3: {
System.out.println("Print table");
h.displayHashtable();
break;
}
case 4: {
In.close();
return;
}
case 1:
{
System.out.println("Enter the Key: ");
key = In.nextInt();
h.insertHash(key);
break;
}
case 2:
{
System.out.println("Enter the Key delete: ");
key = In.nextInt();
h.deleteHash(key);
break;
}
case 3:
{
System.out.println("Print table");
h.displayHashtable();
break;
}
case 4:
{
In.close();
return;
}
}
}
}

View File

@ -3,8 +3,8 @@ package com.thealgorithms.datastructures.hashmap.hashing;
import java.util.Scanner;
public class MainCuckooHashing {
public static void main(String[] args) {
public static void main(String[] args) {
int choice, key;
HashMapCuckooHashing h = new HashMapCuckooHashing(7);
@ -24,41 +24,59 @@ public class MainCuckooHashing {
choice = In.nextInt();
switch (choice) {
case 1: {
System.out.println("Enter the Key: ");
key = In.nextInt();
h.insertKey2HashTable(key);
break;
}
case 2: {
System.out.println("Enter the Key delete: ");
key = In.nextInt();
h.deleteKeyFromHashTable(key);
break;
}
case 3: {
System.out.println("Print table:\n");
h.displayHashtable();
break;
}
case 4: {
In.close();
return;
}
case 5: {
System.out.println("Enter the Key to find and print: ");
key = In.nextInt();
System.out.println("Key: " + key + " is at index: " + h.findKeyInTable(key) + "\n");
break;
}
case 6: {
System.out.printf("Load factor is: %.2f\n", h.checkLoadFactor());
break;
}
case 7: {
h.reHashTableIncreasesTableSize();
break;
}
case 1:
{
System.out.println("Enter the Key: ");
key = In.nextInt();
h.insertKey2HashTable(key);
break;
}
case 2:
{
System.out.println("Enter the Key delete: ");
key = In.nextInt();
h.deleteKeyFromHashTable(key);
break;
}
case 3:
{
System.out.println("Print table:\n");
h.displayHashtable();
break;
}
case 4:
{
In.close();
return;
}
case 5:
{
System.out.println(
"Enter the Key to find and print: "
);
key = In.nextInt();
System.out.println(
"Key: " +
key +
" is at index: " +
h.findKeyInTable(key) +
"\n"
);
break;
}
case 6:
{
System.out.printf(
"Load factor is: %.2f\n",
h.checkLoadFactor()
);
break;
}
case 7:
{
h.reHashTableIncreasesTableSize();
break;
}
}
}
}

View File

@ -5,7 +5,6 @@ import java.util.Scanner;
public class MainLinearProbing {
public static void main(String[] args) {
int choice, key;
HashMapLinearProbing h = new HashMapLinearProbing(7);
@ -23,37 +22,47 @@ public class MainLinearProbing {
choice = In.nextInt();
switch (choice) {
case 1: {
System.out.println("Enter the Key: ");
key = In.nextInt();
h.insertHash(key);
break;
}
case 2: {
System.out.println("Enter the Key delete: ");
key = In.nextInt();
h.deleteHash(key);
break;
}
case 3: {
System.out.println("Print table");
h.displayHashtable();
break;
}
case 4: {
In.close();
return;
}
case 5: {
System.out.println("Enter the Key to find and print: ");
key = In.nextInt();
System.out.println("Key: " + key + " is at index: " + h.findHash(key));
break;
}
case 6: {
h.checkLoadFactor();
break;
}
case 1:
{
System.out.println("Enter the Key: ");
key = In.nextInt();
h.insertHash(key);
break;
}
case 2:
{
System.out.println("Enter the Key delete: ");
key = In.nextInt();
h.deleteHash(key);
break;
}
case 3:
{
System.out.println("Print table");
h.displayHashtable();
break;
}
case 4:
{
In.close();
return;
}
case 5:
{
System.out.println(
"Enter the Key to find and print: "
);
key = In.nextInt();
System.out.println(
"Key: " + key + " is at index: " + h.findHash(key)
);
break;
}
case 6:
{
h.checkLoadFactor();
break;
}
}
}
}