This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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:"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |