How do I convert an array to a Map?
Category: Commons Lang, viewed: 1727 time(s).
This example use the Apache commons-lang's ArrayUtils class to convert a two dimensional array to a Map object.
package org.kodejava.example.commons.lang; import java.util.Map; import org.apache.commons.lang.ArrayUtils; public class ArrayToMapExample { public static void main(String[] args) { // // A two dimensional array of countries capital. // String[][] countries = {{"United States", "New York"}, {"United Kingdom", "London"}, {"Netherland", "Amsterdam"}, {"Japan", "Tokyo"}, {"France", "Paris"}}; // // To convert an array to a Map each array elements must be an array with // at least to element where the first element will be the key and the // second element will be the value. // Map countryCapitals = ArrayUtils.toMap(countries); System.out.println("Capital of Japan is " + countryCapitals.get("Japan")); System.out.println("Capital of France is " + countryCapitals.get("France")); } } |
Can't find what you are looking for? Join our FORUMS and ask some questions!
Related Examples
- How do I find text between two strings?
- How do I check for an empty string?
- How do I get the nearest hour, minute, second of a date?
- How do I format date and time using DateFormatUtils class?
- How do I find items in an array?
- How do I use CompareToBuilder class?
- How do I use ReflectionToStringBuilder class?
- How do I convert array of object to array of primitive?
- How do I reverse array elements order?
- How do I count word occurrences in a string?