Skip to main content

Coming Soon.........JAVA 8

Java 8 may be the most anticipated version of Java ever. Originally slated for release in September, Java 8 has been delayed until March of next year, supposedly to buy time to make security fixesaimed mainly at client-side Java (JavaFX/Swing). Since I, like most of you, stopped caring about client-side Java shortly after Duke finally finished jumping rope, we won't address any of that.
Java 8 is trying to "innovate," according to the Microsoft meaning of the word. This means stealing a lot of things that have typically been handled by other frameworks and languages, then incorporating them into the language or runtime (aka standardization). Ahead of the next release, the Java community is talking about Project Lambda, streams, functional interfaces, and all sorts of other goodies. So let's dive into what's great -- and what we can hate.
Streams
The changes to collections are driving a lot of the other changes in the language. The main component of making those collections better is a new collection called a "stream." This is not to be confused with java.io package's InputStream and OutputStream -- it's a separate concept altogether.
Streams are not meant to replace ArrayLists or other collections. They are only meant to make manipulating the data easier and faster. A stream is a one-time-use Object. Once it has been traversed, it cannot be traversed again.
Streams have the ability to filter, map, and reduce while being traversed. There are two "modes" for a stream: sequential and parallel. This is where the ability of the stream to use the multicore processors of today comes into play. It uses fork/join parallelism to split up the work and speed the processing along.
Using a sequential stream:
List <Person> people = list.getStream.collect(Collectors.toList());
Using a parallel stream:
List <Person> people = list.getStream.parallel().collect(Collectors.toList());
When the stream is traversed sequentially, each item in the stream is read processed, then the next item is read. When the stream is traversed in parallel, the array is split into multiple segments, each of which is processed individually on a different thread. The results are then put back together for the output.
Parallel stream flow:
List originalList = someData;
split1 = originalList(0, mid);
split2 = originalList(mid,end);
new Runnable(split1.process());
new Runnable(split2.process());
List revisedList = split1 + split2;
This very simplified example shows how the parallel Stream would be processed. This is how it will take advantage of multicore processors.
Since a Stream can only be traversed once and generally returns another Stream, use a terminal method to get a useful result. Examples of a terminal method are sum()collect(), or toArray(). Until the Stream is terminated, the results of the operations are not realized. For example:
Double result = list.getStream().mapToDouble(f -> f.getAmount()).sum();
List<Person> people = list.getStream().filter(f -> f.getAge() > 21).collect(Collectors.toList());
The big benefit of this feature is the ability to use multiple processor cores for collection processing. Instead of doing the traditional for loop, use Stream in parallel mode -- theoretically, the speed goes up with each core added. The main problem that could arise from this is readability. With all of the chaining of streams, the lines could get long, which will affect readability. The other problems stem from the things that are built to support this new path. Those are functional Interfaces and Lambda.
Functional interfaces
Java 8 will have a new feature called functional interfaces. Basically, default methods are added to an interface and do not have to be overridden in the interface implementation. These methods can be run directly from the interface.
This was done for backward compatibility for your collections in your interfaces. It is to solve the problem of allowing theStream to be put into an interface without having to change all of the classes to implement the new method. Basically, create a default method in the interface, and all the classes that implement the interface can use the Stream (or whatever is put into the default method). If the default method is not correct for the implementation, it can be overridden in the implementer.
What this essentially does is allow a form of multiple inheritance. This becomes the implementer's problem, as the implementer will be required to override the method anyway. The implementer could then choose which supermethod to use, but this means a lot of classes that implement interfaces could still be changed.
This is probably the detail that will concern most people in Java 8. Perhaps it won't bother those already familiar with Scala. It can be compared directly with the concept of traits in Scala; although the concept is not unique to Scala, it is best known to the Java world from Scala. However, there are some differences: Java 8 functional interfaces can not get a reference to the implementing class. Scala allows this with the self keyword. Language nerds will say that Java 8's functional interfaces allow multiple inheritance of behavior, but not state. Scala's traits are multiple inheritance for both behavior and state.
Consider the power of traits and the stuff we do to work around not having them in Java. In Java to implement transactions and other items, we construct dynamic proxies and do bytecode manipulation with JavaAssist or extend classes with cglib. Traits give us ways to do this more directly in some cases.
On one hand, functional interfaces will probably be misused the way that inheritance is misused. On the other hand, they don't go as far as Scala's traits. We'll still be stuck with some level of bit-twiddling class-loader implementation via Aspect Oriented Programming-type notes in Java in places where traits could probably stand in.
LambdaLambda expressions are coming to Java in version 8. Lambda expressions are designed to allow code to be streamlined. When a Lambda expression is written, it is translated into a functional interface at compile time. Here is an example of using Lambda expressions to replace an anonymous inner class with much cleaner and more readable code.

