HHH-13675 : Optimize PersistentBag.groupByEqualityHash()

This commit is contained in:
Christoph Dreis 2019-10-17 23:10:18 +02:00 committed by Sanne Grinovero
parent 1c840f9dd1
commit 3b1e7afb20
1 changed files with 10 additions and 2 deletions

View File

@ -11,11 +11,12 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.stream.Collectors;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
@ -193,7 +194,14 @@ public class PersistentBag extends AbstractPersistentCollection implements List
* @return Map of "equality" hashCode to List of objects
*/
private Map<Integer, List<Object>> groupByEqualityHash(List<Object> searchedBag, Type elementType) {
return searchedBag.stream().collect( Collectors.groupingBy( elementType::getHashCode ) );
if ( searchedBag.isEmpty() ) {
return Collections.emptyMap();
}
Map<Integer, List<Object>> map = new HashMap<>();
for (Object o : searchedBag) {
map.computeIfAbsent( elementType.getHashCode( o ), k -> new ArrayList<>() ).add( o );
}
return map;
}
@Override