Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38886 > unrolled thread
| Started by | mike <mikaelpetterson@hotmail.com> |
|---|---|
| First post | 2019-04-09 22:01 -0700 |
| Last post | 2019-04-10 19:07 +0200 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.java.programmer
Store Suppliers in a map mike <mikaelpetterson@hotmail.com> - 2019-04-09 22:01 -0700
Re: Store Suppliers in a map mike <mikaelpetterson@hotmail.com> - 2019-04-09 22:04 -0700
Re: Store Suppliers in a map Patrick Roemer <sangamon@netcologne.de> - 2019-04-10 16:12 +0200
Re: Store Suppliers in a map Patrick Roemer <sangamon@netcologne.de> - 2019-04-10 16:35 +0200
Re: Store Suppliers in a map Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-04-10 19:07 +0200
| From | mike <mikaelpetterson@hotmail.com> |
|---|---|
| Date | 2019-04-09 22:01 -0700 |
| Subject | Store Suppliers in a map |
| Message-ID | <88a3b286-0d96-425e-ac82-c02e3ebcc84a@googlegroups.com> |
Hi, I need to to different suppliers: - Supplier<String> - Supplier<Integer> - Supplier<List<String>> Map<String, Supplier> map= new HashMap<>(); How can I define a map ( some similar collection ) to store these different suppliers in? When I get it from map what will be the type? br, //mike
[toc] | [next] | [standalone]
| From | mike <mikaelpetterson@hotmail.com> |
|---|---|
| Date | 2019-04-09 22:04 -0700 |
| Message-ID | <011890e3-b48b-4fda-9d96-dea6913c30ff@googlegroups.com> |
| In reply to | #38886 |
Note: key is String and suppliers is the value.
[toc] | [prev] | [next] | [standalone]
| From | Patrick Roemer <sangamon@netcologne.de> |
|---|---|
| Date | 2019-04-10 16:12 +0200 |
| Message-ID | <q8ktl2$g7$1@newsreader4.netcologne.de> |
| In reply to | #38886 |
Responding to mike: > I need to to different suppliers: > > - Supplier<String> > - Supplier<Integer> > - Supplier<List<String>> > > Map<String, Supplier> map= new HashMap<>(); > > How can I define a map ( some similar collection ) to store these different suppliers in? > > When I get it from map what will be the type? You need a uniform key type. That leaves only raw Supplier or wildcard Supplier<?> or similar, just as you put it above. Thus the compile-time key type you receive from the map will be raw Supplier or wildcard Supplier<?>, as well. Just assuming that through some magic you'd be able to actually yank a compile-time Supplier<String> from the map - how would you proceed working with it? The client code that queries the map would probably be generic in the Supplier's result type, anyway, so you wouldn't have gained much. You could try to use an HList[1] or some abstraction on top of it. I haven't used those in Java, yet. From my limited experience in Scala I'd say this isn't for the faint of heart, and I wouldn't expect it to be more friendly in Java - see [2]. :) Or you could bury the whole remaining computation, starting from the Supplier, in a custom data type and use this as a key type. "Inside" this type, the proper type would be known then, generically or statically. There may be other, less cumbersome approaches, depending on what you actually want to achieve. Best regards, Patrick [1] http://www.functionaljava.org/javadoc/4.8.1/functionaljava/fj/data/hlist/HList.html [2] http://www.functionaljava.org/examples-java7.html#hlistAppend
[toc] | [prev] | [next] | [standalone]
| From | Patrick Roemer <sangamon@netcologne.de> |
|---|---|
| Date | 2019-04-10 16:35 +0200 |
| Message-ID | <q8kv0c$1di$1@newsreader4.netcologne.de> |
| In reply to | #38891 |
Responding to myself: > You need a uniform key type. Value type, of course. > Or you could bury the whole remaining computation, starting from the > Supplier, in a custom data type and use this as a key type. Ditto. Best regards, Patrick
[toc] | [prev] | [next] | [standalone]
| From | Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> |
|---|---|
| Date | 2019-04-10 19:07 +0200 |
| Message-ID | <q8l7sp$o2o$1@dont-email.me> |
| In reply to | #38886 |
On 2019-04-10 07:01, mike wrote:
> Hi,
>
> I need to to different suppliers:
>
> - Supplier<String>
> - Supplier<Integer>
> - Supplier<List<String>>
>
> Map<String, Supplier> map= new HashMap<>();
>
> How can I define a map ( some similar collection ) to store these different suppliers in?
>
> When I get it from map what will be the type?
The type will be whatever you put in the map, namely, "Supplier". The
generic parameter is just sugar. Your Map should be a
Map<String, Supplier<?>>
.
You can ease calls to the class with the following method, which makes
invocations easier but doesn't give you any type safety:
class SupplierHolder {
private final Map<String, Supplier<?>> map = new HashMap<>();
public void put(String key, Supplier<?> s){
map.put(key, s);
}
@SuppressWarnings("unchecked")
public <T> Supplier<T> get(String key){ // may yield a
ClassCastException
return (Supplier<T>) map.get(key);
}
}
Thus you could call:
SupplierHolder sh = ...;
Supplier<Integer> s = sh.get("key")
without casting.
If you want type safety, the only approach I can think of involves more
complex keys. To wit:
final class Key<T> {
private final String key;
private Key(String s){
this.key = s;
}
public static final Key<String> KEY_1 = new Key<>("key_1");
public static final Key<Integer> KEY_2 = new Key<>("key_2");
public static final Key<List<String>> KEY_3 = new Key<>( "key_3" );
}
class SupplierHolder {
private final Map<Key<?>, Supplier<?>> map = new HashMap<>();
public <T> void put(Key<T> key, Supplier<? extends T> s){
map.put(key, s);
}
@SuppressWarnings("unchecked")
public <T> Supplier<T> get(Key<T> key){ //may return null, but won't
yield a ClassCastException (unless you have called #put with raw types)
return (Supplier<T>) map.get(key);
}
}
Note that in this case, the Keys have to be constants in a regular
class, and can't be in an Enum, as the Enum won't allow different type
parameters for its members. (Also, Class parameter is strictly speaking
unnecessary in this example).
(Caveat: untested code)
--
DF.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web