Skip to content

Instantly share code, notes, and snippets.

View SiAust's full-sized avatar
🏠
Open to new opportunities

Simon Aust SiAust

🏠
Open to new opportunities
View GitHub Profile
@SiAust
SiAust / ReHashTable.java
Created August 30, 2020 09:51
Scaling the HashTable by rehashing the contents.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
HashTable<String> hashTable = new HashTable<>(5); // initial size
Scanner sc = new Scanner(System.in);
@SiAust
SiAust / ReverseList.java
Last active September 1, 2020 09:32
Reverse list getter
Scanner sc = new Scanner(System.in);
List<Integer> numbers = Arrays.stream(sc.nextLine().split(" "))
.mapToInt(Integer::parseInt).boxed().collect(Collectors.toList());
int index = sc.nextInt();
if (index < 0) {
System.out.println(numbers.get(numbers.size() + index));
} else {
System.out.println(numbers.get(index));
}
@SiAust
SiAust / TemplatePattern.java
Created September 2, 2020 13:13
A simple example of Template pattern.
class Scratch {
public static void main(String[] args) {
Worker programmer = new Programmer();
Worker carpenter = new Carpenter();
programmer.goToWork();
carpenter.goToWork();
}
}
@SiAust
SiAust / Enum.java
Created September 3, 2020 14:20
Example of an enum class.
public enum DayOfWeek {
MONDAY(1,true),
TUESDAY(2,true),
WEDNESDAY(3,false),
THURSDAY(4,false),
FRIDAY(5,true),
SATURDAY(6,false),
SUNDAY(7,true);
private final boolean sunny;
@SiAust
SiAust / AbstractFactory.java
Created September 4, 2020 13:11
Example Abstract factory design pattern
class TestDrive {
public static void main(String[] args) throws InterruptedException {
Phone phone;
PhoneFactory iphoneFactory = new IphoneFactory();
PhoneFactory samsungFactory = new SamsungFactory();
System.out.println("-Hello, I need Android phone");
System.out.println("-Okay! Please wait for a sec, - Calling to the SamsungFactory. -Bring me the Samsung Galaxy S10");
Thread.sleep(1500);
@SiAust
SiAust / Serialization.java
Created September 5, 2020 09:45
Example of Serializing some classes.
import javax.imageio.IIOException;
import javax.swing.*;
import java.io.*;
public class HyperSkill {
public static void main(String[] args) {
Person simon = new Person("simon", 21, new Address("stillwater", "uk"));
String fileName = "C:\\users\\si_au\\desktop\\simon.data";
try {
@SiAust
SiAust / HashingExample.java
Created September 11, 2020 13:03
Example hashing using bytes
class HashingFun {
public static void main(String[] args) throws NoSuchAlgorithmException {
MessageDigest hasher = MessageDigest.getInstance("SHA-256");
long count = 0;
Random random = new Random();
byte[] data = new byte[258 / 8];
while (true) {
@SiAust
SiAust / FunctionalSupplier.java
Created September 19, 2020 09:59
A functional supplier that returns incremented int
import java.util.function.*;
class FunctionUtils {
public static Supplier<Integer> getInfiniteRange() {
int[] n = {0};
return new Supplier<Integer>() {
@Override
public Integer get() {
@SiAust
SiAust / FuntionalComposition.java
Created September 20, 2020 13:33
Composing functions with default method combine.
import java.security.MessageDigest;
import java.util.Base64;
import java.util.Scanner;
import java.util.function.Function;
class ChainOfResponsibilityDemo {
/**
* Accepts a request and returns new request with data wrapped in the tag <transaction>...</transaction>
*/
import java.util.*;
import java.util.stream.LongStream;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
Optional<String> optAddress = AddressBook.getAddressByName(name);