Created
August 19, 2013 14:21
-
-
Save elaatifi/6269684 to your computer and use it in GitHub Desktop.
Convert a List to a Map using Orika
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
import ma.glasnost.orika.MapperFactory; | |
import ma.glasnost.orika.impl.ConfigurableMapper; | |
public class SO1 { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
ConfigurableMapper mapper = new ConfigurableMapper() { | |
@Override | |
protected void configure(MapperFactory factory) { | |
factory.classMap(Source.class, Destination.class) | |
.field("pairs{theKey}", "values{key}") | |
.field("pairs{theValue}", "values{value}") | |
.byDefault() | |
.register(); | |
} | |
}; | |
Source source = new Source().add("age", "20").add("name", "Lucas"); | |
System.out.println(mapper.map(source, Destination.class)); | |
} | |
public static class Pair { | |
public String theKey; | |
public String theValue; | |
} | |
public static class Source { | |
public List<Pair> pairs = new ArrayList<SO1.Pair>(); | |
public Source add(String key, String value) { | |
final Pair e = new Pair(); | |
e.theKey = key; | |
e.theValue = value; | |
pairs.add(e); | |
return this; | |
} | |
} | |
public static class Destination { | |
public Map<String, String> values; | |
@Override | |
public String toString() { | |
return "Destination [values=" + values + "]"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment