Skip to content

Instantly share code, notes, and snippets.

@anil477
Created June 3, 2017 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anil477/31b1bd0d07fe06fb597260f2fdd5ae8a to your computer and use it in GitHub Desktop.
Save anil477/31b1bd0d07fe06fb597260f2fdd5ae8a to your computer and use it in GitHub Desktop.
Java HashMap basic operations - get value, update value, contains value and loop through hashmap
// Java HashMap basic operations - get value, update value, contains value and loop through hashmap
import java.util.*;
class demo
{
public static void main(String[] args)
{
Integer[] a = new Integer[]{1,2,3,1,2,4,5,6,2,3};
Map<Integer, Integer> dataSet = new HashMap<Integer, Integer>();
int length = a.length;
int value = 0;
for(int i=0; i<length; i++)
{
if (dataSet.containsKey(a[i]))
{
value = dataSet.get(a[i]);
value++;
dataSet.put(a[i], value);
}
else
{
dataSet.put(a[i], 1);
}
}
for (Map.Entry<Integer,Integer> entry: dataSet.entrySet())
{
System.out.println(" Key " + entry.getKey() + " Value " + entry.getValue() + " ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment