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 / 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 / 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 / HashTable.java
Created August 29, 2020 14:06
A simple example of HashTable in Java
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
HashTable<String> table = new HashTable<>(Integer.parseInt(sc.nextLine()));
while (sc.hasNext()) {
@SiAust
SiAust / LocalDate.java
Created August 26, 2020 10:08
Output each Monday of a given month.
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] input = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
@SiAust
SiAust / HTTP.java
Created June 20, 2020 13:08
Example HTTP connection
class HTTP {
public static void main(String[] args) {
HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create("http://www.bbc.com")).build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
@SiAust
SiAust / Iterator.java
Created June 19, 2020 13:37
Example custom iterator for a class.
class Range implements Iterable<Long> {
private final long fromInclusive;
private final long toExclusive;
public Range(long from, long to) {
this.fromInclusive = from;
this.toExclusive = to;
}
@SiAust
SiAust / MatchResultsStream.java
Created June 15, 2020 09:51
Matcher, Pattern, MatchResults, streaming.
/*
* Compiles a regex pattern, calls a Matcher on with the input string as a param,
* calls .results() which returns a stream of MatchResults for each successful match.
* .count() counts the elements in the stream.
* */
String input = new Scanner(System.in).nextLine(); // "accggaabb"
System.out.println((double) Pattern.compile("(?:[cg])").matcher(input).results().count() / input.length() * 100);
// 44.44444444444444
@SiAust
SiAust / StringFormat.java
Created June 12, 2020 10:41
Bumps the text by filling space.
// Bumps the text by filling space to the left with whitespace
System.out.printf("%20s%n", "hello:");
System.out.printf("%20s%n", "hello my old friend:");
System.out.printf("%20s%n", "hello, again:");
System.out.printf("%20s%n", "hello, once more:");
@SiAust
SiAust / Strategy.java
Created June 9, 2020 09:22
Example of a Strategy pattern that could be used for API calls.
// Strategy design pattern with API requests, example.
class ApiRequestSender {
ApiRequestMethod apiRequestMethod;
public static void main(String[] args) {
ApiRequestSender apiCategoriesRequest = new ApiRequestSender();
apiCategoriesRequest.setApiRequestMethod(new CategoriesRequest());
System.out.println(apiCategoriesRequest.sendRequest("www.spotify.com/endpoint/categories"));
@SiAust
SiAust / Snippets.java
Last active June 8, 2020 09:02
while loop and streams
// To get a int[] from a single space separated string
int[] range = Stream.of(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
// Nice way to do a simple loop
while (n-- > 0) { // nice way to do a loop, postfix decrement
// do something
}