Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38895
| From | Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Store Suppliers in a map |
| Date | 2019-04-10 19:07 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <q8l7sp$o2o$1@dont-email.me> (permalink) |
| References | <88a3b286-0d96-425e-ac82-c02e3ebcc84a@googlegroups.com> |
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.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread
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
csiph-web