어느덧 스무살이 된 자바. 좋든싫든 프로그래밍의 세계에 몸담고 있는 이상 부딪히지 않을 수 없는 언어인데요, 이 자바에 대한 올바른 모습의 이해화 앞으로 나아가야 할 방향을 모색해본다는 의미에서 "모던 자바의 역습"이라는 타이틀로 실시한 온라인 세미나의 슬라이드 자료입니다.
진행자: 김대우(http://lekdw.blogspot.kr/)
정도현(http://moreagile.net)
이번 세미나는 게임개발자이신 김대우님을 모시고 진행하게 되었는데요 자바 언어와 관련 하여 다음과 같은 주제로 두시간에 걸쳐 발표를 진행합니다.
- 간단히 살펴보는 자바20년의 발자취
- 자바를 둘러싼 진실 혹은 거짓(성능,생산성,품질)
- SI영역 이외에서 자바의 가치
- 모던자바의 역습: 자바8과 함수형 프로그래밍
- 자바 개발의 새로운 패러다임들
34. private List m_list=null;
private int process_file(String str_file_name){
String str_line;
List list_lines=new ArrayList();
int i_result=read_file(str_file_name,list_lines);
if(i_result==0){
List list_record=new ArrayList();
for(int i=0;i<list_lines.size();i++){
str_line=(String)list_lines.get(i);
Record record=new Record();
i_result=parse_line(str_line,record);
if(i_result!=0){
return i_result;
}
list_recordord.add(record);
}
m_list=list_record;
return 0;
}else{
return i_result;
}
}
http://www.slideshare.net/shintanimoto/from-old-java-to-modern-java
C-ish java
35. private List resultList;
private List processFile(String fileName)throws
SystemException {
List lines = readFile(fileName);
List recordList = new ArrayList();
for (int i = 0; i < lines.size(); i++) {
String line = (String) lines.get(i);
Record record = parseLine(line);
recordList.add(record);
}
return recordList;
}
http://www.slideshare.net/shintanimoto/from-old-java-to-modern-java
Java 1.4
36. private List<Record> processFile(String fileName)throws SystemException {
List<String> lines = readFile(fileName);
List<Record> recordList = new ArrayList();
for (String line : lines) {
Record record = parseLine(line);
recordList.add(record);
}
return recordList;
}
Java 1.7 generic + foreach
43. public static <E extends Comparable<? super E>>
List<E> quickSort(List<E> arr) {
if (!arr.isEmpty()) {
E pivot = arr.get(0); //This pivot can change
to get faster results
List<E> less = new LinkedList<E>();
List<E> pivotList = new LinkedList<E>();
List<E> more = new LinkedList<E>();
for (E i: arr) {
if (i.compareTo(pivot) < 0)
less.add(i);
else if (i.compareTo(pivot) > 0)
more.add(i);
else
pivotList.add(i);
}
less = quickSort(less);
more = quickSort(more);
less.addAll(pivotList);
less.addAll(more);
return less;
}
return arr;
}
http://rosettacode.org/wiki/Sorting_algorithms/Quicksort#Java
Java 7
49. SELECT * FROM BOOK
WHERE BOOK.PUBLISHED_IN = 2011
ORDER BY BOOK.TITLE
jOOQ
create.selectFrom(BOOK)
.where(BOOK.PUBLISHED_IN.eq(2011))
.orderBy(BOOK.TITLE)
http://www.jooq.org/
52. interface DefaultInterfaceTest{
void show();
default void display(){
System.out.println("Default method from interface can have body..!");
}
}
public class DefaultInterfaceTestImpl implements DefaultInterfaceTest{
public void show(){
System.out.println("show method");
}
//we dont need to provide any implementation to default method.
public static void main(String[] args){
DefaultInterfaceTest obj = new DefaultInterfaceTestImpl();
obj.show();//out puts: show method
obj.display();//outputs : Default method from interface can have body..!
}
}
Default Method
59. interface CustomerRepository extends Repository<Customer, Long> {
@Query("select c from Customer c")
Stream<Customer> streamAllCustomers();
}
try (Stream<Customer> customers = repository.streamAllCustomers()) {
// use the stream here
}
Support for JDK 8' Stream in repository methods
60. interface CustomerRepository extends Repository<Customer, Long> {
@Async
CompletableFuture<List<Customer>> readAllBy();
}
CompletableFuture<List<Customer>> future =
repository.readAllBy().thenApply(this::doSomethingWithCustomers);
while (!future.isDone()) {
log.info("Waiting for the CompletableFuture to finish...");
TimeUnit.MILLISECONDS.sleep(500);
}
List<Customer> processedCustomers = future.get();
61. http://doc.akka.io/docs/akka/2.3.0-RC1/java/lambda-actors.html
import akka.actor.AbstractActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import akka.japi.pf.ReceiveBuilder;
import scala.PartialFunction;
import scala.runtime.BoxedUnit;
public class MyActor extends AbstractActor {
private final LoggingAdapter log = Logging.getLogger(context().system(), this);
@Override
public PartialFunction<Object, BoxedUnit> receive() {
return ReceiveBuilder.
match(String.class, s -> s.equals("test"), s -> log.info("received test")).
matchAny(o -> log.info("received unknown message")).build();
}
}