Skip to content

Instantly share code, notes, and snippets.

View wkdalsgh192's full-sized avatar
🏠
Working from home

Blue_Avocado wkdalsgh192

🏠
Working from home
View GitHub Profile
ExecutorService executorService = Executors.newSingleThreadExecutor();
Callable<String> hello = () -> {
Thread.sleep(2000L);
return "Hello";
};
Callable<String> java = () -> {
Thread.sleep(3000L);
return "Java";
// when there is no return value
CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
System.out.println("Hello "+ Thread.currentThread().getName());
});
future2.get(); // get() should be called to execute the thread
// when there is a return value
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
System.out.println("HEllo " + Thread.currentThread().getName());
// Using a callback method
CompletableFuture<String> future4 = CompletableFuture.supplyAsync(() -> {
System.out.println("Hello "+ Thread.currentThread().getName());
return "Hello";
}).thenApply((s) -> { // thenApply, thenAccept or thenRun can also be used here
System.out.println(Thread.currentThread().getName());
return s.toUpperCase(Locale.ROOT);
});
System.out.println(future4.get());
// 1. Assign a function to a variable
const sayHello = function() {
console.log("Hello");
return "Hello, ";
}
// Invoke it using the variable
sayHello();
// 2. Pass a function as a parameter
function sayHello() {
return "Hello, ";
}
function greeting(helloMessage, name) {
console.log(helloMessage() + name);
}
greeting(sayHello, "JavaScript!");
// Creata a hash map class and put values into the map
Map<String, String> map = new HashMap<>();
map.put("1", "A");
map.put("2", "B");
map.put("3", "C");
// Create a class that only contains a collection.
public class GameRanking {
public class LottoService {
private static final int LOTTO_NUMBERS_SIZE = 6;
public void createLottoNumber() {
List<Long> lottoNumbers = createNonDuplicateNumbers();
validateSize(lottoNumbers);
validateDuplicate(lottoNumbers);
}
public void validateSize(List<Long> lottoNumbers) {
public class LottoService {
private static final int LOTTO_NUMBERS_SIZE = 6;
public void createLottoNumber() {
List<Long> lottoNumbers = createNonDuplicateNumbers();
validateSize(lottoNumbers);
validateDuplicate(lottoNumbers);
}
public void validateSize(List<Long> lottoNumbers) {
public class LottoTicket {
private static final int LOTTO_NUMBERS_SIZE = 6;
public LottoTicket(List<Long> lottoNumbers) {
List<Long> lottoNumbers = createNonDuplicateNumbers();
validateSize(lottoNumbers);
validateDuplicate(lottoNumbers);
}
public void validateSize(List<Long> lottoNumbers) {
public class Orders {
private final List<Order> orders;
public Orders(List<Order> orders) {
this.orders = orders;
}
public long getAmountSum() {
return orders.stream()
.mapToLong(Order::getAmount)