[Java] Try to implement using generics

Here are some notes on implementations using Generics.

Takes Class \ <T > and returns an object with generics \ <T >

If you declare something that specifies generics normally, it tends to be redundant, so if you want to generate such an object many times, I think it is better to create a generation function in the Util class.

Implementation example


//A function that returns a List corresponding to the class
<T> List<T> generateList(Class<T> clazz) {
    return new ArrayList<>();
}

//A function that returns a BeanPropertyRowMapper that corresponds to the class
<T> BeanPropertyRowMapper<T> getBeanPropertyRowMapper(Class<T> clazz) {
    return new BeanPropertyRowMapper<>(clazz);
}

Usage example


//Redundant when written like this
new BeanPropertyRowMapper<Something>(Something.class);

//It is easier to read if you write like this
Util.getBeanPropertyRowMapper(Something.class);

Write so that no warning / error is issued when the type does not matter

In the case of "use a type that takes generics, but the type of the contents does not matter", simply omitting the type will give a warning.

Example of warning


//Duplicate check using hash set
boolean checkForDuplicate(List list) {
    return list.size() == new HashSet(list).size();
}

In such cases, you can write <?> And the diamond operator to avoid warnings.

Implementation example


//Duplicate check using hash set
boolean checkForDuplicate(List<?> list) {
    return list.size() == new HashSet<>(list).size();
}

You can write it by specifying it with <T>, but it will be a little redundant.

Redundant implementation example


//Duplicate check using hash set
<T> boolean checkForDuplicate(List<T> list) {
    return list.size() == new HashSet<>(list).size();
}

When using Stream

Even when performing Stream processing with List as an argument, compilation may not pass unless <?> Is written.

Example that compilation does not pass


BeanPropertySqlParameterSource[] makeParamArray(List entities) {
    return entities.stream()
            .map(BeanPropertySqlParameterSource::new)
            .toArray(BeanPropertySqlParameterSource[]::new);
}

Example of compiling


BeanPropertySqlParameterSource[] makeParamArray(List<?> entities) {
    return entities.stream()
            .map(BeanPropertySqlParameterSource::new)
            .toArray(BeanPropertySqlParameterSource[]::new);
}

Recommended Posts

[Java] Try to implement using generics
Try to implement Yubaba in Java
Try to implement TCP / IP + NIO with JAVA
Try using RocksDB in Java
Try scraping using java [Notes]
[Java] How to implement multithreading
Try to implement using Rakuten product search API (easy)
Try to build a Java development environment using Docker
Try to implement tagging function using rails and js
Implement Thread in Java and try using anonymous class, lambda
Try to implement Yubaba in Ruby
Try to implement iOS14 Widget function
Try to extract java public method
[Java] Generics
Try using IBM Java method tracing
Try Spark Submit to EMR using AWS SDK for Java
Try using Hyperledger Iroha's Java SDK
[Java] Where did you try using java?
[Java] Try to solve the Fizz Buzz problem using recursive processing
Try passing values from Java Servlet to iPhone app using JSON
Interface Try to make Java problem TypeScript 7-3
Try using Java framework Nablarch [Web application]
How to implement date calculation in Java
How to implement Kalman filter in Java
Try to solve Project Euler in Java
Try using the Stream API in Java
[Java] How to calculate age using LocalDate
Study Java Try using Scanner or Map
Try using JSON format API in Java
Try to implement login function with Spring-Boot
Connect from Java to MySQL using Eclipse
How to implement coding conventions in Java
Try to implement UIAlertController by separating files
Try using JobScheduler's REST-API --Java RestClient implementation--
[Swift5] How to implement animation using "lottie-ios"
How to implement image posting using rails
Try using the Wii remote with Java
Try using libGDX
Try using Maven
Try using powermock-mockito2-2.0.2
Try using GraalVM
[Java] Generics sample
Java Generics (Notes)
Try Java 8 Stream
Try using jmockit 1.48
Try using sql-migrate
Try using SwiftLint
Try using Log4j 2.0
[Java] Introduction to Java
Introduction to java
Roughly try Java 9
Try using Firebase Cloud Functions on Android (Java)
Try using JobScheduler's REST-API --Java RestClient Test class-
Try to build Java8 environment on Amazon Linux2
Try to create a bulletin board in Java
Try to link Ruby and Java with Dapr
[Java] Convert DB code to code value using enum
Increment behavior Try to make Java problem TypeScript 3-4
Try using Sourcetrail (win version) in Java code
I tried to implement deep learning in Java
Try using GCP's Cloud Vision API in Java