This file contains hidden or 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
ExecutorService executorService = Executors.newSingleThreadExecutor(); | |
Callable<String> hello = () -> { | |
Thread.sleep(2000L); | |
return "Hello"; | |
}; | |
Callable<String> java = () -> { | |
Thread.sleep(3000L); | |
return "Java"; |
This file contains hidden or 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
// 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()); |
This file contains hidden or 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
// 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()); |
This file contains hidden or 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
// 1. Assign a function to a variable | |
const sayHello = function() { | |
console.log("Hello"); | |
return "Hello, "; | |
} | |
// Invoke it using the variable | |
sayHello(); |
This file contains hidden or 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
// 2. Pass a function as a parameter | |
function sayHello() { | |
return "Hello, "; | |
} | |
function greeting(helloMessage, name) { | |
console.log(helloMessage() + name); | |
} | |
greeting(sayHello, "JavaScript!"); |
This file contains hidden or 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
// 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 { |
This file contains hidden or 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
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) { |
This file contains hidden or 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
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) { |
This file contains hidden or 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
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) { |
This file contains hidden or 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
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) |