I background
In many scenarios, the data of List needs to be transformed into the key-value pairs of Map scenarios to facilitate quick query data. For example: the need to query the corresponding person name details scenario according to the work number. There are two kinds of scenarios, the first one is to query the database once by each work number, which is obviously not reasonable because it is not good for DB pressure and connection. The second will be a collection of work number (need to consider the volume of data scenarios, Oracle supports a maximum of one thousand, mysql, although no online, but consider the performance and memory consumption need to consider the upper limit recommended 1000 below), the next is the second way to expand.
Translated with www.DeepL.com/Translator (free version)
II Development practice
List<User> userList = new ArrayList<>();
userList.add(User.builder().id(123).name("TEST123").build());
userList.add(User.builder().id(1231).name("TEST1231").build());
userList.add(User.builder().id(1232).name("TEST1232").build());
userList.add(User.builder().id(1233).name("TEST1233").build());
userList.add(User.builder().id(1234).name("TEST1234").build());
1.The stream implementation was not available before Java 1.8
Map<String, Long> idNameMap = new HashMap<String, Long>();
for(User user : userList ){
idNameMap.put(user.getName(), user.getId());
}
return idNameMap ;
2.Java 1.8 onwards supports stream implementation
return userList.stream().collect(Collectors.toMap(User::getName, User::getId);
III Existing problems
If you use the 2.1 way, the possible problems, if there are two data with the same name in the data, it will report an error java.lang.IllegalStateException: Duplicate key.
When using stream stream, it will not overwrite the data directly like the above way, but will report an error. So it needs to be optimized as
return userList.stream().collect(Collectors.toMap(User::getName, User::getId , (entity1, entity2) -> entity1);
It can solve the problem of error reporting.
Read More:
- [Solved] Internal error (java.lang.IllegalStateException): Duplicate key org.jetbrains
- JAVA 8: How to Convert List to Map
- [Solved] Java.lang.ClassCastException: [Ljava.lang.Long; cannot be cast to java.util.List
- Caused by: java.lang.IllegalStateException (How to Fix)
- JUnit test classes error: java.lang.IllegalStateException: Failed to load ApplicationContext
- [Solved] Java.lang.IllegalStateException: getReader() has already been called for this request
- [Solved] Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;
- [Solved] Scala error: type mismatch; found : java.util.List[?0] required: java.util.List[B]
- [Solved] java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
- [Solved] Java.lang.IllegalStateException: Type handler was null on parameter mapping for property ‘__frch_it
- Java will convert Excel to list set or JSON, export excel file to local, excel import and export, easyexcel tool class
- [Solved] Hibernate Error: java.lang.StackOverflowError at java.lang.Integer.toString(Integer.java:402)
- [Solved] Logstash Error: Logstash – java.lang.IllegalStateException: Logstash stopped processing because of an err
- How to convert a Java string into a number (stringtonumber)
- [Solved] Failed to instantiate java.util.List using constructor NO_CONSTRUCTOR with arguments
- I/O error while reading input message; nested exception is java.io.IOException: Stream closed
- [Solved] Read the resources resource and convert it to file error: java.io.filenotfoundexception
- JAVA: How to Convert PDF from Color to Grayscale
- Java uses class array to report error Exception in thread “main” java.lang.NullPointerException solution
- [Solved] POI Read excel Error: Your InputStream was neither an OLE2 stream, nor an OOXML stream