https://makecodework.tistory.com/entry/Java-%EB%9E%8C%EB%8B%A4%EC%8B%9DLambda-%EC%9D%B5%ED%9E%88%EA%B8%B0
1. 기초: comparable를 이용한 정렬 (sort)
package day5;
import java.util.Arrays;
class Rectangle implements Comparable<Rectangle> {
private int width, height;
public Rectangle( int width, int height){
super();
this.width = width ;
this.height = height ;
}
public int findArea() {
return width*height;
}
@Override
public String toString() {
return "Rectangle [width=" + width + ", height=" + height + "]";
}
@Override
public int compareTo(Rectangle o) {
return this.findArea() - o.findArea();
}
}
public class ComparableDemo {
public static void main(String[] args) {
Rectangle[] r = {
new Rectangle(10,5),
new Rectangle(2,5),
new Rectangle(4,3)
};
Arrays.sort(r);
for (Rectangle rectangle : r) {
System.out.println(rectangle);
}
}
}
2. 람다 연습 1
package day5;
import java.util.Arrays;
import java.util.Comparator;
class Rectangle implements Comparable<Rectangle> {
private int width, height;
public Rectangle( int width, int height){
super();
this.width = width ;
this.height = height ;
}
public int findArea() {
return width*height;
}
@Override
public String toString() {
return "Rectangle [width=" + width + ", height=" + height + "]";
}
@Override
public int compareTo(Rectangle o) {
return -( this.findArea() - o.findArea()) ;
}
}
public class ComparableDemo {
public static void main(String[] args) {
Rectangle[] r = {
new Rectangle(10,5),
new Rectangle(2,5),
new Rectangle(4,3)
};
System.out.println("----------------------------------------");
Arrays.sort(r, ( a,b ) -> a.findArea() - b.findArea() );
for (Rectangle rectangle : r) {
System.out.println(rectangle);
}
}
}
2-2. 람다 연습 2
interface Printable{
void print();
}
public class LamdaDemo2 {
public static void main(String[] args) {
Printable p;
p = () ->{ System.out.println("print");} ;
p.print();
}
}
print
3. 람다 응용 참조
package day5;
interface Mathmatical{
double calculate(double b);
}
interface Pickable{
char pick (String s, int i);
}
interface Computeble{
int compute (int x , int y);
}
class Utils{
int add( int a, int b) {
return a+b;
}
}
public class LamdaDemo3 {
public static double my(double b) {
return Math.abs(b);
}
public static void main(String[] args) {
Mathmatical m ;
Pickable p;
Computeble c ;
m = ( double b ) -> { return (b>0)? b : -b ; };
m = b -> (b>0)? b : -b ;
m.calculate(-5.5);
System.out.println(m.calculate(-5.5));
m = b -> Math.abs(b);
m.calculate(-50.2);
System.out.println(m.calculate(-50.2));
m = Math::abs;
m.calculate(-53.2);
System.out.println(m.calculate(-53.2));
p = (String s, int i) -> { return s.charAt(i) ;};
p = (String s, int i) -> s.charAt(i) ;
p = (s, i) -> s.charAt(i) ;
p = String::charAt;
p.pick("abc", 0);
c = Math::addExact;
c.compute(2, 3);
System.out.println(c.compute(2, 3));
Utils u = new Utils();
c = (a,b) -> u.add(a, b);
c = u::add;
}
}
4. 람다 응용
package day5;
interface NewObject<T> {
T getObject(T o);
}
interface NewArray<T> {
T [] getArray(int size);
}
public class LamdaDemo4 {
public static void main(String[] args) {
NewObject<String> s;
NewArray<Integer> i;
s = (String str) -> { return str; };
s = str -> new String(str);
s = String::new;
String str = s.getObject("hello");
i = size -> new Integer [size] ;
i = Integer[]::new;
System.out.println(i.getArray(10));
}
}
5. 람다와 익명클래스가 가리키는 대상의 차이
package day5;
interface Usethis{
void use();
}
public class LamdaDemo5 {
public void lamda() {
String hi = "hi";
Usethis u1 = new Usethis() {
@Override
public void use() {
System.out.println(this);
}
} ;
u1.use();
Usethis u2 = () -> System.out.println(this);
u2.use();
}
public static void main(String[] args) {
LamdaDemo5 demo = new LamdaDemo5();
demo.lamda();
System.out.println(demo);
}
}