Comments

Popular posts from this blog

DBA_SCHEDULER_JOB_RUN_DETAILS and PURGE_LOG

How to purge DBA_SCHEDULER_JOB_RUN_DETAILS? Manually deleting from DBA_SCHEDULER_JOB_RUN_DETAILS is not recommended by oracle.DBA_SCHEDULER_JOB_RUN_DETAILS is a view that is using two master tables (scheduler$_job_run_details and scheduler$_event_log) and display the information about jobs history. As there is one procedure named PURGE_LOG and Oracle have Scheduler for this procedure. It will purges all rows in the job log that are older than 30 days.This is the default behavior of this procedure. You can change this to any number of days you want by setting the attribute "SET_SCHEDULER_ATTRIBUTE". e.g. exec DBMS_SCHEDULER.SET_SCHEDULER_ATTRIBUTE('log_history','15'); It will purge all logs older than 15days and it will maintain the history of 15days. But If you want manually purge these logs, you can use below solution:- exec DBMS_SCHEDULER.PURGE_LOG(log_history => 15, which_log => 'JOB_LOG'); It will purge all entries from the jog log that are o...

ORA-65016: FILE_NAME_CONVERT must be specified

ORA-65016: FILE_NAME_CONVERT must be specified I just started working on 12C version of oracle database.It's new to me and facing many problems. Error code: ORA-65016: FILE_NAME_CONVERT must be specified Description:"ORA-65016: FILE_NAME_CONVERT must be specified" normally occurs when you create a PDB.I will explain later what is a PDB. Cause and solution :  ORA-65016: FILE_NAME_CONVERT must be specified caused when Data files, and possibly other files, needed to be copied as a part of creating a pluggable database.Enable OMF or define PDB_FILE_NAME_CONVERT system parameter before issuing CREATE PLUGGABLE DATABASE statement, or specify FILE_NAME_CONVERT clause as a part of the statement and make sure the path you are giving to convert the file exists. I think if you are creating the PDB's using GUI then you will not face this error "ORA-65016: FILE_NAME_CONVERT must be specified". If you creating ODB using script and you have gave a wrong path then may you f...

ORA-02051 Another Session Or Branch In Same Transaction Failed

ORA-02051 Another Session Or Branch In Same Transaction Failed (Doc ID 2253226.1)          SYMPTOMS for ORA-02051 Another Session Or Branch In Same Transaction Failed. Database performance is slow and caused   the transactions ORA-02051 another session or branch in same transaction failed or finalized CAUSE for ORA-02051 Another Session Or Branch In Same Transaction Failed. Session transactions branches caused the issue Excessive Waits On The Event "Global transaction acquire instance locks" SOLUTION Please use below sql and identified underscore parameter values for ORA-02051 Another Session Or Branch In Same Transaction Failed : SQL> select a.ksppinm "Parameter", b.ksppstvl "Session Value",c.ksppstvl "Instance Value"  FROM x$ksppi a,x$ksppcv b, x$ksppsv c  WHERE a.indx = b.indx AND a.indx = c.indx AND a.ksppinm LIKE '/_%' escape '/'  AND (a.ksppinm like '%clusterwide_global%' or a.ksppinm like '%disable_autotune_...