Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

How to resolve the sun.security.validator.ValidatorException: PKIX path building failed



When you first setup Maven in your office/home computer you might come up with an exception as below when you try to run/build the Maven project.


Could not transfer artifact com.adobe.aem:aem-api:pom:6.0.0.1 from/to adobe-public-releases (https://repo.adobe.com/nexus/content/groups/public): sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target -> [Help 1]

SOLUTION to sun.security.validator.ValidatorException: PKIX path building failed

So for the exception above we can see that the URL is clearly mentioned in the exception report. In this case it is https://repo.adobe.com/nexus/content/groups/public


Resolution Steps:


STEP 1:  Navigate to the URL in the exception report using a browser. In this case I am using Google Chrome browser. See below snapshot.



STEP 2: Now press F12 or right click on the page and click on “Inspect”. This will open the developer tools.


STEP 3: Go to the “Security” tab.

STEP 4: Click on the “View Certificate” button.


STEP 5: Check the name of the Certificate (or the issuing authority)



STEP 6: Now close this window and go to browser “Settings”.


STEP 7: Now search for “Manage Certificates”. Alternatively, you can open the “Internet Options” in IE or similar browser and go to “Content” tab and click on “Certificates” button.




STEP 8: Now find the name of the certificate matching the one in STEP 5.




STEP 9: Click on “Export” and export it in a new file (using the default selected format).



STEP 10: Now run the JAVA  Keytool command to add this certificate to your computer’s JDK keystore.


STEP 11: You should run this command.

keytool -import -alias ctsrootca3 -file D:\certs\ctsrootca3.cer -keystore "C:\Program Files\Java\jdk1.8.0_152\jre\lib\security\cacerts"

Please make changes as below.
D:\certs\ctsrootca3.cer  - This should be the path to your certificate file that you exported.
C:\Program Files\Java\jdk1.8.0_152 – This should be path to your JDK folder.
 
If prompted for a password use : “changeit” (without the quotes, this is the default password for cacerts file)



STEP 12: After successful addition of the key you will see a message like this.



STEP 13: If you have followed this steps correctly then now when you run Maven it should not give any errors.



How to resolve ObjectFactory Collisions in JAVA

In my previous article I have explained how to generate the web service classes.If you have not already read the topic then I would advise you to do so. Moving on, we can sometimes get an error like below

[ERROR] Two declarations cause a collision in the ObjectFactory cl
  line 1191 of file:/D:/RAMEEZ/Web%20Services/TeamBpl/TeamBpl3.xsd

[ERROR] (Related to above error) This is the other declaration.
  line 981 of file:/D:/RAMEEZ/Web%20Services/TeamBpl/TeamBpl3.xsd

This is a very common issue we face, and the way to resolve this is to simply write a XML file having a JAXB binding configuration.

Simple Steps for Clear Understanding:

Here are a few simple steps with images to help you fix the issue quickly :)


STEP 1:

Analyze the issue.
From the above error log we see line number 1191 and 981 have issues in the
XSD Schema file. Lets look into the XSD file. See below.



STEP 2:

So this is happening because the "name" attribute is somehow causing collision when the WSDL file is getting parsed.


STEP 3:

To solve this simple open a new file in Notepad(or any text editor you use) and name it something like "conflict.xml".

STEP 4:

Copy it from here.

<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">

 <jxb:bindings node="/xs:schema" schemalocation="TeamBpl3.xsd">
  <jxb:bindings schemalocation="TeamBpl3.xsd">
   <jxb:bindings node="./xs:element[@name='MediaUrl']">
    <jxb:factorymethod name="RootMediaElement">
   </jxb:factorymethod></jxb:bindings>
  </jxb:bindings>
 </jxb:bindings> 


</jxb:bindings> 



STEP 5:

Now run the "wsimport" command with the -b switch. This is called the binding (-b).

wsimport [WSDL_FILE.wsdl] -b conflict.xml -keep -verbose

STEP 6:

This will generate the stub classes properly without any issues.




REMEMBER:

When you have multiple collisions then in that case you have to write multiple jxb:bindings for each. Means you have to repeat line number 5 to 11 from STEP 3.

Hope this helps!



Steps to Create Web Service Stub

This document gives instructions on creating web service stubs better knowas proxy classes. We will take an example and proceed with the explanation.

STEP 1:

Copy the URL link in the browser address bar and hit enter.Check if the WSDL is accessible through your network or not.

STEP 2:

Download the WSDL file in a folder and should be saved with the ".wsdl" extension.

STEP 3:

Now start to download the artifacts in a folder in your local drive.This should include the WSDL file and all the associated XSDs. The XSD files can be downloaded from the links found inside the downloaded WSDL file.

STEP 4:

Go into the WSDL file.You will see a section having the schemas.

STEP 5:

Rename the XSD Http links.Point it to the local path of the XML Schemas.

STEP 6:

Open a command prompt and check the "wsimport" tool.

STEP 7:

We will use the wsimport and the associated commands to create the stubs.Please check the snap shot below f the command prompt.The command is like:wsimport -keep -extensionThere are many associated commands. We need to use them as and when required.

STEP 8:

After the previous command we can see folders created in the same destination of the artifact files.

STEP 9:

Copy this folders in the “src” folder of your JAVA project in your IDE.It should look like this ad you are good to go.

 

Java DecimalFormat - For Large Decimal Point Numbers

The Java DecimalFormat class helps us to format numbers in many ways. I was trying to convert
a big number to a 2 decimal point number.Thought of sharing the same with some explaination.

Trying to achieve this:    1,308,207 ---> 1.31


    String number = "1,308,207";
    java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
    number = number.replace(",", ""); 
    Float f = Float.parseFloat(number) / 1000000;
    String result = df.format(f); 
    System.out.println(result);   // result = 1.31

So how does it work??

Well it works like this. DecimalFromat uses java.math.RoundingMode.HALF_EVEN method which
rounds off the number to nearest even i.e when it's 0.05 it becomes 0.1.

1. The number becomes 1308207 at line 3.
2. At line no. 5 it becomes 1.308207.
3. Now the actual formatting starts:-
4. The right hand side(RHS) value is 07,so nearest round off is 10, only 1 taken & RHS discarded.
5. Now the number becomes 1.308201
6. Rightmost 01,the nearest round off id 00, RHS discarded & number becomes 0.
7. Again number becomes smaller 1.30820, in same way becomes 1.3082.
8. Next step : 1.308
9. Finally 1.31(08 becomes 10 with HALF_EVEN method and 0 discarded).


Do you have suggestions or want to improve the solution? Please do feel free to write back to us

Remove SOAP Request XML parameter programmatically.

SOAP is a protocol that allows systems to communicate using HTTP and uses predominantly XML
for communication. The necessity of web applications to inter communicate made it necessary for
a protocol to be designed.

Sometimes when setting the web service parameters there are compulsory parameters whose values needs
to be set. But passing these parameter might cause the Web Service to fail. So a situation may arise
where we need to both set the web service parameter value as well as remove the parameter from the
request XML!!!

So lets say for example you have a webservice parameter like this:

<ProductName>CoderMagnet<ProductName>
<CodeValue>...</CodeValue>
<ProductId>ABCXYZ<ProductId>


Suppose passing this value in the request xml is giving an error.
And also lets assume this is being set from the JAVA program like this.


product.setProductName("CoderMagnet");
product.setCodeValue("ABC123");
product.setProductId("004");



Now to remove this parameter from the web service request XML we just need to set
the parameter as NULL. This will remove the web service parameter from the request XML.


product.setProductName("CoderMagnet");
product.setCodeValue(null);
product.setProductId("004");



Afer you set a web service parameter to null. It will automatically
be dropped like shown below.


<ProductName>CoderMagnet<ProductName>
<ProductId>004<ProductId>



We can see from the above XML that the CodeValue got dropped.




How to iterate over a HashMap in Java

Iterating over a HashMap in Java is a very common task but can be tricky sometimes
since it uses the Entry object (java.util.Map.Entry) which we may not be familiar with readily.

Whenever we need to "Iterate over a HashMap" we need to call the entrySet() method over the map
object and then only we can iterate over it.


This is best achieved in the following two ways.
Supposing we have a Hashmap like like this.


Map<String,String> timeZones=new HashMap<String, String>();
 
timeZones.put(" (GMT-12:00) ", " International Date Line West ");
timeZones.put(" (GMT-11:00) ", " Midway Island ");
timeZones.put(" (GMT-10:00) ", " Hawaii Time ");
timeZones.put(" (GMT-09:00) ", " Alaska Time ");
timeZones.put(" (GMT-08:00) ", " Pacific Time (US and Canada) ");


Method 1: Using For Each Loop to iterate over a HashMap

for(Map.Entry timeZone : timeZones.entrySet()){

    System.out.println(timeZone.getKey() + " is the " + timeZone.getValue());

  }


Method 2: Using Iterator and While Loop to iterate over a HashMap

 while (it.hasNext()) {
  
  Entry timeZone = (Entry)it.next();
  System.out.println(timeZone.getKey() + " is the " + timeZone.getValue());
  it.remove(); 
    }


Output:

 (GMT-08:00)  is the  Pacific Time (US and Canada) 
 (GMT-12:00)  is the  International Date Line West 
 (GMT-09:00)  is the  Alaska Time 
 (GMT-10:00)  is the  Hawaii Time 
 (GMT-11:00)  is the  Midway Island


Well in both cases the result is he same. Its depends on the taste and the situation
to decide which one of the method you want to use.
Going a little deeper we can look into other requirements of HashMap iteration.





Iterate over a HashMap Values/ How to iterate over the values in a HashMap.

Well you may very well come across a situation where you only need to iterate over HashMap values.
This can be easily achieved in the following two ways.

 for(String timeZone : timeZones.values()){

  System.out.println(timeZone);
 }


OR,


Iterator it = timeZones.values().iterator();

 while (it.hasNext()) {
  String timeZone = (String) it.next();
  System.out.println(timeZone);
  it.remove();
 }


Output:

 Pacific Time (US and Canada) 
 International Date Line West 
 Alaska Time 
 Hawaii Time 
 Midway Island 






Iterate over a HashMap Keys/ How to iterate over the Keys in a HashMap.

You may also need to iterate over HashMap keys. This HashMap iteration is shown below.

 for (String timeZone : timeZones.keySet()) {

  System.out.println(timeZone);
 }


OR,


Iterator it = timeZones.keySet().iterator();

 while (it.hasNext()) {
  String timeZone = (String) it.next();
  System.out.println(timeZone);
  it.remove();
 }


Output:

 (GMT-08:00) 
 (GMT-12:00) 
 (GMT-09:00) 
 (GMT-10:00) 
 (GMT-11:00) 


How to convert String to Int in Java

Converting a String to Int in Java is a common scenario which we face everyday.
Now this can be achieved in multiple ways.

