29
20 years of Java & Simplified PARALLELISM - Java8

Java 8-streams-and-parallelism

Embed Size (px)

Citation preview

Page 1: Java 8-streams-and-parallelism

20 years of Java&

Simplified PARALLELISM - Java8

Page 2: Java 8-streams-and-parallelism

AGENDA

20 Years of JAVA

✣ Prehistory - the WEB✣ Birth of OAK or JAVA✣ Evolution till date

++++++++++++++++

Fun Quiz

Java 8 - Streams API and Parallelism

✣ Lambdas✣ Streams✣ Parallel Streams

++++++++++++++++

Demo Q&A

Slides will be uploaded at http://www.slideshare.net/dshevani

Page 3: Java 8-streams-and-parallelism

Hello everyone !

I am Deepak ShevaniI work at Flipkart, building next generation e-commerce platforms.

I like presenting & love to mingle with technology enthusiasts.

Contact me : [email protected]

@deepak_shevani

Page 4: Java 8-streams-and-parallelism

1.Evolution of JAVA

(1995-2015)Community of 9 million developers

Powering 7 billion devices

Page 5: Java 8-streams-and-parallelism

Prehistory

✣ [1991] ■ World Wide Web initially released by CERN for physics community■ Project Green started at Sun to explore opportunities in consumer electronics■ New Wave was about convergence of interconnected computers and devices■ James Gosling was exploring programming languages to suit requirements

✣ [1992]■ OAK programming language, descendant of C++ but platform independent■ Star7 interactive hand-held PDA introduced by Project Green

✣ [1993]■ World Wide Web is born at CERN, put on public domain■ 500 known web servers around the world

✣ [1994]■ Browsers : Netscape Navigator released, WebRunner (HotJava)* demoed■ 10,000 known web servers and 10 million web users■ Java compiler re-written in Java rather in C++

Page 6: Java 8-streams-and-parallelism

Birth OF JAVA [1995]

✣ 16 million internet users worldwide✣ Birth of e-Commerce companies

■ __a___ and __a_✣ Birth of internet companies

■ _a___✣ Demo of WebRunner at TED conference✣ Debut of

■ Java 1.0a2 ■ Hotjava

H A P P Y B I R T H D A Y J A V A

Page 7: Java 8-streams-and-parallelism

Birth OF JAVA [1995]

✣ 16 million internet users worldwide✣ Birth of e-Commerce companies

■ Amazon and eBay✣ Birth of internet companies

■ Yahoo✣ Demo of WebRunner demo at TED✣ Debut of

■ Java 1.0a2 ■ Hotjava

H A P P Y B I R T H D A Y J A V A

Page 8: Java 8-streams-and-parallelism

Evolution OF JAVA [1996-2000]

✣ [1996] ■ First JavaOne developer conference held in SFO and attracts 6000 people■ Java 1.0 available for download■ Sun licenses Java to OS vendors including MS, Apple, IBM and others

✣ [1997]■ JDK 1.1 released, included JavaBeans, RMI, JIT, Inner Classse, JDBC support■ Sun sues Microsoft over use of Java and distorting it :)■ 43% of US families own a computer

✣ [1998]■ Google :) Apple iMac :)■ Netscape Navigator acquired by AOL for whopping $4.2 billion■ JDK 1.2 released, included Swing, Plugin, IDL, Collections, strictfp

✣ [1999]■ Sun and AOL announce a joint alliance to market business software■ Sun announces Java 2 Platform, J2ME, J2SE and J2EE

✣ [2000]■ DotCom bubble bursts■ JDK 1.3 released, included HotSpot, RMI/Corba, Java Sound

Page 9: Java 8-streams-and-parallelism

Evolution OF JAVA [2001-2005]

✣ [2001] ■ Google awarded patent for pagerank algorithm■ Launch of Eclipse Project by IBM ■ Apple iPod :)

✣ [2002]■ JDK 1.4 released, included JCP, WebStart, NIO, Logging, assertions■ J2EE SDK downloads reach 2 million

✣ [2003]■ Java’s new coffee logo debuts■ Java.com launches

✣ [2004]■ Java technology powered Mars Rover (Spirit) touches Mars■ Java is already on 1.5 billion devices■ Java 2 Platform Standard Edition J2SE 5.0 (Project Tiger) released■ Included Generics, AutoBoxing/UnBoxing, Concurrency Utilities, enums

✣ [2005]■ Google Maps launched■ Sun launches Glassfish product

Page 10: Java 8-streams-and-parallelism

Evolution OF JAVA [2006-2010]

✣ [2006] ■ Java SE 6 released (no longer branded as J2SE) (Project Mustang)■ Included Scripting, XML and Web Services■ Sun releases Java Platforms on Oct 25 under the GNU General Public License

:)✣ [2007]

■ Apple iPhone :)■ Sun releases Duke, the Java mascot, under the free BSD license

✣ [2008]■ Google introduces the Android operating system for mobile phones.■ Facebook reaches 100 million users.■ Twitter.com has more than 70 million unique visitors per month.■ Sun slowing down ??

✣ [2009]■ Oracle announces that it will acquire Sun Microsystems at JavaOne■ Project Coin launched to enhance the Java programming language

