Java: Associative Arrays

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Adding to Arrays in Java

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Associative Array
  • 0:50 Maps to the Rescue!
  • 2:25 Adding Values &…
  • 3:34 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

It's maps to the rescue! Java doesn't technically support an associative array, but Java does provide a Map class for that purpose. This lesson will define the concept and provide working code examples for working with this powerful approach to arrays.

Associative Array

In most arrays, a common way to reference an item in the array is to state the index. For example, the fifth bucket of an array of plates would look like this one:


Basic array reference


What if we have an array of names, called directory, and we don't know the index? Let's say we want to find Jane Austen. The associative array lets us do the following thing:


Associative array concept


Unfortunately, Java won't let us create an associative array like this. An associative array takes a lot of overhead to support looking up the values. Other languages support the concept of operator overloading, which is what makes associative arrays possible. Java requires an index in an array, and won't let you specify the value instead. All is not lost, however: Java give us a map to solve this problem.

Maps to the Rescue!

Java provides a Map (also called HashMap) class, which is a type of array. Instead of referencing indexes (for example 1, 2, or 5), we reference the objects of the array. In that sense, it's an alternative to the associative array.

We can add items to the array using the put method, and pull items using get. In order to display all values, use the keySet function. Objects are removed from the map with the remove function. In order to discover the size of the array, use the size function (comparable to the length function in other arrays). With a map we can associate (link) a value with a key (the value in the data).

The very first thing we need to do is import the utilities that allow use of the map. For this, we will include the Map and HashMap utilities. In order to import a utility, use the import function at the top of your code like this one appearing here:


Java import maps util


Next, let's declare our map. We'll be creating an employee directory, which includes employee name and desk/mobile phone number. In this example, the employee name is the key to the actual value. The key could be Victor Hugo, and the value is 5558888 for his mobile number. In a sense, we've created our own version of an associative array.

Creating the map is much like declaring a new object or array, since we use the new keyword to create a new instance of the Map class. This class has its own methods that help us work with the members of the map.

As we progress through the lesson, we will be building upon the original code.


Java Create Map


Adding Values & Determining Size

Now that we have declared the map, we can add values to it. Adding values is done through the put command, which is part of the Map class we are using.


Java map put


To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support