How to convert For loop using Stream in Java

Creating a Map of Applicant object, where I filter out on the applicant’s age. I only need the first name and last name of the applicant. Using the application ID as the key. The ID is generated integer and unique. Also, testing for null, don’t want that in there. I am using a traditional for loop where I am most comfortable at. But I want to use the Java 8 Stream instead. How is it done?

My code for this is below:

        Applicant a1 = new Applicant();
        a1.setId(1001);
        a1.setFirstName("Joseph");
        a1.setLastName("Dey");
        a1.setAge(25);

        Applicant a2 = new Applicant();
        a2.setId(2001);
        a2.setFirstName("Maxine");
        a2.setLastName("Summers");
        a2.setAge(21);

        Applicant a3 = new Applicant();
        a3.setId(3001);
        a3.setFirstName("Jimmy");
        a3.setLastName("Cox");
        a3.setAge(17);

        Applicant a4 = null;

        List<Applicant> list = new ArrayList<>();
        list.add(a1);
        list.add(a2);
        list.add(a3);
        list.add(a4);

        Map<Integer, String> map = new HashMap<>();
        for (Applicant a : list) {
            if (a != null && a.getAge() > 18) {
                map.put(a.getId(), a.getFirstName() + " " + a.getLastName());
            }
        }

ANSWER

By Statement Lambda in Collectors.toMap – right-hand side is a block. This can become longer to write but sometimes when you have to do more transformations, then it can’t be avoided.

1       Map<Integer, String> map = list.stream()
2                .filter(applicant -> applicant != null)
3                .filter(applicant -> applicant.getAge() >= 18)
4                .collect(Collectors.toMap(applicant -> applicant.getId(), applicant -> {
5                    return applicant.getFirstName() + " " + applicant.getLastName();
6                }));

By Expression Lambda in Collectors.toMap – right-hand side is an expression.

1       Map<Integer, String> map = list.stream()
2                .filter(applicant -> applicant != null)
3                .filter(applicant -> applicant.getAge() >= 18)
4                .collect(Collectors.toMap(applicant -> applicant.getId(), applicant -> applicant.getFirstName() + " " + applicant.getLastName()));

In both cases above, line #2 the Lambda can be replaced with method reference too. It will look like:

.filter(Objects::nonNull)

And on line #4, the same can be done for the Lamba replacing it with a method reference. It will look like:

.collect(Collectors.toMap(Applicant::getId,  // rest of code ommitted

Convert a Map to POJO in Java

How do I convert a Map object to my other plain old Java object (POJO) without going into loops and having to write a class using Java reflection, or some other?

ANSWER

Well, yes, reflection is one but you didn’t want to do that yourself. For some good reason, I bet. It’s a good exercise if you have all the time in the world. But when faced with deadlines and having to write Unit tests for a whole class you wrote, there must be an easier way.

There is more than one way, but what I normally use is the Jackson ObjectMapper. Yes, the same one from the com.fasterxml.jackson library.

Anyway, with ObjectMapper it is pretty straightforward to do so. If I have a Person class like this:

    public class Person {
        private String firstName;
        private String lastName;
    }

My Map object will look like this:

        Map<String, Object> map = new HashMap<>();
        map.put("firstName", "Johnny");
        map.put("lastName", "Foo");

Then with ObjectMapper one can simply do this:

        ObjectMapper mapper = new ObjectMapper();
        Person person = mapper.convertValue(map, Person.class);

Alternatively, the keys in the Map may not align with the fields in the Person class. Well, I usually encounter this when working with JSON objects with lots of crazy looking field names. Something like this – NZT_Mor_First_Name__c – which I clearly don’t want my class field name to be like.

Well, we can use @JsonProperty annotation which is part of Jackson by the way and assign that our class field. The Person class will now look like:

    public class Person {

        @JsonProperty("NZT_Mor_First_Name__c")
        private String firstName;

        @JsonProperty("NZT_Mor_Last_Name__c")
        private String lastName;
    }

Again the Map will hold these values:

        Map<String, Object> map = new HashMap<>();
        map.put("NZT_Mor_First_Name__c", "Jose");
        map.put("NZT_Mor_Last_Name__c", "Yamut");

Now it will map out those weird looking key names to its corresponding class fields.

One thing to note is you might need to set ObjectMapper features such as ignoring unknown properties and make it case insensitive. Allow it a bit more room to wiggle, wiggle.

        ObjectMapper mapper = new ObjectMapper()
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                .enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_VALUES);

How to ignore unknown fields when parsing JSON using Jackson

In Java, there is Jackson 2 library that is very popularly used when reading JSON objects and mapping those values out to a POJO.

Happy path, as long a I have all the fields from the JSON defined in the POJO then okay. In a perfect world it will work 100%.

But there is no perfect world. The JSON object in other scenarios can contain unknown fields. These are not in my POJO. Application fails from reading that JSON to POJO.

Getting this error:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "status" (class com.example.dto.Evaluation), not marked as ignorable (6 known properties: "Book",

ANSWER

Use this Jackson annotation at a class level. This has to be configured per class. That should do it. Will ignore any unknown properties silently including nested objects.

@JsonIgnoreProperties(ignoreUnknown = true)

When you are using Jackson ObjectMapper directly, that can be configured to ignore unknown properties globally as well.

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

How Do I Decode An OpenShift Secret In A Mounted Volume?

Mounting an OpenShift Secret to its own volume is straightforward. There are plenty of examples on how to do it. The Web is littered with examples. Next, the most common thing any manual, guide or tutorial would say is to encode the secret in Base64.

For example I have a SSL certificate stored in a Java KeyStore file format or JKS. The recommended way is to store it in a Secret instead of ConfigMap since it is sensitive information. Of course, that goes without saying the JKS file is password-protected.

Second, it must be as a Base64 string before I save it as OpenShift Secret.

How do I get the Base64 string to be decoded in the mounted volume? This one does not seem to get many answers.

Do I need to include shell commands to decode it and write it to a folder?

Which folder should I write it to as best practice?

ANSWER

#1 The encoded JKS Secret as an environment variable

This is an option but I’m not a fan of it. The JKS file when encoded can become very long, especially when the file size is large. I don’t think environment variables were meant to be used like this – hold very long text values. That said, this is an easier implementation.

Map out the secret as an env var in OpenShift or Kubernetes. Then you can use that env var to echo the value and write it out to a file.

echo -n $SECRET_JKS_VAR | base64 --decode > /file/path/to/decoded-secret.jks

#2 Mount for read, mount for write

You have the secret mounted to a file path. That is for reading. Now you need to decode, which means writing to a file so your application can read it back unencoded.

First, define a mount point as and empty directory. It must be writable. Then make it memory only.

Next, read the JKS file from its mount point, write it out to the empty dir mount point.

cat /mount/file/path/for-reading/encoded-secret.jks | base 64 --decode > /mount/file/path/for-writing/decoded-secret.jks

I like this approach better. It makes more sense rather than mapping a very long base64-encoded text value as an environment variable. The caveat is here is slightly more configurations to be made. Also if I’m not mistaken the in-memory volume count against your app memory quota. Should be negligible unless you write thousands of files into it.

#3 Lastly, write where you can

Underneath that container is a file system. Where you have permission to write, then do so there. If it’s Linux, then this decision is pretty much arbitrary IMHO. Put it in /tmp or /home or /mnt. Security wise, others might have access to your app’s pod, that means they can get to the Secret as well. But that is another topic.

I am completely new to Java 8 and unable to refactor my Java 7 code:

my java 7 code is as below:

List reviewList=new ArrayList<>();

if(!reviewList.isEmpty()  &&  !(reviewList.contains(bookReview.getBookReview().get(0)))) {
    boolean flag=false;

   for(Review review:reviewList)
    {
      if(review.getUserId().equals(bookReview.getBookReview().get(0).getUserId()))
      {
        review.setRating(bookReview.getBookReview().get(0).getRating());
        review.setReviewText(bookReview.getBookReview().get(0).getReviewText());
        review.setBookId(bookReview.getBookReview().get(0).getBookId());
        review.setUserId(bookReview.getBookReview().get(0).getUserId());
        flag=true;
        break;
      }
    }
    if(flag==false)
    {
      reviewList.add(bookReview.getBookReview().get(0));
    }
}

Can anyone please help me

Go to Source
Author: Tanay Rahangdale