Integer.parseInt()
Integer.parseInt() returns a primitive int datatype.

String num = "12345";
int number = 0;

try {  
number = Integer.parseInt(num);   
} 
catch (NumberFormatException e) {
}


Integer.valueOf()
The Integer.valueOf() method returns an cached instance of java.lang.Integer.

String num = "12345";
Integer number = 0;
try {
      number = Integer.valueOf(num);
    } 
catch (NumberFormatException e) {
}


Integer.decode()
This basically decodes a String into an Integer.It returns a wrapper object of type java.lang.Integer.
It accepts Strings in the formats like DecimalNumeral, HexDigits, and OctalDigits.
DecimalNumeral, HexDigits, and OctalDigits are as defined in section 3.10.1 of The Java™ Language
Specification, except that underscores are not accepted between digits.

String num = "12345";
Integer number = 0;
try {
      number = Integer.decode(num);
    } 
catch (NumberFormatException e) {
}


Integer.decode() for Octal numbers

String num = "022";
Integer number = 0;
try {
      number = Integer.decode(num); //18
    } 
catch (NumberFormatException e) {
}


Integer.decode() for Hexadecimal numbers

String num = "0X12A";
Integer number = 0;
try {
      number = Integer.decode(num); //298
    } 
catch (NumberFormatException e) {
}


Retrieve List from java.util.Stream in Java 8

In the previous article I explained the new class introduced in Java 8 called Optional.
Today I was working with streams and needed to convert it to a List. So thought of sharing this..

Go here if you need to Convert a List to Stream.
Stream API in Java 8 does not define a direct operation or standard to convert a stream data,
into a List. Java 8 design pattern unfortunately does not allow direct conversions from the Stream
to Collections like List or Set.

However not everything is lost, as the developers have given us the
concept of Collectors.You can read more about collectors in my Simple Collector Tutorial.
Using Collectors we can easily achieve inter conversions from Stream to Collections.


There are various method explaining how to convert Stream to List in Java 8.
Here are the most common ones.

The Collectors.toList() method


The Collectors.toList() method is the most straightforward method to convert Stream to List in java 8.
It involves some simple code.

Stream<String> streamData=Stream.of("Convert","Stream","to","List");
List<String> newList=streamData.collect(Collectors.toList());

System.out.println(newList);  //[Convert, Stream, to, List]

Most of the daily requirements will be addressed by the above method
but if in case you are the odd one out who just can't do thing as the normal
people do, then you might look at the below options as well.



The Collectors.toCollection() method to convert a Stream to List in Java8


This is quite same as the previous one except for the fact that it is more generalized
conversion. The toCollection() can be used to convert Stream to other collection class also
like for example a TreeSet.

Stream<String> streamData=Stream.of("Convert","Stream","to","List");
List<String> newList=streamData.collect(Collectors.toCollection(ArrayList::new ));
//TreeSet<String> set=streamData.collect(Collectors.toCollection(TreeSet::new ));

System.out.println(newList);  //[Convert, Stream, to, List]



Using forEach() to convert a Stream to List in Java 8


Ok so this is a crude way of converting a Stream to a List. But if the other options
are not good enough for you then you might try out this one. Here what we will traverse
through the elements of the Stream using the forEach() loop and add each element to the List.

Stream<String> streamData=Stream.of("Convert","Stream","to","List");
List<String> newList=new ArrayList<String>();
 
streamData.forEach(newList::add);
 
System.out.println(newList);  // [Convert, Stream, to, List]



Using forEachOrdered() to convert a Stream to ordered List in Java 8


The forEach() will work with Streams pretty good. But there is a catch.
In case of parallel streams where multiple Stream data is processed horizontally
it will break the ordering. So you will end up with a jumbled list of objects from the initial
Stream. The forEachOrdered() helps to traverse a parallel stream in an orderly way. So the
list you will get will maintain the initial order of elements as present in the parent Stream.

Stream<String> streamData=Stream.of("Convert","Stream","to","List");
List<String> newList=new ArrayList<String>();
 
streamData.forEachOrdered(newList::add);
 
System.out.println(newList);  // [Convert, Stream, to, List]




Using toArray() and Arrays.asList() to convert a Stream to List in Java 8


If you are really looking to lower efficiency, readability and clarity of our code
and you do not like the normal stuff that others do then you can try this out.
Jokes apart, this method can be used to learn the features of Stream and also to
understand the basics of Streams and Collections in Java 8.

Stream<String> streamData=Stream.of("Convert","Stream","to","List");
List<String> newList=(List<String>)(Object)Arrays.asList(streamData.toArray());

System.out.println(newList);  // [Convert, Stream, to, List]



Conclusion:

To conclude we can adopt various steps to convert Stream to List in Java 8.
Since there is no direct way to do this I leave it upto you to choose the best method
for your development activity.However I prefer the first two methods using Collectors.

SEE ALSO: Convert a List to Stream in Java 8.


Convert List<V> into Map<K, V> in Java 8

Sometimes it is necessary to convert a List of Objects to Map in Java 8.
In Java 8 we can simply do this using Collectors class .
Let us look at the following example.


class Employee {
 public String name;
 public int age;

 Employee(String name, int age) {
  this.name = name;
  this.age = age;
 }