✣ [2010]■ Oracle acquires Sun Microsystems■ JCP approves Java 7 and Java 8 roadmaps

Page 11: Java 8-streams-and-parallelism

Evolution OF JAVA [2011-2015]

✣ [2011] ■ Java SE 7 released (Project Dolphin) on July 28, 2011■ Features

○ Strings in Switch statement○ Type Inference for Generic Instance Creation○ Multiple Exception Handling○ Diamond Syntax○ Automatic null Handling○ ForkJoin, NIO2○ etc

✣ [2014]■ Java SE 8 released (No Project code !! ) on March 18, 2014■ Features

○ Lambda Expressions, Streams○ Pipelines and Streams API○ Default Methods○ Nashorn Javascript engine○ Parallel operations○ etc

Page 12: Java 8-streams-and-parallelism

QUIZ TIME

1) When did Java go open-source ? (date-year)

Page 13: Java 8-streams-and-parallelism

QUIZ TIME

1) When did Java go open-source ? (date-year)

Oct 25, 2006

Page 14: Java 8-streams-and-parallelism

QUIZ TIME

1) When did Java go open-source ? (date-year)

Oct 25, 2006

2) When was internet first put on public domain ?

Page 15: Java 8-streams-and-parallelism

QUIZ TIME

1) When did Java go open-source ? (date-year)

Oct 25, 2006

2) When was internet first put on public domain ?

1993

Page 16: Java 8-streams-and-parallelism

QUIZ TIME

1) When did Java go open-source ? (date-year)

Oct 25, 2006

2) When was internet first put on public domain ?

1993

3) When was Java Coffee logo introduced ?

Page 17: Java 8-streams-and-parallelism

QUIZ TIME

1) When did Java go open-source ? (date-year)

Oct 25, 2006

2) When was internet first put on public domain ?

1993

3) When was Java Coffee logo introduced ?

2003

GREAT - Let’s move on :)

Page 18: Java 8-streams-and-parallelism

Parallel STREAMSNo more boilerplate code !!

Page 19: Java 8-streams-and-parallelism

Basic Jargon FIRST

LambdasA lambda is anonymous function. A function is a computation that may take some arguments and may return some value. Lambdas enable functions to passed around and stored like values (smells like functional programming, yeah)

Syntax of lambda expression

( [optional arguments] ) -> body

( Person p1, Person p2 ) -> p1.name() == p2.name()

**** Types can be inferred (parameters, return type)

StreamsA Stream is an abstract concept that represents multiple values. It may or may not be collection. It may or may not be a sequence. It may or may not be an iterator. It is something that would allow sequential as well as parallel processing :)

Stream<T> is opened using

Collections.stream(), Collection.parallelStream()

IntStream.range(), Stream.of(), Arrays.stream()

BufferedReader.lines(), CharSequence.chars()

Page 20: Java 8-streams-and-parallelism

E X A M P L E S H O U L D M A K E I T C L E A R

SomeList<Employee> employees = ….

double highestPaid =

employees.stream()

.filter(e -> e.getDepartment().equals(‘Search’)

.map(e -> e.getSalary())

.max();

Page 21: Java 8-streams-and-parallelism

I T E R A T I O N S

// EXTERNAL

for (Item items : items) {

process(item);

}

// INTERNAL

items.forEach(item -> process(item));

Page 22: Java 8-streams-and-parallelism

S T R E A M O F B E N E F I T S

- LAZY EVALUATION- SHORT CIRCUITING- AUTOMATIC PARALLELISM

Page 23: Java 8-streams-and-parallelism

DEMO TIME

Page 24: Java 8-streams-and-parallelism

QuestionFind all transactions of type mobiles and return a

list of transaction IDs sorted in decreasing order of transaction value.

Page 25: Java 8-streams-and-parallelism

W I T H O U T S T R E A M S ( Java 7)

List mobileTransactions = new Arraylist<>();

for(Transaction t: transactions){

if(t.getType() == Transaction.MOBILE){

mobileTransactions.add(t);

}

}

Collections.sort(mobileTransactions, new MobilesComparator()); List transactionIds = new ArrayList<>();

for(Transaction t: mobileTransactions){

transactionsIds.add(t.getId());

}

Page 26: Java 8-streams-and-parallelism

U S I N G S T R E A M S ( Java 8)

List transactionsIds = transactions.stream()

.filter(t -> t.getType() == Transaction.MOBILE)

.sorted(comparing(Transaction::getValue)

.reversed())

.map(Transaction::getId)

.collect(toList());

Page 27: Java 8-streams-and-parallelism

U S I N G P A R A L L E L S T R E A M S

List transactionsIds = transactions.parallelStream()

.filter(t -> t.getType() == Transaction.MOBILE)

.sorted(comparing(Transaction::getValue)

.reversed())

.map(Transaction::getId)

.collect(toList());

Page 28: Java 8-streams-and-parallelism

Credits

Special thanks to all the people who made and released these awesome resources for free:

✣ Presentation template by SlidesCarnival✣ Photographs by Unsplash✣ Paper texture by GraphicBurguer

Page 29: Java 8-streams-and-parallelism

That’s IT FOLKSThanks for being

SUCH a WONDERFUL AUDIENCE