JetBrains Academy: Method references - Kamil-Jankowski/Learning-JAVA GitHub Wiki
Writing a method reference - 1:
Given the following class with a single method:
class Functions {
public static int fun(int value) {
return value + 10;
}
}
How to write a method reference to the method fun
?
Functions::fun
Writing a method reference - 2:
Given the following class with few methods:
class Customer {
String firstName;
String lastName;
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
Here is one instance of the class:
Customer customer = new Customer("Paul", "Morris");
How to write a method reference to the method getFirstName
using the instance?
customer::getFirstName
Writing a method reference - 3:
Given the following class containing getters and setters:
class Article {
String name;
String text;
String[] authors;
public String[] getAuthors() {
return authors;
}
// other getters and setters
}
How to write a method reference to the method getAuthors
without an instance?
Article::getAuthors
Writing a method reference - 4:
Given the following class containing getters and setters:
class Journal {
String title;
String publisher;
public Journal() { }
// getters and setters
}
How to write a method reference to the constructor of the class Journal
?
Journal::new
Writing a method reference - 5:
There is a standard class Math
.
class Math {
/**
* Converts an angle measured in degrees to an approximately
* equivalent angle measured in radians. The conversion from
* degrees to radians is generally inexact.
*/
public static double toRadians(double angdeg) {
return angdeg / 180.0 * PI;
}
// other methods
}
How to write a method reference to the method toRadians
?
Math::toRadians
Writing a method reference - 6:
The standard class Integer
. has a static method named parseInt
with a single argument.
Write a method reference to this method without ; at the end. Do not add whitespaces to your result.
Integer::parseInt
Ascending and descending sorting:
It is always nice to have one function that can sort array both ascending and descending. This function is implemented below and based on bubble sorting algorithm. The function takes 2 parameters - array and comparison function.
The comparison function should take 2 parameters and return the minimum of the two if the array should be sorted in the ascending order, and the maximum of the two if the array should be sorted in the descending order.
Use isAscending
variable do determine if array should be sorted in ascending or descending order. If the variable is true, then you need to sort the array in the ascending order, otherwise in the descending order.
import java.util.*;
import java.util.function.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean isAscending = scanner.nextLine().equals("ascending");
int[] array = Arrays.stream(scanner.nextLine().split(" "))
.mapToInt(Integer::parseInt)
.toArray();
BiFunction<Integer, Integer, Integer> comparator = null;
// write your code here
comparator = isAscending ? Math::min : Math::max;
//
sort(array, comparator);
Arrays.stream(array).forEach(e -> System.out.print(e + " "));
}
public static void sort(int[] array, BiFunction<Integer, Integer, Integer> comparator) {
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
if (comparator.apply(array[j], array[j + 1]) == array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
}
Apply function to all elements:
You need to implement a method that applies given function to all elements of the given array.
You need to change the existing array, and not create a new array.
import java.util.*;
import java.util.function.*;
public class Main {
public static <T> void applyFunction(T[] array, Function<T, T> func) {
for (int i = 0; i < array.length; i++){
T temp = array[i];
array[i] = func.apply(temp);
}
}
// do not change code below
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String method = scanner.nextLine();
String[] array = scanner.nextLine().split(" ");
applyFunction(array,
"lower".equals(method) ? String::toLowerCase :
"upper".equals(method) ? String::toUpperCase :
"new".equals(method) ? String::new :
"trim".equals(method) ? String::trim : String::intern);
Arrays.stream(array).forEach(e -> System.out.print(e + " "));
}
}