 public String getName() {
  return name;
 }

Now we can convert a list of Employee Objects to a Map like this
We will use the names of the Employees as the key of the Map.


List<Employee> empList = Arrays.asList(
                            new Employee("Jim", 18),
                            new Employee("Joe", 25), 
                            new Employee("Harry", 40)
                            );
  
Map<String, Employee> result = empList.stream()
                    .collect(Collectors.toMap(Employee::getName, x -> x));

System.out.println(result);


Output:

{
Joe   = codermag.net.java8.Employee@404b9385, 
Harry = codermag.net.java8.Employee@6d311334, 
Jim   = codermag.net.java8.Employee@682a0b20
}

When to use Java 8 forEach() over Enhanced For loop

Well in Java 8 you have an option to call the forEach() method over any iterable.
This is simply iterable.forEach(action), where forEach() performs the given action for
each element of the Iterable until all elements have been processed or the action throws an exception.

In normal scenarios there is absolutely no noticeable difference in performance.
So an operation like 1 and 2 this will yield similar results.


List<String> items = Arrays.asList("Hello", "Java", "Developer","CoderMagnet");


Operation 1:
items.forEach(System.out::println);
Operation 2:

for (String item : items) 
{
   System.out.println(item);
}
Output (Same for Both)

Hello
Java
Developer
CoderMagnet



So where is the difference????

The main advantage of forEach() is observed when it is invoked over a Java8 parallel stream.Parallel
execution reduces processing time. But unlike the previous example the sequential order is not maintained.
Long story short, forEach() over parallel streams will execute much faster.



// Normal Stream (Slower, sequential order maintained)
items.stream().forEach(System.out :: print);

System.out.println("\n");

// Parallel Stream (Executes much faster, sequential order not maintained)
items.parallelStream().forEach(System.out :: print);
Output:

Hello Java Developer CoderMagnet 

Developer CoderMagnet Java Hello 


The Java8 forEach() should be used to boost performance in situations where parallel executions
can boost performance. But we should remember that parallel execution will not maintain the
sequential nature of data iteration.

How to convert a List<Object> to List

This is a tricky situation that you may end up with sometimes.
Directly typecasting like shown below doesn't work:

List<String> stringList=(List<String>)objList;


Typecasting List<Object> to List<String> but it does not work as typecasting generic types is
not allowed. So this can be achieved by converting a List<Object> to an intermediate.
We can acheive this conversion in the following ways.


Typecasting the List itself as a simple Object and then to its generic type.

List<Object> objList=Arrays.asList("Hello", "World","CoderMagnet");
List<String> stringList=(List<String>)(Object)objList;


You can typecast through an intermediate wildcard.

List<Object> objList=Arrays.asList("Hello", "World","CoderMagnet");
List<String> stringList=(List<String>)(List<?>)objList;




Convert a java.util.List to java.util.Stream in Java 8

In my previous example I showed "How to convert Stream into a List in Java 8".
In this example I will show how to convert a List into a Stream in Java 8.

Collections like the List can be easily obtained from a Java 8 Stream
by calling the stream() operation over a List.
This is a inbuilt feature and it helps in easy conversions from collections to Stream data.

List<String> itemList=Arrays.asList("List","to","Stream","Java8","Stream");

//Converting List to Stream in Java 8
Stream<String> itemStream=itemList.stream();

itemStream.distinct().forEach(x -> System.out.print(x+" "));


SEE ALSO: How to convert Stream into a List in Java 8

Convert Stream to List in Java8

In the previous article I explained the new class introduced in Java 8 called Optional.
Today I was working with streams and needed to convert it to a List. So thought of sharing this..

Go here if you need to Convert a List to Stream.
Stream API in Java 8 does not define a direct operation or standard to convert a stream data,
into a List. Java 8 design pattern unfortunately does not allow direct conversions from the Stream
to Collections like List or Set.

However not everything is lost, as the developers have given us the
concept of Collectors.You can read more about collectors in my Simple Collector Tutorial.
Using Collectors we can easily achieve inter conversions from Stream to Collections.


There are various method explaining how to convert Stream to List in Java 8.
Here are the most common ones.

The Collectors.toList() method


The Collectors.toList() method is the most straightforward method to convert Stream to List in java 8.
It involves some simple code.

Stream<String> streamData=Stream.of("Convert","Stream","to","List");
List<String> newList=streamData.collect(Collectors.toList());

System.out.println(newList);  //[Convert, Stream, to, List]

Most of the daily requirements will be addressed by the above method
but if in case you are the odd one out who just can't do thing as the normal
people do, then you might look at the below options as well.



The Collectors.toCollection() method to convert a Stream to List in Java8


This is quite same as the previous one except for the fact that it is more generalized
conversion. The toCollection() can be used to convert Stream to other collection class also
like for example a TreeSet.

Stream<String> streamData=Stream.of("Convert","Stream","to","List");
List<String> newList=streamData.collect(Collectors.toCollection(ArrayList::new ));
//TreeSet<String> set=streamData.collect(Collectors.toCollection(TreeSet::new ));

System.out.println(newList);  //[Convert, Stream, to, List]



Using forEach() to convert a Stream to List in Java 8


Ok so this is a crude way of converting a Stream to a List. But if the other options
are not good enough for you then you might try out this one. Here what we will traverse
through the elements of the Stream using the forEach() loop and add each element to the List.

Stream<String> streamData=Stream.of("Convert","Stream","to","List");
List<String> newList=new ArrayList<String>();
 
streamData.forEach(newList::add);
 
System.out.println(newList);  // [Convert, Stream, to, List]



Using forEachOrdered() to convert a Stream to ordered List in Java 8


The forEach() will work with Streams pretty good. But there is a catch.
In case of parallel streams where multiple Stream data is processed horizontally
it will break the ordering. So you will end up with a jumbled list of objects from the initial
Stream. The forEachOrdered() helps to traverse a parallel stream in an orderly way. So the
list you will get will maintain the initial order of elements as present in the parent Stream.

Stream<String> streamData=Stream.of("Convert","Stream","to","List");
List<String> newList=new ArrayList<String>();
 
streamData.forEachOrdered(newList::add);
 
System.out.println(newList);  // [Convert, Stream, to, List]




Using toArray() and Arrays.asList() to convert a Stream to List in Java 8


If you are really looking to lower efficiency, readability and clarity of our code
and you do not like the normal stuff that others do then you can try this out.
Jokes apart, this method can be used to learn the features of Stream and also to
understand the basics of Streams and Collections in Java 8.

Stream<String> streamData=Stream.of("Convert","Stream","to","List");
List<String> newList=(List<String>)(Object)Arrays.asList(streamData.toArray());

System.out.println(newList);  // [Convert, Stream, to, List]



Conclusion:

To conclude we can adopt various steps to convert Stream to List in Java 8.
Since there is no direct way to do this I leave it upto you to choose the best method
for your development activity.However I prefer the first two methods using Collectors.

SEE ALSO: Convert a List to Stream in Java 8.


findFirst() example in Stream, Java 8

The findFirst() operation returns an object of type Optional (tutorial for Optional Class here) describing the
first element of this stream, or an empty Optional if the stream is empty.
If the stream has no encounter order,then any element may be returned.


Stream<String> items=Stream.of("findFirst","Example","Stream","Java8");
Optional<String> op = items.findFirst();
  
System.out.println(op);       // Optional[findFirst]
System.out.println(op.get()); // findFirst


In the case where the Stream is empty the findFirst() method returns a empty Optional object.

Stream<String> items=Stream.of();
Optional<String> op = items.findFirst();

System.out.println(op); // Optional.empty


To conclude use the findFirst() method in Java 8 to find the first element of any Java 8 Stream.
It returns a object of type Optional.
If the Stream is empty it will return an empty Optional object in the form Optional.empty.



In the previous article I explained about Collectors in Java 8. Now we will move on to another class that is introduced in Java 8 called Optional.

Optional is a new class introduced in Java 8 to address problems associated with NullPointerExceptions.
The Optional object in Java 8 is used to hold a SINGLE value which as the name suggests, is optional, meaning it
has two options, that is it can be null or not-null.
Which means that it is a container object which may or may not contain a NULL value.


Some of them are Explained in details below:
Optional.isPresent()
Optional.get()
Optional.orElse()
Optional.orElseGet()
Optional.orElseThrow()
Optional.filter()
Optional.of()
Optional.ofNullable()
Optional.empty()


Consider the code sample.
We have two separate streams. Stream1 is having valid data while Stream2 is empty stream.

Stream<String> items1=Stream.of("findFirst","Example","Stream","Java8");
Stream<String> items2=Stream.of();
  
Optional<String> op1 = items1.findFirst();   
Optional<String> op2 = items2.findFirst();  


The Optional.isPresent() method


The ispresent() method of the optional object returns true if there is a value present,
otherwise it returns false.

System.out.println(op1.isPresent()); //true
System.out.println(op2.isPresent()); //false



The Optional.get() method

The get() method of the Optional object returns the value if a value is present in this Optional
object, otherwise throws NoSuchElementException.

System.out.println(op1.get()); //findFirst
System.out.println(op2.get()); //java.util.NoSuchElementException: No value present



The Optional.orElse() method


If a value is present in this optional then it is returned else it returns the replacement.
Since the 1st optional object has a value thus the value is returned.
The second optional does not have a value and thus the replacement "Hello" is returned.

System.out.println(op1.orElse("Hello"));  // findFirst
System.out.println(op2.orElse("Hello"));  // Hello



The Optional.orElseGet() method

Return the value if present, otherwise invoke supplied object and return the result of that invocation.
Here in the first case op1 has value hence the value is returned.
In the case of op2, there is no value and it will call the supplier.get() method by default.

Supplier<String> supplier=new Supplier<String>() {

   @Override
   public String get() {
    
    return "This is a replacement";
   }
  };
System.out.println(op1.orElseGet(supplier));  // findFirst
System.out.println(op2.orElseGet(supplier));  // This is a replacement



The Optional.orElseThrow() method

The orElseThrow() method of the Optional class returns the contained value, if present,
otherwise throw an exception to be created by the provided supplier.

System.out.println(op1.orElseThrow(NullPointerException::new));  // findFirst
System.out.println(op2.orElseThrow(NullPointerException::new));  // java.lang.NullPointerException



The Optional.filter() method

The filter() method in the Optional class is used to filter the data based on a given condition.
If this condition is met then it returns an Optional object defining its value.
Else if the filter condition is not met it will return a Optional.empty object.

System.out.println(op1);                                   //Optional[findFirst]
System.out.println(op1.filter(x -> x.contains("First")));  // Optional[findFirst]
System.out.println(op1.filter(x -> x.contains("Second"))); // Optional.empty



The Optional.of() method

The of() method of the optional class returns an Optional with the specified present non-null value.
It is to be noted here that the value supplied must not be NULL in any situation else we will get a NullPointerException in those cases. If we have situations where the supplied value
may be null then we must use ofNullable() instead.

Optional<String> op1 = Optional.of("Hello");
System.out.println(op1.get());  // Hello



The Optional.ofNullable() method

The ofznullable() method in Optional class returns an Optional describing the specified value,
if non-null, otherwise returns an empty Optional.Unlike of() it accepts NULL values.
If the Null value is supplied then it returns an empty Optional object.

Optional<String> op1 = Optional.ofNullable("Hello");
System.out.println(op1);  // Optional[Hello]

  
Optional<String> op2 = Optional.ofNullable(null);
System.out.println(op2);  // Optional.empty



The Optional.empty() method

The empty() method of the Optional class returns an empty Optional instance.
No value is present for this Optional.
It makes the Optional drop its value and makes it empty.

Optional<String> op1 = Optional.ofNullable("Hello");
System.out.println(op1);  // Optional[Hello]
  
Optional<String> op2 = op1.empty();
System.out.println(op2);  // Optional.empty



In conclusion the Optional is a container object which may or may not contain a non-null value.
If a value is present, isPresent() will return true and get() will return the value.

Stream Collectors Example in Java 8 : toList, toCollection, summingInt, groupingBy

The collect operation in stream is a handy terminal operation which is used to accumulate or convert a stream in to collections like List, Set Or Map.

It can be used to accumulate data present in a stream and convert into a list or a similar collection.
Similarly we can summarize data according to various criteria.

A collect operation is a combination of 4 elements namely supplier, an accumulator, a combiner and a finisher.
But since Java 8 uses inbuilt collectors, we do not need to implement one for most of our daily operations.


Here are a list of Methods explained below:
   1.   Collectors.toList()
   2.   Collectors.toCollection()
   3.   Collectors.summingInt()
   4.   Collectors.averagingDouble() and Collectors.averagingInt()
   5.   Collectors.groupingBy()


Lets the fun begin!!
We will create a List of Employee Objects to explain this tutorial.
The Employee class looks like this. It has a constructor and some getters and setters. Simple right?!!

package codermag.net.java8;

public class Employee {
 public String name;
 public int age;

