{{announcement.body}}
{{announcement.title}}

Why I Am Thankful for Java

DZone 's Guide to

Why I Am Thankful for Java

In one of five Thanksgiving-based articles, a Zone Leader provides aspects of Java programming language for which he is thankful to utilize on a daily basis.

· Java Zone ·
Free Resource

Coffee and fall leaves

What are you thankful for this year?

In the United States, the end of the month of November is when time is taken to perform a retrospective-like event called Thanksgiving. What started out as a dedication to giving thanks for the blessings of the harvest and preceding year has transformed into a time to simply be thankful for one's blessings.

Since Thanksgiving is recognized on the fourth Thursday in the month of November, I thought I would introduce a five-part technical twist with the following Thanksgiving-focused articles:

I hope you find time to check out the other articles, as well!

Stream API

As I've noted in my "I Want My Code to Be Boring" article, "I fully expect you to yawn when you see the aspects of the Java language that I am employing most of the time." However, one aspect of Java 8 that I've really embraced is the Stream API:

When I need to find a value in a Collection-based class, I can leverage the stream functionality:

myObject = objectList.stream()
                .filter(thisObject -> valueToFind.equals(thisObject.getValueToFind()))
                .findAny()
                .orElse(null);


If I need to create a new List from a property in another List, I can leverage the stream functionality:

List<Long> idList = myList.stream().map(MyObject::getId).collect(Collectors.toList());


If I want to loop through the values of a List and do some work, I can also use the stream functionality:

List<SomeObject> newObjects = new ArrayList<>();

myList.stream().forEach(id -> {
  itemList.stream().filter(object -> object.getId() == id).findAny().ifPresent(newObjects::add);
});


Project Lombok

After spending years of writing a great deal of boilerplate code, I have become so thankful that Project Lombok (and other similar options) exist. Using Lombok, I no longer have to create getters and setters for an object:

public class MyObject {
private long id;
private String name;

public MyObject() { }

public MyObject(long id, String name) {
   this.id = id;
   this.name = name;
}

public long getId() {
return this.id;
}

public void setId(long id) {
   this.id = id;
}

public String getName() {
   return this.name;
}

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


I can simply use an annotation ... and Project Lombok has options for all arguments constructor and no arguments constructor too:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class MyObject {
private long id;
private String name;
}


JPA Repository

The Spring Data JPA Repository interface is something in which I am very grateful. The days of writing a DAO interface and implementation class, where a query is executed and the results are cast into an object or a collection, are no longer a requirement. With this departure comes the benefit of no longer needing to write even more boilerplate code.

Consider the following JPA Repository examples:

public interface MyObjectRepository extends JpaRepository<MyObject, Long> {

    List<MyObject> findBySomeAttributeId(Long attributeId);

    @Query(value = "SELECT ... ", nativeQuery = true)
    List<MyObject> findUsingSomeComplexQuery(Long id);
}


If the MyObject has a Long someAttribute value, the findBySomeAttributeId() method will perform the query and return a List<MyObject>  which matches the attributeId provided. No need to write the query and handle the result set either.

In the findUsingSomeComplexQuery() method, a native SQL query can be provided and executed in the same manner.

What is even cooler about the JPA Repository is that it is possible to use built-in functionality as well:

Optional<MyObject> optional = myObjectRepository.findById(id);


By providing a valid id in the example above, an Optional is returned, which will contain the expected MyObject ... without having to add anything to the MyObjectRepository interface.

Spring Boot

As a Java developer creating RESTful APIs, I must give thanks to Spring Boot and all the hard work provided by the amazing team at Pivotal software.

Honestly, a month-long series of articles could be penned focused solely on the greatness that is Spring Boot.

If you have not had an opportunity to use Spring Boot, I highly suggest taking 15-30 minutes to create your very first Spring Boot project, then start peeling back layers of functionality to help build an amazing API for your client or customer to utilize.

IntelliJ IDEA Ultimate Edition

So many times the phrase "you get what you pay for" becomes a topic of conversation. In terms of developer IDE's, I fully believe this is the case. While there are certainly some very good IDE's that are available at no cost to the developer, the small cost one has to pay for IntelliJ IDEA is certainly outweighed by the value returned to not only the developer — but those projects being completed by the developer using the Ultimate Edition of IntelliJ IDEA.

For quite some time, I have worked on projects where developers utilized one of those free IDE's. In every case, merely opening up the code during the PR stage leads me to suggestions and corrections the free IDE's fail to locate. Furthermore, the Ultimate Edition also provides feedback when code is being introduced which will be non-performant - offering suggestions to provide a better code stream.

In 2003, my friend Darren told me to start using IntelliJ over another IDE I was using at the time. The biggest benefit for us back then, was a very small memory footprint. In fact, I am pretty sure it was 128k of RAM. Since that time, I have been an avid use of IntelliJ's projects — especially the use of the IDEA Ultimate Edition on all of my projects since 2014.

Like the Spring Boot thoughts above, I highly recommend taking IntelliJ IDEA for a test drive. If you are like me, you will see the value well before the free-trial expires.

Conclusion

With this article and the other articles listed in the introduction, I was inspired by realizing just how fortunate I am to be employed in an industry that has so many wonderful facets that can be employed on a daily basis. Since Information Technology is an industry of constant change, I fully believe I could revisit this topic on a yearly basis and provide another series of articles for which I am thankful.

As a kid growing up in the United States, there were two Thanksgiving television specials that I looked forward to watching each year: A Charlie Brown Thanksgiving and the Brady Brunch Thanksgiving episode (The Un-Underground Movie, season two, episode four). While most have likely seen A Charlie Brown Thanksgiving, I thought I would include a link to Greg Brady's school project, "Our Pilgrim Fathers", from that episode:


https://www.youtube.com/watch?v=ygoLKkLExAg&has_verified=1

My hope is that you can take time this month to reflect on the things by which you are thankful. While I am blessed to work in such an amazing field, I am truly blessed that each of you have taken time to read my article.

Have a really great (holi)day!

Topics:
java ,stream api ,spring boot ,jpa 2.1 ,lombok ,intellij

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}