Why Java 8 Date Time API needed ?1. Prior to Java8 we have SimpleDateFormatter and java.util.Date and both are not Thread safe.Every application now a days requires concurrency and multithreading support.So slowly application developers started preferring 3rd party library JodaTime over java.util.Date package.This was a concern for Java Team and they come up with Java Date Time API which is thread safe.2. Prior to Java 8 some of the date and time classes has poor API design in terms of date range.For example,default years in java.util.Date start at 1900,default months start at 1, and default days start at 0, so not very good.3. Also Developers need to code a lot to handle TimeZone due to poor API design.

1. In java an enum type is a special data type which enables a variable to hold a set of predefined constants.It means basically enum is a datatype which can hold a set of constants.2. Directions(EAST,WEST,NORTH,SOUTH) is an example of enum or DAY(SUN,MON,TUE,WED,THU,FRI,SAT) is an example of enum.3. enum can be traversed using values() method.4. enum can have constructor,fields and also user defined methods.5. enum internally extends enum class ,so it can not extend any other class .enum can implement multiple interfaces.

1

2

3

publicabstractclassEnum<EextendsEnum<E>>

extendsObject

implementsComparable<E>,Serializable

6. enum can be used in switch statements,enum is final so we can use for Singleton,enum ensures type safety as well.These are some of the reasons why we use enum.

7. You can use valueOf(String arg) to get a particular enum and you can use valueOf(Class<T> arg, String arg1) to get a particular enum of a class Type.8. enum was introduced in Java 5 version jdk 1.5

1. java.util.regex API is used for pattern matching using regular expressions2.java.util.regex.Pattern and java.util.regex.Matcher are the two main classed in java.util.regex API.3. class java.util.regex.Pattern extends Object class and implements Serializable class.4. A regular expression, specified as a string, must first be compiled into an instance of Pattern class. The resulting pattern can then be used to create a Matcher object that can match arbitrary character sequences against the regular expression. All of the state involved in performing a match resides in the matcher, so many matchers can share the same pattern.

In this post we are going to code forEach() and forEachOrdered() examples. forEach() method is used to iterate over a collection like List,Set,Map etc or a stream.forEachOrdered() method process the elements in the order the elements are added to collection.

Syntax ::

1

2

3

// * Performs an action for each element of this stream.

voidforEach(Consumer<?superT>action);

1

2

3

// Performs an action for each element of this stream, in the encounter

// * order of the stream if the stream has a defined encounter order.

voidforEachOrdered(Consumer<?superT>action);

Program ::
In the below Program I have used three methods to use forEach() over List,Set and Map.I have used the same method to show forEachOrdered() on List and Set using stream.

Abstract Factory Design Pattern ::Abstract Factory design pattern is also called as factory of factories. This type of design pattern comes under creational design pattern as this pattern provides way to select a factory and then create an instance of related classes of that factory.

Real Time Example :: I am going to have two factories where one factory will have VideoPlayer and its implementation classes and another factory will have AudioPlayer and and its implementation classes.Based on user input a factory will be selected and then an instance will be created of related classed of that factory.

Observer Design Pattern :: Observer Design Pattern comes under behvioral design pattern.In this design pattern an observer is notified if any changes occur to an object which the observer is subscribed to.

Real Time Example :: Suppose there are serveral readers.One category of readers like to read Java Topics and other set of readers like to read Spring Topics .So these readers(Observers) first subscribes to corresponding SubjectTopic(Subjects).Once any changes to Subject, new articles added to topic and any subscriber is added/removed then if reader/subscriber opts for notification he/she can get the notification about the same.

Step 2 :
Create an Interface Observer.java and declare methods updateObserver() .
updateObserver() method updates an Observer/Subscriber if any changes happen to Subject.
I have also optionally added getName() method to get details about Observer/Subscriber.

1

2

3

4

5

6

7

8

9

packagecom.ai1tutorial.design.patterns;

packagecom.ai1tutorial.design.patterns.observer;

publicinterfaceObserver{

publicvoidupdateObserver();

publicStringgetName();// Optionally added to print observer details

}

Step 3 :
Create a class TopicEnrollment.java which implements interface Subject.java and has its own implementation for methods subscribeObsever(Observer observer),unSubscribeObsever(Observer observer) and notifyAllObsevers() .

Step 4 :
Create a class Subsrciber.java which implements interface Observer.java and has its own implementation for methods updateObserver()nd getName()
I also have an instance of Subject here ,so that a Subscriber can subscribe to corresponding Subject.I am doing the same in the constructor.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

packagecom.ai1tutorial.design.patterns.observer;

publicclassSubsrciberimplementsObserver{

privateSubject subject;

privateStringname;

publicSubsrciber(Stringname,Subject subject){

super();

this.name=name;

this.subject=subject;

this.subject.subscribeObsever(this);

}

publicStringgetName(){

returnthis.name;

}

publicvoidupdateObserver(){

System.out.println("Subscriber "+this.name+" is notified");

}

}

Step 5 :
Create main class ObserverDesignPatternDemo.java.I have created two topics “java-8” and “Spring-5” to demo Observer design pattern.

Factory Design Pattern :: Factory Design Pattern comes under creation design pattern.As the name suggests,basically using these design pattern you can create an instance of class from a factory of related classes.

Real Time Example :: I am going to have a factory of MediaPlayer classes and each class has the functionality to play song.Based on user input ,user will select a particular type media player and will play song using that media player only.

Post navigation

About the Author

Dear Friends,
I am A.K.M. .This online journal ai1tutorial.com is a humble effort from my side to all students and professionals who want to learn computer science related technologies.I am not a professional blogger and I write posts in this blog only in my leisure time.I may not be 100% perfect in all my posts,but your review comments can surely help me to make it 100% perfect.