 Employee(String name, int age) {
  this.setName(name);
  this.setAge(age);
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 @Override
 public String toString() {
  return getName();
 }
}


Now lets us look at the program below:


 List<Employee> employees = new ArrayList<Employee>();
  employees.add(new Employee("John", 35));
  employees.add(new Employee("Wendy", 43));
  employees.add(new Employee("Scott", 22));
  employees.add(new Employee("Joe", 17));
  employees.add(new Employee("Brad", 43));

  Stream<Employee> employeeStream = employees.stream();




1. Lets collect all the Employee Names and collect them in a List

The toList() operation in one of the basic operations of the Collectors class.
The toList() function in Collectors helps to convert a Stream to a List as shown below.

  // Accumulate names into a List
  List<String> listOfNames = employeeStream
                                   .map(Employee::getName)
                                   .collect(Collectors.toList());

System.out.println(listOfNames ); //[John, Wendy, Scott, Joe, Brad]




2. Lets filter names starting with the letter 'J'.


  // Filter names starting with 'J'
  List<String> listOfNamesStartingWithJ = employeeStream
                                              .filter(x -> x.getName().startsWith("J"))
                                              .map(Employee::getName)
                                              .collect(Collectors.toList());  

System.out.println(listOfNamesStartingWithJ); //[John, Joe]



3. Get the List of person objects whose name starts with 'J'.

For this case we are introducing an intermediate filter() method to filter data based on criteria.

List<Employee> listOfPersons = employeeStream
                                 .filter(x -> x.name.startsWith("J"))
                                 .collect(Collectors.toList());  

System.out.println(listOfPersons); //[John, Joe]




4. Lets sort the names using TreeSet.

A similar operation is toCollection() operation of Collectors class in Java 8.
Unlike the toList() Collector class, this is more generalized method for converting to collections.

