Lambdas Flashcards
Loop a map and print out every item.
Map items = new HashMap<>();
items. put(“A”, 10);
items. put(“B”, 20);
items. put(“C”, 30);
items. put(“D”, 40);
items. put(“E”, 50);
items. put(“F”, 60);
items.forEach((k,v)->System.out.println(“Item : “ + k + “ Count : “ + v));
Loop a map and only print if “E” equals the String value
Map items = new HashMap<>();
items. put(“A”, 10);
items. put(“B”, 20);
items. put(“C”, 30);
items. put(“D”, 40);
items. put(“E”, 50);
items. put(“F”, 60);
items.forEach((k,v)->{ System.out.println("Item : " + k + " Count : " + v); if("E".equals(k)){ System.out.println("Hello E"); } });
Print a Map the regular way. No lambda.
Map items = new HashMap<>();
items. put(“A”, 10);
items. put(“B”, 20);
items. put(“C”, 30);
items. put(“D”, 40);
items. put(“E”, 50);
items. put(“F”, 60);
for (Map.Entry entry : items.entrySet()) {
System.out.println(“Item : “ + entry.getKey() + “ Count : “ + entry.getValue());
}
lambda
Output : A,B,C,D,E
List items = new ArrayList<>();
items. add(“A”);
items. add(“B”);
items. add(“C”);
items. add(“D”);
items. add(“E”);
items.forEach(item->System.out.println(item));
lambda
Output : C
List items = new ArrayList<>();
items. add(“A”);
items. add(“B”);
items. add(“C”);
items. add(“D”);
items. add(“E”);
items.forEach(item->{ if("C".equals(item)){ System.out.println(item); } });
lambda
Output : A,B,C,D,E
List items = new ArrayList<>();
items. add(“A”);
items. add(“B”);
items. add(“C”);
items. add(“D”);
items. add(“E”);
items.forEach(item->System.out.println(item));
method reference
Output : A,B,C,D,E
List items = new ArrayList<>();
items. add(“A”);
items. add(“B”);
items. add(“C”);
items. add(“D”);
items. add(“E”);
items.forEach(System.out::println);
Stream and filter
Output : B
List items = new ArrayList<>();
items. add(“A”);
items. add(“B”);
items. add(“C”);
items. add(“D”);
items. add(“E”);
//Stream and filter //Output : B items.stream() .filter(s->s.contains("B")) .forEach(System.out::println);
Read file as stream and print.
public static void main(String args[]) {
String fileName = "c://lines.txt";
try (Stream stream = Files.lines(
Paths.get(fileName) ))
{
stream.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
Read file as stream and print.
- filter out line 3 w/ lambda
- convert all content to upper case
- convert it into a List
- print list.
public static void main(String args[]) {
String fileName = "c://lines.txt"; List list = new ArrayList<>();
try (Stream stream =Files.lines(
Paths.get(fileName))) {
list = stream.filter(line -> line.startsWith(“line3”))
.map(String::toUpperCase)
.collect(Collectors.toList());
} catch (IOException e) { e.printStackTrace(); } list.forEach(System.out::println); }
Read file with BufferedReader and print.
br returns as stream and convert it into a List
print list.
public static void main(String args[]) {
String fileName = "c://lines.txt"; List list = new ArrayList<>();
try ( BufferedReader br = Files.newBufferedReader( Paths.get(fileName)) ) { list = br.lines().collect(Collectors.toList() );
}catch ( IOException e ) { e.printStackTrace(); } list.forEach(System.out::println); }
Use Scanner to read file + try-with-resources
public static void main(String args[]) {
String fileName = "c://lines.txt";
try (Scanner scanner = new Scanner(new File(fileName))) {
while (scanner.hasNext()){ System.out.println(scanner.nextLine()); } } catch (IOException e) { e.printStackTrace(); } }
Filter out the line "mkyong" // convert list to stream // collect the output and convert streams to a List
import java.util.stream.Collectors;
List lines = Arrays.asList(“spring”, “node”, “mkyong”);
List result = lines.stream()
.filter(line -> !”mkyong”. equals (line))
.collect(Collectors.toList());
result.forEach(System.out::println);
Convert to steam
we want “michael” only
If ‘findAny’ then return found
List persons = Arrays.asList( new Person("mkyong"), new Person("michael"), new Person("lawrence"));
Person result = persons.stream()
.filter(x -> “michael”.equals(x.getName()))
.findAny()
.orElse(null);
Filter with multiple conditions.
Filter out a stream where name equals Michael
Use findAny() to maximize parallel processing speed.
List persons = Arrays.asList( new Person("mkyong", 20), new Person("michael", 21), new Person("lawrence", 23));
Person result = persons.stream()
.filter((x) -> “michael”.equals(x.getName()) && 21==x.getAge())
.findAny()
.orElse(null);
//or like this Person result = persons.stream() .filter(x -> { if("michael".equals(x.getName()) && 21==x.getAge()){ return true; } return false; }).findAny() .orElse(null);