This commit is contained in:
akeshri 2020-08-29 21:02:52 +05:30
parent b59f4d32b4
commit eab67c04b5
2 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,36 @@
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

@ -0,0 +1,37 @@
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")));
}
}