[Java10] Be careful of using var and generics together

0. Introduction

Var introduced in Java 10. It's convenient, but in some cases it can be addictive.

Sample code


var list = Set.of(BigDecimal.ZERO, BigDecimal.ONE);
//What is the type of this Collector?
var toList = Collectors.toList();
var list2 = list.stream().collect(toList);

//What is the type of list2? Correct answer: list<Object>
System.out.println(list2.get(0));

1. Why is this happening?

If all of the following conditions are met, it will be an Object type because the type cannot be determined. (1) There is no argument, or the generic type is not determined by the argument (2) No type argument is specified

2. What do you do?

However, avoidance itself is not difficult, and detection is not difficult. -Specify the type argument ・ Do not use var as before

Sample code 1(Specify the type argument)


var list = Set.of(BigDecimal.ZERO, BigDecimal.ONE);
//What is the type of this Collector?
var toList = Collectors.<BigDecimal>toList();
var list2 = list.stream().collect(toList);

//What is the type of list2? Correct answer: list<BigDecimal>
System.out.println(list2.get(0));

Sample code 2(Avoid using var as before)



var list = Set.of(BigDecimal.ZERO, BigDecimal.ONE);

Collector<BigDecimal, ?, List<BigDecimal>>  toList = Collectors.toList();
var list2 = list.stream().collect(toList);

//What is the type of item?
System.out.println(list2.get(0));

However, this case is not just about "how to avoid it?" It seems that the difficult point is "whether to incorporate or not to incorporate? What kind of response policy should be unified?" For the new element called var, but by the way.

Recommended Posts

[Java10] Be careful of using var and generics together
[Java] Be careful of the key type of Map
Be careful with requests and responses when using the Serverless Framework in Java
[Java] Generics classes and generics methods
java Generics T and? Difference
Advantages and disadvantages of Java
[Java] Comparison method of character strings and comparison method using regular expressions
Be careful of initialization timing when using MessageEncryptor with Rails 5.2 / 6.0
Be careful when using multiple articles
[Java] Judgment of identity and equivalence
[Java] Try to implement using generics
Java generics (defines classes and methods)
Summary of object-oriented programming using Java
I tried using GoogleHttpClient of Java
Deploy an image consisting of Tomcat, Java and MySQL using Ansible and Jenkins
Parse and objectize JSON using the @JsonProperty annotation of the Java library Jackson
Display Japanese calendar and days of the week using java8 standard class
java jshell and var are too recommended!
[Java] Inheritance and structure of HttpServlet class
Implementation of tabs using TabLayout and ViewPager
[Java / Swift] Comparison of Java Interface and Swift Protocol
Java and Derby integration using JDBC (using NetBeans)
Summary of Java Math.random and import (Calendar)
[Java] Contents of Collection interface and List interface
Be careful when using rails_semantic_logger with unicorn
Basics of java basics ② ~ if statement and switch statement ~
Discrimination of Enums in Java 7 and above
[Java] Generics
Be absolutely careful when putting the result of and / or in a variable!
Create API using Retrofit2, Okhttp3 and Gson (Java)
Install java and android-sdk on Mac using homebrew
Things to be aware of when writing Java
Be careful if you find SHIFT-JIS in Java
[Java] Personal summary of classes and methods (basic)
Verification of performance impact when using Java volatile
[Java] The confusing part of String and StringBuilder
Simple installation of nginx and Docker using ansible
I compared the characteristics of Java and .NET
JAVA: Realizes generation and scanning of various barcodes
Try similar search of Image Search using Java SDK [Search]
[Java] Be aware of short circuits (short-circuit evaluation)
[Java] Classification memo of compilation error and run-time error
Java Servlet should be aware of multithreaded environment
Story of test automation using Appium [Android / java]
Java enables extraction of PDF text and images