  // Accumulate names into a TreeSet
  Set<String> setOfName = employeeStream
                                  .map(Employee::getName)
                                  .collect(Collectors.toCollection(TreeSet::new));

System.out.println(setOfName);  //[Brad, Joe, John, Scott, Wendy]




5. Get the sum of ages of all Employees.

Collectors in Java 8 are versatile and there are various utility methods which makes our
life much more simplier. For example the summingInt() of Collectors class in Java 8 lets you
calculate the sum of attributes of Stream data.

      int totalAge = employeeStream
                           .collect(Collectors.summingInt(Employee::getAge));
  
  System.out.println(totalAge); //10



6. Get the average of the ages of all employees.

Collectors class in Java 8 has functions like averagingDouble() and averagingInt()
to directly calculate the average of nummeric values.

Double averageAge=employeeStream
                        .collect(Collectors.averagingDouble(emp -> emp.age));
  
System.out.println(averageAge); // 32.0




7. Group by a particular attribute.

In java 8 Collectors, probably one of the most talked about method is the groupingBy() method.
The groupingBy() method in Collectors class in Java 8 lets you group data based on criterias set by you.
For example the following method uses the groupingBy() Collectors class method to group employees based
on their ages.

Map<Integer, List<Employee>> empMap = employeeStream
                                          .collect(Collectors.groupingBy(x -> x.age));

  empMap.forEach((age, empList) -> System.out.format(
    "Employees of age %s are : %s\n", age, empList));


/*
Employees of age 17 are : [Joe]
Employees of age 35 are : [John]
Employees of age 22 are : [Scott]
Employees of age 43 are : [Wendy, Brad]
*/




Stream.filter() Example in Java 8

The filter is a Stream API method defined as filter(Predicate<? super T> predicate).
The filter method filters the Stream and returns a stream consisting of the elements of this stream that match the given predicate/condition.
Lets see the filter method in action.


Example 1:

The following program will filter all the names starting with 'B'.

package net.codermag.sample.java8;

import java.util.stream.Stream;

public class StreamFilterExample1 {
 
