add examples in java 8 (#12)
* code for chapters 3-9 in Java * code for chapters 1 in Java * code for chapter 2 in Java * missing CheckVoter for chapter 5 in Java * add missing sample output for SetCovering as a comment
This commit is contained in:
committed by
Aditya Bhargava
parent
12265a8c61
commit
4631b7a156
@@ -0,0 +1,17 @@
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PriceOfGroceries {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Map<String, Double> book = new HashMap<>();
|
||||
|
||||
// an apple costs 67 cents
|
||||
book.put("apple", 0.67);
|
||||
// milk costs $1.49
|
||||
book.put("milk", 1.49);
|
||||
book.put("avocado", 1.49);
|
||||
|
||||
System.out.println(book); // {apple=0.67, avocado=1.49, milk=1.49}
|
||||
}
|
||||
}
|
||||
21
05_hash_tables/java/02_check_voter/src/CheckVoter.java
Normal file
21
05_hash_tables/java/02_check_voter/src/CheckVoter.java
Normal file
@@ -0,0 +1,21 @@
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CheckVoter {
|
||||
private static Map<String, Boolean> voted = new HashMap<>();
|
||||
|
||||
private static void checkVoter(String name) {
|
||||
if (voted.containsKey(name)) {
|
||||
System.out.println("kick them out!");
|
||||
} else {
|
||||
voted.put(name, true);
|
||||
System.out.println("let them vote!");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
checkVoter("tom"); // let them vote!
|
||||
checkVoter("mike"); // let them vote!
|
||||
checkVoter("mike"); // kick them out!
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user