ModulePackageTest
ModuleAppTest
ModuleHello
ModulePerson
ModulePackageTest
PersonPackageTest
PersonPackageTest
kr.ac.dankook.ace.app
kr.ac.dankook.ace.dao
kr.ac.dankook.ace.dto
PersonPackageTest-src
lecture9
lecture9
Java Double Colon Operator
Java :: 연산자 (Double Colon Operator)는 람다식을 대체하여 메서드 참조(method reference)로 사용된다. 즉, 람다식이 사용될 수 있는 Functional Interface Implementation에서만 사용 가능하다.
-
- Using Lambda
Comparator<Person> c = (Person p1, Person p2) -> p1.getName().compareTo(p2.getName());
- Using Lambda
-
- Using Lambda with type interference
Comparator<Person> c = (p1, p2) -> p1.getName().CompareTo(p2.getName());
- Using method reference ( :: operator)
Comparator<Person> c = Comparator.comparing(Person::getName);
- Method reference ( :: operator)
Function<Person, String> getName = Person::getName; // Person->String String name = getName.apply(person1);
- Method reference ( :: operator)는 람다식과 동일한 처리를 하는 식이지만 메소드 본문을 제공하는 대신 기존 메소드를 이름으로 참조한다.
Function<Double, Double> sq = (Double x) -> x * x; // lambda double result = sq.apply(3); // 3 * 3 = 9 Function<Double, Double> sq2 = MyClass::square; // MyClass에 정의된 square 정적 메소드 double result2 = sq2.apply(3); // 3 * 3 = 9
BiFunction<Double, Double, Double> add = (x, y) -> x + y; // lambda double result = add.apply(3.1, 3.2); // 3.1 + 3.2 = 6.3 BiFunction<Double, Double, Double> add2 = MyClass::sum; // MyClass에 정의된 sum 정적 메소드 double result2 = add2.apply(3.1, 3.2); // 3.1 + 3.2 = 6.3
- Using Lambda with type interference
Java Lambda Default Functional Interface
Java Lambda 기본 함수형 인터페이스 java.util.function 패키지에 정의되어 있음.
-
- Functions
public interface Function<T, R> { R apply(T t); }
- Functions
-
- Suppliers
public interface Supplier<T> { T get(); }
- Consumers
public interface Consumer<T> { void accept(T t); }
- Predicates
public interface Predicate<T> { boolean test(T t); }
- Operators
public interface UnaryOperator<T> extends Function<T, T> { static <T> UnaryOperator<T> identity() { return t -> t; } }
public interface BinaryOperator<T> extends BiFunction<T,T,T> { public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) { Objects.requireNonNull(comparator); return (a, b) -> comparator.compare(a, b) <= 0 ? a : b; } public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) { Objects.requireNonNull(comparator); return (a, b) -> comparator.compare(a, b) >= 0 ? a : b; } }
- Suppliers
Lab4
Lab4 프로젝트 디렉토리 안에 모든 파일(src/*.java & bin/*.class)와 보고서 (2~3장)를 넣고 JAVA22_Lab4_분반_학번_이름.zip 압축한 후 제출 (Due by 5/18)
Lab4 Inheritance/Collection
PersonClassInterfaceCollectionTest
PersonClassInterfaceCollectionTest
PersonClassInterfaceCollectionTest
OO vs FP
Java9 Immutable Collection
Java9 Immutable Collection
https://dzone.com/articles/immutable-collections-in-java-9
https://docs.oracle.com/javase/9/core/creating-immutable-lists-sets-and-maps.htm#JSCOR-GUID-202D195E-6E18-41F6-90C0-7423B2C9B381
List list3 = List.of("One", "Two", "Three");
Set set3 = Set.of("One", "Two", "Three");
Map map = Map.of("One", "1", "Two", "2", "Three", "3");
Creating Immutable Collection (Java8 or lower)
- Collections.unmodifiableList(list) 사용
- Arrays.asList( … ) 사용
- stream.of(….).collect(collectingAndThen(toList(), Collections::unmodifiableList)) 사용
- Guava 라이브러리 사용
Java Collections: List, Set, Map
Java Collections: List, Set, Map (http://web.mit.edu/6.031/www/sp17/classes/02-basic-java/#java_collections)
A List contains an ordered collection of zero or more objects, where the same object might appear multiple times.
A Set is an unordered collection of zero or more unique objects.
A Map is similar to a dictionary (key, value).