Created
June 3, 2017 15:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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