First Month as a Mentee: Using a HashMap in Java

First Month as a Mentee: Using a HashMap in Java

·

4 min read

A Month and Counting...

I love to understand the details of whatever I want to create, it makes it easier for me to work towards the big picture.

Programming is not an exception, I was uncomfortable with the endless loop of being stuck at a particular stage while working on projects.

So, I looked up resources to help me with it. In the course of doing that, I came across She Code Africa.

SCA is a non-profit organisation that's focused on celebrating and empowering young girls and women in tech across Africa.

The recruitment for their 2nd Cohort of mentees was closed at that time, but I joined the community with the hope of applying for the next cohort.

I applied for the Java track when the application for the 3rd Cohort was open. Thankfully, it was approved and the journey has been fulfilling.

app.png

The guidance from a professional has been helpful. And the need to turn in assignments at a specific time pushes me to go the extra mile.

It's important to understand the basics of things and I'm glad that I've been able to do that.

So, in the light of getting the basics right, I will be discussing an aspect of Java Collections framework - Using a HashMap to store Random Numbers.

Introduction

HashMap implements the Map interface and it stores elements in key and value pair. Each element has it's unique key that can be used to access it. It's part of the java.util package.

Here's a simple example of how HashMap works:

import java.util.HashMap; //you import the package 

HashMap<Integer, String> someThing = new HashMap<Integer, String>();

In the above example the key is an int and the value is a String.

HashMap Methods

Let's store some chemical elements and their atomic numbers in a HashMap using the put() method; Chlorine - 17, Zinc - 30, Vanadium - 23

import java.util.HashMap;

public class Elements {
    public static void main (String[] args) {

      //an HashMap called chemElements
      HashMap<String, Integer> chemElemMap = new HashMap<String, Integer>(); 

      //the keys and values i.e (atomic nos, elements) 
      chemElements.put(30, "Zinc"); 
      chemElements.put(17, "Chlorine");
      chemElements.put(23, "Vanadium");
        }
     }

Having done that, to access an element you use the get() method. To access Chlorine you use it's key as earlier stated.

chemElements.get(17); //this prints Chlorine

Also, you may decide to delete an element and you can do that with the remove() method.

chemElements.remove(17); //this removes Chlorine from the list.

And to get the size of a HashMap or to delete its content, you use the size() and clear() method respectively.

HashMap for Random Numbers

It's a bit different from the Object.put() method. In this case, you want to assign keys to numbers that are not fixed, that is, you don't know the values.

To do that, you use the forEach() method, it's just as simple as it's name - for every number that is generated after the program is compiled, a key is automatically assigned to it.

import.java.util.HashMap;

HashMap<Integer, Integer> numMap = new HashMap<Integer, Integer>();
    /*uses the forEach() that takes in the input(numbers)
    from the 'numList' object*/
       numList.forEach((number) -> numMap.put();

So, let's generate five random numbers and assign keys to each of them.

import java.util.*;

public class Main {
  public static void main (String[] args) {
    /*sets a new object to class Random*/
    Random rand = new Random();
    ArrayList<Integer> numList = new ArrayList<>(); 

    /*generates 5 random nos from 1 up to 9*/
    while(numList.size() < 5){
      numList.add(rand.nextInt(10)); 
    }

    HashMap<Integer, Integer> numMap = new HashMap<Integer, Integer>();
    /*a key that is twice each number is assigned*/
    numList.forEach((number) -> numMap.put(number*2,number));

    System.out.println("The list of nos: " + numList); /*prints out the list of nos*/
    System.out.println("\n");
    System.out.println("key = value: " + numMap); /*prints out the keys and values*/

  }

}

After the code is compiled, the result looks like this:

COMPILED.png

Note: HashMap doesn't maintain an order, hence the unordered list of outputs.

I would be sharing more articles on Java here. See you soon. :)