This commit is contained in:
akeshri 2020-09-08 21:14:28 +05:30
parent b3c1df2bbc
commit 64b7dec1b0
3 changed files with 0 additions and 77 deletions

View File

@ -1,4 +0,0 @@
# HashMap - Getting First Key And Value
A example on how to get first key and value from HashMap.
- JDK 1.8 and above

View File

@ -1,36 +0,0 @@
package com.baeldung.article;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* @author AshwiniKeshri
*
*/
public class HashMapExample {
public static void main(String[] args) {
Map<Integer, String> hashmap = new HashMap<>();
hashmap.put(5, "A");
hashmap.put(1, "B");
hashmap.put(2, "C");
// hashmap.put(0, "D");
System.out.println(hashmap);
Set<Map.Entry<Integer, String>> entrySet = hashmap.entrySet();
Iterator<Map.Entry<Integer, String>> iterator = entrySet.iterator();
if (iterator.hasNext())
System.out.println("Using Iteraor: " + iterator.next());
System.out.println("Using Stream: " + entrySet.stream()
.findFirst()
.orElse(new AbstractMap.SimpleEntry<Integer, String>(-1, "DEFAULT")));
}
}

View File

@ -1,37 +0,0 @@
package com.baeldung.article;
import java.util.AbstractMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/**
* @author AshwiniKeshri
*
*/
public class LinkedHashMapExample {
public static void main(String[] args) {
LinkedHashMap<Integer, String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put(5, "A");
linkedHashMap.put(1, "B");
linkedHashMap.put(2, "C");
// linkedHashMap.put(0, "D");
System.out.println(linkedHashMap);
Set<Map.Entry<Integer, String>> entrySet = linkedHashMap.entrySet();
Iterator<Map.Entry<Integer, String>> iterator = entrySet.iterator();
if (iterator.hasNext())
System.out.println("Using Iterator: " + iterator.next());
System.out.println("Using Stream: " + entrySet.stream()
.findFirst()
.orElse(new AbstractMap.SimpleEntry<Integer, String>(0, "DEFAULT")));
}
}