Lambdas Flashcards

1
Q

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);

A

items.forEach((k,v)->System.out.println(“Item : “ + k + “ Count : “ + v));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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);

A
items.forEach((k,v)->{
	System.out.println("Item : " + k + " Count : " + v);
	if("E".equals(k)){
		System.out.println("Hello E");
	}
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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);

A

for (Map.Entry entry : items.entrySet()) {
System.out.println(“Item : “ + entry.getKey() + “ Count : “ + entry.getValue());
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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”);

A

items.forEach(item->System.out.println(item));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

lambda

Output : C

List items = new ArrayList<>();

items. add(“A”);
items. add(“B”);
items. add(“C”);
items. add(“D”);
items. add(“E”);

A
items.forEach(item->{
	if("C".equals(item)){
		System.out.println(item);
	}
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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”);

A

items.forEach(item->System.out.println(item));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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”);

A

items.forEach(System.out::println);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Stream and filter
Output : B

List items = new ArrayList<>();

items. add(“A”);
items. add(“B”);
items. add(“C”);
items. add(“D”);
items. add(“E”);

A
//Stream and filter
//Output : B
items.stream()
	.filter(s->s.contains("B"))
	.forEach(System.out::println);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Read file as stream and print.

A

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();
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Read file as stream and print.

  1. filter out line 3 w/ lambda
  2. convert all content to upper case
  3. convert it into a List
  4. print list.
A

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);

}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Read file with BufferedReader and print.

br returns as stream and convert it into a List

print list.

A

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);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Use Scanner to read file + try-with-resources

A

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();
		}
	}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
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”);

A

List result = lines.stream()
.filter(line -> !”mkyong”. equals (line))
.collect(Collectors.toList());

result.forEach(System.out::println);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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"));
A

Person result = persons.stream()
.filter(x -> “michael”.equals(x.getName()))
.findAny()
.orElse(null);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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));
A

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()) &amp;&amp; 21==x.getAge()){
	return true;
		}
	return false;
	}).findAny()
	.orElse(null);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Use Stream to search for “michael”
Use map to convert stream to String
make String name = michael

List persons = Arrays.asList(new Person(“mkyong”, 20), new Person(“michael”, 21), new Person(“lawrence”, 23));

A

String name = persons.stream()
.filter(x -> “michael”.equals(x.getName()))
.map(Person::getName) //convert stream to String
.findAny()
.orElse(“”);

//name = michael