-
-
Notifications
You must be signed in to change notification settings - Fork 343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Provide collect methods instead of GenericType #2262
Comments
Hi @gpsfl, thanks for opening an issue with the Jdbi project. Use I feel that the list collection is as succinct as a new
For maps, you can use Map<K, V> result = query.reduceRows(new LinkedHashMap<K, V>(), (map, rowView) -> {
map.put(rowView.getRow(K.class), rowView.getRow(V.class));
return map;
}); Map<K, V> map = query.mapTo(new GenericType<Map.Entry<K, V>>() {})
.collect(Collectors.toMap(Map.Entry::getKey, Map::Entry::getValue)); If you are concerned about class counts / readability, use a constant: private static final CUSTOM_MAP_TYPE = new GenericType<Map.Entry<KeyType, ValueType>>() {};
[...]
Map<KeyType, ValueType> map = query.mapTo(CUSTOM_MAP_TYPE).collect(toMap(Map.Entry::getKey, Map::Entry::getValue)); which has a similar amount of characters / type as Map<KeyType, ValueType> map = query.collectToMap(KeyType.class, ValueType.class, toMap(Map.Entry::getKey, Map.Entry::getValue)); Please let me know if that works for you; we try to keep our API footprint small. I agree that our documentation could be better. |
I suggested the Regarding the |
@stevenschlansker @sman-81 any thoughts? |
For generic Collector use, it is easy enough to call I don't quite see the compelling use case for List specialization: it seems that The Map special forms do feel like there could be improvement. The reduceRow solution works but might be common enough to warrant a |
@stevenschlansker If you want to keep the |
@gpsfl I see, so you want to actually control the type of the List implementation. If we added the ability to customize the Regarding Map, I was suggesting adding a shortcut, it would look like:
Since it is important to you that it be an ImmutableMap, we could also make the result container type customizable. I am trying to avoid creating intermediate Map.Entry objects, since there is almost always a nicer way to write things without them. |
@stevenschlansker I think adding a Map<K, V> result = handle.createQuery("select k, v from mapthing")
.collectMap(K.class, V.class, LinkedHashMap::new);
Map<K, V> result = handle.createQuery("select k, v from mapthing")
.mapTo(V.class)
.list(LinkedList::new); The only issue I see with this solution is that you still can't do some of the more complex things with the Collectors api. It's not as important (you can still do it with map + collect) but I wonder if you also have an idea how this could be done easier? Here's some common examples from our codebase: .map(V.class).collect(toSet()) // Set
.map(V.class).colelct(toCollection(TreeSet::new)) // Custom collection
.map(V.class).collect(toMap(V::getId, Function.identity())) // Map by key
.map(V.class).collect(toMap(V::getId, Function.identity(), (a, b) -> a)) // Map by key (use first if not unique)
.map(V.class).collect(groupingBy(V::getGroupId)) // Grouping by
// (and all of the above with immutable collectors) |
@gpsfl , I took a look at this and made some improvements. With #2303, it is now possible to configure the collector used for I added a I think the Could you please take a look and see if my suggested improvements will help you out? I know it's not everything you asked for, but it should make some of the common cases easier. |
This is the first piece to address jdbi#2262 - ResultIterable#set() - ResultIterable#collectToMap(keyFunction, valueFunction) - add test suite - some code reshuffling
This is the first piece to address jdbi#2262 - ResultIterable#set() - ResultIterable#collectToMap(keyFunction, valueFunction) - add test suite - some code reshuffling
This is the first piece to address jdbi#2262 - ResultIterable#set() - ResultIterable#collectToMap(keyFunction, valueFunction) - add test suite - some code reshuffling
new `collectToMap()`, `toCollection()`, `collectInto()`, `collectIntoList()`, `collectIntoSet()` collect methods. This should fully address jdbi#2262
new `collectToMap()`, `toCollection()`, `collectInto()`, `collectIntoList()`, `collectIntoSet()` collect methods. This should fully address jdbi#2262
new `collectToMap()`, `toCollection()`, `collectInto()`, `collectIntoList()`, `collectIntoSet()` collect methods. This should fully address jdbi#2262
new `collectToMap()`, `toCollection()`, `collectInto()`, `collectIntoList()`, `collectIntoSet()` collect methods. This should fully address jdbi#2262
This is addressed by #2331 and will be released as part of 3.38.0 |
Right now the most simple way to convert a Query to a typed
Collection
orMap
is the following:This however has two disadvantages:
Collectors
apiThis is why I suggest adding the following two methods to handle the most common cases. From my understanding they could be implemented using the already existing logic from above:
We're using JDBI in our production environment and I think this would save quite a lot of code lines / output classes while remaining as readable. If you agree with this change I can submit a pull request for it.
The text was updated successfully, but these errors were encountered: