简体   繁体   中英

Is there any standard 'gettable' Java interface?

I need to expose few containers to different component and what I actually need here is just:

interface Gettable {
    public String get(String key);
}

Is there any standard Java (SE) interface as close to this as it is possible to not introduce yet another one but limit exposed container behavior to needed minimum?

UPDATE: Selected solution is custom interface. I really'd like to keep it as generic as it is possible.

There is nothing wrong with another custom interface.

public interface Gettable<K, V> {
    public V get(K key);
}

I think the closest standard interface there is, is java.util.Map . Note that many methods of Map are optional (may thorw an UnsupportedOperationException).

Guava is almost like a Java standard, and they have Function ( javadoc ) which has the signature you are looking for

T apply(F input)

Returns the result of applying this function to input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM