Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class EZMap<T> {
public static void main(String[] args) {
Map<String,Object> m = hashMap(
bob -> 5,
TheGimp -> 8,
incredibleKoolAid -> "James Taylor",
heyArnold -> new Date()
);
System.out.println(m);
}
private final Map<String,T> map;
private EZMap(Map<String,T> _map, Function<Object,T>[] entries) {
map=_map;
for( Function<Object,T> entry: entries ) {
final Method m;
try {
m = entry.getClass().getDeclaredMethod("apply", Object.class);
} catch (NoSuchMethodException nsme ) { throw new RuntimeException(nsme); }
final Parameter p = m.getParameters()[0];
final String key = p.getName();
final T value = entry.apply(null);
map.put(key,value);
}
}
public static <R> Map<String,R> hashMap(Function<Object, R>... entries) {
return new EZMap<R>(new HashMap<>(), entries).map;
}
public static <R> Map<String,R> treeMap(Function<Object, R>... entries) {
return new EZMap<R>(new TreeMap<>(), entries).map;
}
}
@arnehormann

This comment has been minimized.

Copy link

commented Apr 16, 2014

Your code is pretty similar to my all time favourite support function for simple array creation, so I'll just leave this here:

public static <T> T[] array(T...entries) {
    return entries;
}
@briangoetz

This comment has been minimized.

Copy link

commented Apr 19, 2014

With what java compiler does this actually work?

@okram

This comment has been minimized.

Copy link

commented May 12, 2014

This only works in Java8 which supports lambdas.

@jagwire

This comment has been minimized.

Copy link

commented Jun 24, 2014

For some reason I don't get my parameter names, I get "arg0" instead.

@janeklb

This comment has been minimized.

Copy link

commented Nov 5, 2014

Great idea - but I'm having the same arg0 problem as @jagwire - see discussion on reddit: http://www.reddit.com/r/java/comments/2360is/pretty_map_literals_for_java_8/

@oallouch

This comment has been minimized.

Copy link

commented Dec 2, 2014

Pretty, but expensive.

@yutaodou

This comment has been minimized.

Copy link

commented Dec 26, 2014

The trick is how to get the key from the lambda, looks good.

@MeritCampus

This comment has been minimized.

Copy link

commented Feb 18, 2016

You can clear many of your doubts regarding boolean Literals in Java through Merit Campus, visit: http://java.meritcampus.com/core-java-topics/data-types-in-java, http://java.meritcampus.com/core-java-topics/literals-in-java

Not only data types, we also have each and every topic in Core Java with example for each. You can read lot of sessions and can write many practice tests in Merit Campus Java website. visit: http://java.meritcampus.com/core-java-topics/ to know more.

@henrik242

This comment has been minimized.

Copy link

commented Feb 8, 2017

Not bad! Here's a less evil, more compact, but slightly less pretty attempt: http://stackoverflow.com/a/39510693/13365

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.