1. Using Java 8 Stream API
2. Using ArrayList
3. Using Arrays
4. Using if-else statements
Read Also: Get the first and last elements of the Set or HashSet in Java
Let's dive deep into the topic:
Get the first and last entries in a Map or HashMap in Java
1. Using Stream API
We can easily get the first and last entries in a Map or HashMap in Java using Stream API. Just like Set or HashSet, we can get the first entry in a Map or HashMap using the findFirst() method that returns Optional<T>. Invoke the get() method on Optional<T> to obtain the final result.
Just like HashSet or Set, we can get the last entry in a Map or HashMap using reduce() method that returns Optional<T>. You have to invoke the get() method on Optional<T> to obtain the final result.
import java.util.Map; import java.util.HashMap; public class FirstLastEntryMap { public static void main(String args[]) { Map.Entry<Integer,String> firstEntry = null; Map.Entry<Integer,String> lastEntry = null; Map<Integer,String> map = new HashMap<>(); map.put(1, "Table"); map.put(2, "Chair"); map.put(3, "Lamp"); map.put(4, "Clock"); System.out.println("Original Map entries are: "+ map); // Get first entry in HashMap firstEntry = map.entrySet().stream().findFirst().get(); // Get last entry in HashMap lastEntry = map.entrySet().stream().reduce((first,second) -> second).get(); System.out.println("First entry is: " + firstEntry); System.out.println("Last entry is: " + lastEntry); } }
Output:
Original Map entries are: {1=Table, 2=Chair, 3=Lamp, 4=Clock}
First entry is: 1=Table
Last entry is: 4=Clock
2. Using ArrayList
You can also get the first and last entries in a Map or HashMap in Java using ArrayList. First, we will use the keySet() method to fetch all the keys present in the map and store them in the ArrayList. Then we will use the get() method to fetch the first entry of the map by passing the 0th index.
Similarly, we will use the get() method to fetch the last entry of the map by passing list.size()-1 as shown below in the example:
import java.util.Map; import java.util.HashMap; import java.util.List; import java.util.ArrayList; public class FirstLastEntryMap2 { public static void main(String args[]) { String firstEntry = null; String lastEntry = null; Map<Integer,String> map = new HashMap<>(); map.put(1, "Mobile"); map.put(2, "Laptop"); map.put(3, "Tablet"); map.put(4, "iPod"); System.out.println("Original Map entries are: "+ map); List<Integer> list = new ArrayList<>(map.keySet()); if (list.size() > 0 && !list.isEmpty()) { // Get first entry in HashMap firstEntry = list.get(0) + "=" + map.get(list.get(0)); // Get last entry in HashMap lastEntry = list.get(list.size()-1) + "=" + map.get(list.get(list.size()-1)); } System.out.println("First entry is: " + firstEntry); System.out.println("Last entry is: " + lastEntry); } }
Output:
Original Map entries are: {1=Mobile, 2=Laptop, 3=Tablet, 4=iPod}
First entry is: 1=Mobile
Last entry is: 4=iPod
3. Using Arrays
Using Arrays is another way to get the first and last entries in a Map or HashMap in Java. We have stored the keys of the map in the Integer array. As we all know that we can access elements of an array directly by using the index. To get the first entry we need to pass the 0th index whereas to get the last entry we need to pass arr.length-1 in the index as shown below in the example:
import java.util.Map; import java.util.HashMap; public class FirstLastEntryMap3 { public static void main(String args[]) { String firstEntry = null; String lastEntry = null; Map<Integer,String> map = new HashMap<>(); map.put(1, "Test cricket"); map.put(2, "One day international"); map.put(3, "Twenty 20"); map.put(4, "Hundred"); System.out.println("Original Map entries are: "+ map); Integer[] arr = map.keySet().toArray(new Integer[map.size()]); if (arr != null && arr.length > 0) { // Get first entry in HashMap firstEntry = arr[0] + "="+ map.get(arr[0]); // Get last entry in HashMap lastEntry = arr[arr.length-1] + "=" + map.get(arr[arr.length-1]); } System.out.println("First entry is: " + firstEntry); System.out.println("Last entry is: " + lastEntry); } }
Output:
Original Map entries are: {1=Test cricket, 2=One day international, 3=Twenty 20, 4=Hundred}
First entry is: 1=Test cricket
Last entry is: 4=Hundred
4. Using if-else statements
We can easily get first and last entries in a Map or HashMap in Java using if-else statements as shown below in the example:
import java.util.Map; import java.util.HashMap; public class FirstLastEntryMap4 { public static void main(String args[]) { Map.Entry<Integer,String> firstEntry = null; Map.Entry<Integer,String> lastEntry = null; Map<Integer,String> map = new HashMap<>(); map.put(1, "Water"); map.put(2, "Fire"); map.put(3, "Earth"); System.out.println("Original Map entries are: "+ map); for (Map.Entry<Integer,String> element: map.entrySet()) { // Get first entry in HashMap if (firstEntry == null) firstEntry = element; // Get last entry in HashMap lastEntry = element; } System.out.println("First entry is: " + firstEntry); System.out.println("Last entry is: " + lastEntry); } }
Output:
Original Map entries are: {1=Water, 2=Fire, 3=Earth}
First entry is: 1=Water
Last entry is: 3=Earth
That's all for today. Please mention in the comments if you have any questions related to how to get the first and last entries in a Map or HashMap in Java with examples.