Java HashMap

Java HashMap is under java.util. – Java API

1. What is HashMap?
HashMap is an Object that stores both “key/value” as a pairs. In this article, we show how to create a HashMap instance and iterates the HashMap data.
—> put(“key”, “value”);  // set the value
—> get(“key”);  // get the value

import java.util.HashMap;
import java.util.Map;
// Frank Nandong(08/05/2014)
// franknandong@gmail.com / fndong@wordpress.com
// Task : one key been pick from two keys, get the value.

public class Hasmap_one {
public static String sPickValue = “”;

public static void main(String args[]){
Map mMap = new HashMap();

// add the key and value of the dessert

mMap.put(“banana”, “icecream Banana”);
mMap.put(“apple”, “apple pie”);

// pick the apple key , to get value “apple pie”
// convert object to string by put (String) betwen mMap.get(“apple”)

sPickValue = (String)mMap.get(“apple”);

// output display

System.out.println(“I Pick apple key , the value is : ” + sPickValue);
}
}

2

Java SE (JDK) – java Util Package

Image

1. Java SE ( Java Platform – Standard Edition) Is a Platform for Development and Deployment.

2. java.util.*
Java.util.* is a package provide support for the event model, collections framework, date and time facilities,and contain various utility classes.

java.util.* package contains with numbers of useful classes and interface.

2.1. Using java.util.List

– What is Java.util.List ?
answer: List is a sub interface to Collection under java.util package.It is an ordered collection:

Example code using java.util.List with Iterator

package array;
import java.util.Iterator;
import java.util.List;import java.util.Arrays;

public class iterator_utilList1 {
// How iterator work? its same like others type of loop syntax

public static void main(String[] argv) {

// set the array value – string
String stArrays[] = new String[] {“chicken”, “Dog”, “cats”,”Duck” };

//how to convert array to list? using java.util.list package
/*** what is Arrays?

– answer:An array is a container object that holds a fixed number of values of a single type. The length of an
array is established when the array is created. After creation, its length is fixed.

******/
                List<String> listOfAnimal = Arrays.asList(stArrays);

              // What is Itterator?
                        Iterator<String> iterator = listOfAnimal.iterator();
              while (iterator.hasNext()) {
                     System.out.println(iterator.next());
                    }
          }
}

 

Output:

chicken
Dog
cats
Duck