Skip to Content
Jakarta Commons Cookbook
book

Jakarta Commons Cookbook

by Timothy M. O'Brien
November 2004
Intermediate to advanced
400 pages
10h 31m
English
O'Reilly Media, Inc.
Content preview from Jakarta Commons Cookbook

1.12. Creating a Map from a Multidimensional Array

Problem

You need to create a Map from a multidimensional array.

Solution

Use ArrayUtils.toMap() to create a Map from a two-dimensional array (Object[][]). Example 1-8 demonstrates the creation of such a Map. Example 1-8 takes an Object[][] representing atomic symbols and atomic weights and turns it into a Map retrieving the atomic weight for hydrogen.

Example 1-8. Creating a Map from an Object[ ][ ]

import org.apache.commons.lang.ArrayUtils;

Object[] weightArray = 
    new Object[][] { {"H" , new Double( 1.007)},
                     {"He", new Double( 4.002)},
                     {"Li", new Double( 6.941)},
                     {"Be", new Double( 9.012)},
                     {"B",  new Double(10.811)},
                     {"C",  new Double(12.010)},
                     {"N",  new Double(14.007)},
                     {"O",  new Double(15.999)},
                     {"F",  new Double(18.998)},
                     {"Ne", new Double(20.180)} };

// Create a Map mapping colors.
Map weights = ArrayUtils.toMap( weightArray );

Double hydrogenWeight = map.get( "H" );

Discussion

Instead of calling weights.put( ) for each entry in the Map, an Object[][] is created and then passed to ArrayUtils.toMap() . The toMap() method then extracts the first item in each array as the key and the second item as the value. This is a simple way to quickly create a Map in a piece of code; the alternative to using ArrayUtils.toMap( ) is to simply create a Map and repeatedly call the put( ) method.

See Also

In the previous example, Double objects are added to a Map, and values are retrieved from the Map using get( ). Commons Collections contains utility ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Spring Boot 2.0 Projects

Spring Boot 2.0 Projects

Mohamed Shazin Sadakath
RESTful Java Web Services - Third Edition

RESTful Java Web Services - Third Edition

Balachandar Bogunuva Mohanram
Back-end Performance

Back-end Performance

Bruno Skvorc, Christopher Pitt, Tonino Jankov, Reza Lavaryan, Daniel Berman

Publisher Resources

ISBN: 059600706XCatalog PageErrata