 public static void main(String[] args) {

 Stream<String> dataStream=Stream.of("Bob","Henry","Brad","Wendy"); 
 
 dataStream.filter(x -> x.startsWith("B"))
           .forEach(System.out :: println);
 }
}

Output:

Bob
Brad



Example 2:

Lets see another example.
The following program uses an isOdd() to filter the odd number.

package net.codermag.sample.java8;

import java.util.stream.Stream;

public class StreamFilterExample2 {

public static void main(String[] args) {
 
 Stream<Integer> dataStream=Stream.of(1,2,3,4,5,6); 
 dataStream.filter(x -> isOdd(x))
           .forEach(System.out :: println); 
}

 private static boolean isOdd(Integer x) {
  if (x % 2 == 0)
   return false;
  else
   return true;
 }
}


Output:

1
3
5



Example 3:

Now lets see a little more complex example.
Here we will filter persons based on their age.

A simple Person Class.

package net.codermag.sample.java8;

public class Person {

 private int age;
 private String name;
 
 Person(int age, String name) {
   this.age = age;
   this.name = name;
  }


 public int getAge() {
  return age;
  }
 
 public String getName() {
  return name;
  }

}


Filtering Persons based on age and printing their names.

package net.codermag.sample.java8;

import java.util.stream.Stream;

public class StreamFilterExample3 {
 public static void main(String[] args) {

  Person person1 = new Person(25, "John");
  Person person2 = new Person(45, "Travis");
  Person person3 = new Person(35, "Scott");
  Person person4 = new Person(55, "Wendy");

  Stream<Person> personStream = Stream.of(person1, person2, person3,person4);
  
  personStream.filter(person -> person.getAge() > 40)
              .forEach(person -> System.out.println(person.getName()));

 }
}
Output:

Travis
Wendy


So to conclude the filter() method is a very handy method while working with Streams in Java 8,
as it allows us to filter data based on our needs very easily, quickly and clearly.

Efficiency and Optimizations:
The filter() operation in Stream is an intermediate operation, which returns a lazy stream
and it thereby improves the efficiency as it does not have to process the whole Stream data.

It gains this advantage by breaking after a certain predicate is fulfilled.
Get an idea of Lazy Streams.
LEARN MORE about it here : "LAZY STREAMS in Java 8"

Stream.map() method in Java 8

The Stream.map(Function) methods returns a stream consisting of the results of applying the given function to the elements of this stream.

For Example:
If we have a Stream like this {10, 20 , 30 ,40 ,50}.
Then applying a map function like this {10, 20 , 30 ,40 ,50}.map( x -> x=x+5 )
will return a Stream like {15, 25, 35, 45, 55}.


Lets consider the following examples.

Program 1: A Simple One


package codermag.net.java8;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class StreamMapExample {
 public static void main(String[] args) {

  
  List<Integer> myList = Arrays.asList(10,20,30,40,50);
  
  //Convert this list to a stream
  Stream<Integer> items=myList.stream();
  
  Stream<Integer> newStream=items.map(x -> x=x+5);
  
  newStream.forEach(System.out :: println);
  
  
  
 }
}


Output:

15
25
35
45
55



Program 2: Some simple String operations.


package codermag.net.java8;

import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Stream;

public class StreamMapExample {
 public static void main(String[] args) {

  List<String> myList = Arrays.asList("HelloWorld","Java","CoderMagnet");
  
  //A Supplier is a supplier of stream. Each get() will give you a copy of the Stream
  Supplier<Stream<String>> streamSupplier=() -> myList.stream();
  Stream<String> stream1=streamSupplier.get();
  Stream<String> stream2=streamSupplier.get();
   
  
  
  Stream<String> newStream=stream1.map(x -> x.substring(2));
  newStream.forEach(System.out :: println);
  
  
  
  Stream<String> newStream2=stream2.map(String :: toUpperCase);
  newStream2.forEach(System.out :: println);
  
 }
}

A stream can be used only once. We are using a supplier here only because we want to use the same stream of elements 2 times.
On the 1st copy of the Stream we apply a substring() function.
On the 2nd cop of the Stream we apply a toUpperCase() function.
The output looks like this.

Output:

lloWorld
va
derMagnet
HELLOWORLD
JAVA
CODERMAGNET


Program 3: A little more complex program, trust me its easy.


package codermag.net.java8;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class StreamMapExample {
 public static void main(String[] args) {

  List<String> myList = Arrays.asList("HelloWorld","Java","CoderMagnet");
  Stream<String> stream1= myList.stream();
  
  Stream<String> newStream=stream1.map(s -> 
    {
           System.out.format("String---- %s %s\n",s,s.toUpperCase());
           return s.toUpperCase();
          });
   

  //Running a loop will call the above "map" method over each element
  newStream.forEach(x -> x=x);
  
 }
}

Here the mapping function has a function body.
Whenever we run forEach Loop over the elements, this method body will be called every time.
The output will look like this below.

Output:

String---- HelloWorld HELLOWORLD
String---- Java JAVA
String---- CoderMagnet CODERMAGNET

Java8 flatMap Example/Tutorial

Java8 introduces a new collection API called the Stream API.
The Stream Collection API supports processing streams of data stored within it.
This API has many advantages which will be elaborated in a consolidated post.


Supported operations in Stream API are :

Stream<String>
Stream<Integer>
Stream<String[]>
Stream<ArrayList<String>>
Stream<Set<String>>

Flattening a Stream makes it more easy to iterate and operate over it.
For these reasons it might be required to flatten the Stream like this.

Stream<String[]>           -->  Stream<String>
Stream<ArrayList<String>>  -->  Stream<String>
Stream<Set<Integer>>       -->  Stream<String>


{{1,2}, {3,4}, {5,6}}             --> flatten --> {1,2,3,4,5,6}
{{"Java","Jcr"},{"Hello","Java"}} --> flatten --> {"Java","Jcr","Hello","Java"}

ATTENTION:One WRONG thing which you might have learnt.
In some websites its said that Stream Operation like (filter, sum, distinct…) will not work without flattening a Stream.
This is absolutely wrong. All operations will work, its just that flattening a Stream will make your job easier.
I will explain these with examples a the end of this article.

1. Convert Stream<String[]> to Stream<String>


package codermag.net.java8;

import java.util.Arrays;
import java.util.stream.Stream;

public class StringArrayToFlatMap {

 public static void main(String[] args) {

  String[][] data = new String[][] 
    { 
     { "Java", "Jcr" },
     { "Hello", "Java" }, 
     { "Coder", "Magnet" } 
    };

  //Converting to Stream<String[]>
  Stream<String[]> dataStream = Arrays.stream(data);

  //Flattening the Stream to Stream<String>
  Stream<String> stringStream = dataStream.flatMap(x -> Arrays.stream(x));

  stringStream.forEach(System.out::println);

 }
}



2. Convert a Stream<List<String>> to Stream<String>


package codermag.net.java8;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;


public class ListToFlatMap {
 public static void main(String[] args) {
  
  
  List<String> list1=new ArrayList<String>();
  list1.add("Hello");
  list1.add("Developers");
  
  List<String> list2=new ArrayList<String>();
  list2.add("Welcome");
  list2.add("To Codermagnet");
  
  List<String> list3=new ArrayList<String>();
  list3.add("Wats");
  list3.add("Up?");
  
    
  List<List<String>> itemList=new ArrayList<List<String>>();
  itemList.add(list1);
  itemList.add(list2);
  itemList.add(list3);

  
  //Converting the List of List<String> to Stream
  //List<List<String>> --> Stream<List<String>>
  Stream<List<String>> listStream=itemList.stream();  
  
  //Flattening the Stream<List<String>>  to  Stream<String> 
  Stream<String> flattenedStream=listStream.flatMap(x -> x.stream());

  flattenedStream.forEach(System.out :: println);
  
  
 }
}



You don't need a flattened Stream to use methods like filter(),sum()....etc

So just to show that you can apply Stream methods here are some examples.



Example 1:
The following example filters arrays whose size is greater than 2.
This shows how you can use filter() on a Stream<String[]> instead of using a flattened Stream<String>.


package codermag.net.java8;

import java.util.Arrays;
import java.util.stream.Stream;

public class FilterWorksOnNonFlattenedStream {

 public static void main(String[] args) {

  String[][] alphabets = new String[][] 
    { 
     { "a", "b" }, 
     { "c", "d" },
     { "e", "f", "g" } 
    };

  Stream<String[]> alphabetStream = Arrays.stream(alphabets);

  // Filtering out the arrays having more than 2 elements
  Stream<String[]> filteredStream = alphabetStream.filter(x -> (x.length > 2));

  filteredStream.forEach(x -> System.out.println(Arrays.toString(x)));
 }
}


OUTPUT:

[e, f, g]



Example 2:
Filtering out List objects which contain the String "Hello".
This shows how you can use filter() on a Stream<List<String>> instead of using a flattened Stream<String>.

package codermag.net.java8;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;


public class FilterWorksOnNonFlattenedStream2 {
 public static void main(String[] args) {
  
  
  List<String> list1=new ArrayList<String>();
  list1.add("Hello");
  list1.add("Developers");
  
  List<String> list2=new ArrayList<String>();
  list2.add("Welcome");
  list2.add("To Codermagnet");
  
  List<String> list3=new ArrayList<String>();
  list3.add("Hello");
  list3.add("WhatsUp?");
  
    
  List<List<String>> itemList=new ArrayList<List<String>>();
  itemList.add(list1);
  itemList.add(list2);
  itemList.add(list3);

  
  //Converting the List of List<String> to Stream
  //List<List<String>> --> Stream<List<String>>
  Stream<List<String>> listStream=itemList.stream();  
  
  //Filtering only the List objects containing the string "Hello"
  Stream<List<String>> filteredList=listStream.filter(x -> x.contains("Hello"));
  
  filteredList.forEach(System.out :: println);
 }
}


OUTPUT:
[Hello, Developers]
[Hello, WhatsUp?]




Remove duplicates from a list using Stream.distinct() in Java 8

In Java 8 we can easily remove duplicates from a List(or collection) in the following manner.

File:RemoveDuplicateUsingStream .java

package net.codermag.java8;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class RemoveDuplicateUsingStream {
 public static void main(String[] args) {
    List<Integer> integerList = Arrays.asList(67, 8, 4, 21, 25, 64, 76, 52, 4, 21);   
    
    Stream<Integer> filteredStream=integerList.stream().distinct();
     
    
    
    System.out.println("Count before removing duplicates : "+integerList.size());
    System.out.println("Count after removing duplicates : "+filteredStream.count());
    
   }
}



Output:

Count before removing duplicates : 10
Count after removing duplicates : 8