Wednesday, January 10, 2018

Programming: HashMap

“There are situations where a Java application needs something that’s more akin to a database, without actually having all the database baggage. For example, you might want to be able to find a specific value in a list without having to look at every element individually” (Mueller 322). This quote from the book explains what is called a map. A map is an interface that helps describes a class and help store information with a value and key. A map is like a dictionary and the keys are the word and the value are the definitions.  There are many different types of maps but the one I learn is called a “HashMap”.
A “HashMap” is written out as “HashMap<Key, Value>. First, I put in “String” for key and “integer” for the value. By doing this, the string key will allow be to add text inside double quotes and the integer will allow be to add a value represented by a number. As an example, I created a new object called “food”. I then added three foods inside the key called “Pizza”, “Hotdog”, and “Hamburger”. For the value, I added the cost of each of the food. This is done by typing out the object, “food”, followed by “.put” and would look like this.

To access the data, you would type out the “System.out.println” followed by the object and then add “.get”. Say I wanted a pizza. I would type in pizza in the parenthesis and it would output the cost of the pizza. So, the code would be written out like this.

               We can also access the number of entries of the HashMap. By using the for each loop, it will run over the data input and access the key name and use the get method to access the value. By doing this, the code will be able to print out the food and the cost of each of the food. This is what the code would look like and its output.


Output
To sum it up, there are many maps which helps with accessing data that is in a program. A HashMap is one of those example as it stores information and can be access by printing out its key. What other built-in data structure in Java do you guys think is useful or easy to use?


Mueller, John Paul. Java eLearning Kit For Dummies. Wiley, 2014.

2 comments:

  1. I love this extremely detailed blog. Is there another thing that is similar to Java but is not Java that might be a little bit user friendly?

    ReplyDelete
  2. Hi Jerico, thank you for your comment. To answer your question, yes, Java is not the only language for programming. Python is another language used for programming and I would recommend it to people who are starting out how to program since it is made mainly for beginners.

    ReplyDelete