Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ************************************************************************************************************************
- // what's wrong with this code?
- // ************************************************************************************************************************
- public class Person {
- private String firstName;
- private String lastName;
- public void setName(String firstName, String lastName) {
- this.firstName = firstName;
- this.lastName = lastName;
- }
- public String getName() {
- return this.firstName + " " + this.lastName;
- }
- public static void printName() {
- System.out.println(this.firstName + " " + this.lastName);
- }
- }
- // ************************************************************************************************************************
- // rewrite the following as a lamba statement
- // ************************************************************************************************************************
- new Thread(new Runnable() {
- @Override
- public void run() {
- System.out.println("Hello world!");
- }
- });
- // ************************************************************************************************************************
- // what is the value of the variable "i"?
- // ************************************************************************************************************************
- byte[] bytes = {(byte)0x82, (byte)0x19, (byte)0x02};
- int i = (0xF & bytes[0]) + (bytes[1] >>> 4) + (bytes[2] | 0x01);
- // ************************************************************************************************************************
- // how would you rewrite the following with a method reference?
- // ************************************************************************************************************************
- Arrays.asList("one", "two", "three").forEach(n -> System.out.println(n));
- // ************************************************************************************************************************
- // a. what's wrong with this code?
- // ************************************************************************************************************************
- @FunctionalInterface
- public interface FooBar {
- public void foo(int num);
- public void bar(String str);
- default void xar(String str) {
- System.out.println(str);
- }
- }
- // =============================================================================
- // b. explain the default keywork
- // =============================================================================
- // ************************************************************************************************************************
- // use the stream API to produce the desired result
- // ************************************************************************************************************************
- public class Person {
- @Getter
- private String name;
- @Getter
- private int age;
- Person(String name, int age) {
- this.name = name;
- this.age = age;
- }
- }
- List<Person> people = Arrays.asList(
- new Person("David", 22);
- new Person("Nancy", 37);
- new Person("Julie", 23);
- new Person("Joe", 15);
- new Person("Julie", 12);
- new Person("David", 38);
- new Person("Nancy", 22);
- new Person("Vicky", 72);
- new Person("Steve", 20);
- new Person("Ed", 89);
- );
- // use the list of Person objects to create an ArrayList of sorted names
- // Conditions:
- // - age is greater than or equal to 20
- // - age is less than or equal to 80
- // - names are sorted alphbetically
- // - no name appears more than once
- // Result should be:
- // - ["David", "Julie", "Nancy", "Steve", "Vicky"];
- // ************************************************************************************************************************
- // a. what's wrong with this code?
- // ************************************************************************************************************************
- int totalOdds = 0;
- IntStream.range(1, 9)
- .filter(n -> !(n % 2 == 0))
- .forEach(n -> totalOdds += n);
- // =============================================================================
- // b. how would you refactor to maintain the lambda statements but make it
- // compile?
- // =============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement