JavaRush /Java Blog /Random EN /Coffee break #73. How to Improve Your Java Code with Stre...

Coffee break #73. How to Improve Your Java Code with Streams. Five Best IntelliJ IDEA Plugins for Java Developers

Published in the Random EN group

How to Improve Your Java Code with Streams

Source: Dev.toCoffee break #73.  Improve your Java code with Streams.  Five Best IntelliJ IDEA Plugins for Java Developers - 1

What are Streams?

Streams first appeared in Java 8. According to Oracle documentation, they are classes for supporting functional-style operations on streams of elements, such as map-reduce transformations on collections. In simple terms, a stream consists of a data source, followed by zero or more intermediate operations, and then a terminal operation.

What is a data source?

  • Collections, Lists, Sets, ints, longs, doubles, arrays, file lines.

What are intermediate operations?

  • Filter, map, sort, etc.
  • These operations return a Stream so they can be chained to other operations.

What are terminal operations?

  • ForEach, collect, reduce, findFirst, etc.
  • They return an invalid or non-stream result.
  • If a thread does not have a terminal operation, intermediates will not be called.

Let's convert a function from imperative to declarative style using Streams

Imperative function (No Streams):

private int getResult_imperative(List<String> strings) {
    int result = 0;
    for (String string : strings){
        if(isDigit(string.charAt(0))) continue;
        if (string.contains("_")) continue;
        result += string.length();
    }
    return result;
}
Here we notice that we need to do a few things manually:
  • Declare a result variable to keep track of the result.
  • Loop through the Strings.
  • Write two if statements (which can be much more complex than this case).
  • Add the length of each to the result.

Let's check the declarative style (Streams):

private int getResult_Declarative(List<String> strings){
    return strings.
            stream().
            filter(s -> !isDigit(s.charAt(0))).
            filter(s -> !s.contains("_")).
            mapToInt(String::length).
            sum();
}
So what's the difference?
  • We obtain a Stream object by calling the stream() function .
  • (Intermediate operation) we use the filter function twice - each time we specify a condition that must be met only by those elements that we want to move to in the next phase.
  • (Intermediate operation) we map each String object to an int by calling the length method (using method reference style).
  • (Terminal operation) sum all previous int values .

Observations

Didn't the second approach seem simpler? We indicated what we wanted, not how we wanted to do it . This is the spirit of declarative programming and the goal of the Stream API in modern Java applications.

Five Best IntelliJ IDEA Plugins for Java Developers

Source: GitHubCoffee break #73.  Improve your Java code with Streams.  Five Best IntelliJ IDEA Plugins for Java Developers - 2 Plugins can be very useful in many situations when working with code on a regular basis. They are capable of extending core functionality, providing various integrations, and supporting automation of many tasks. Here are the best plugins in my opinion:

SonarLint

SonarLint allows you to fix errors and vulnerabilities when writing code. It highlights coding problems in real time, giving the developer clear instructions on how to fix them, so you can fix them before the code is committed. This plugin is necessary because it greatly improves coding.

Maven Helper

I hope you are using Maven? I do! The Maven Helper plugin provides:
  • a simple way to analyze and eliminate conflicting dependencies;
  • an easy way to find direct or transitive dependencies;
  • steps to run/debug maven targets for the module containing the current file, or in the root module;
  • action to open a terminal at the current path to the maven module;
  • actions to run/debug the current test file. If maven-surefire-plugin is configured to skip or exclude a test, the "verify" goal will be used. Various configuration styles can be found on GitHub .

CodeMetrics

This plugin has tab indicators based on custom difficulty calculations for Java files. They give the developer hints in classes and methods so that he can easily determine what needs to be checked. Although this is not a standard metric, it is a close approximation to cyclomatic complexity . You can also customize the complexity calculation for a project by changing the appropriate configuration entries.

String Manipulation

Case switching, sorting, filtering, zooming, column alignment, grepping, escaping, encoding and much more...

JPA Buddy

JPA Buddy makes everything JPA-related easy and fast. It provides tools to help you work with Hibernate, Spring Data JPA, Liquibase, Flyway. Key features of JPA Buddy:
  • JPA Entities: Create and edit entities, entity attributes, lifecycle callbacks, indexes, and constraints. Support for JPA converters and Hibernate custom types. Ability to use Lombok annotations for entities.
  • Create correct implementations of equals, hashCode and toString methods for JPA entities.
  • Source code intents, checks, and quick fixes for JPA entity declarations.
  • Graphically display the entity relationship in the JPA structure panel under the persistent units node.
  • Automatic generation of Liquibase change logs and Flyway version migrations: database-to-database, model-to-database, model-to-snapshot comparisons.
  • Visual Liquibase changelog designer and coding assistance: creating and editing items, referencing table names, column names, included files and more.
  • Actions: create a snapshot of Liquibase, execute the commands “Liquibase update” and “Liquibase updateSQL”.
  • Spring Data Repositories: Creating repositories, creating repository methods, editing method properties, creating a projection based on an entity class, extracting a JPQL query.
  • Kotlin: All visual designers fully support code generation for Kotlin objects and repositories.
I said about the top five plugins, but I have one more...

Extra Icons

This is an icon collection that adds shortcuts to files such as Markdown, Yaml, Maven, Git and many more. You will get used to them as they help you recognize files easily. You will definitely need them.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION