Last active
          March 9, 2023 21:21 
        
      - 
      
- 
        Save tejainece/d32cba84b747c0b2e7df to your computer and use it in GitHub Desktop. 
    Get element by index in LinkedHashMap
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | import java.util.LinkedHashMap; | |
| import java.util.Map; | |
| import java.util.Set; | |
| public class LinkedHashMapValueByIndexArray { | |
| public static void main(String []args){ | |
| LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>(); | |
| map.put("Qatar", 98814); | |
| map.put("Luxembourg", 78670); | |
| map.put("Singapore", 64584); | |
| map.put("Norway", 54947); | |
| map.put("Brunei", 53431); | |
| map.put("United States", 53101); | |
| map.put("Hong Kong", 52722); | |
| map.put("Switzerland", 46430); | |
| Set<Map.Entry<String, Integer>> mapSet = map.entrySet(); | |
| Map.Entry<String, Integer> elementAt5 = (Map.Entry<String, Integer>) mapSet.toArray()[5]; | |
| System.out.println(elementAt5.getKey()); | |
| } | |
| } | 
  
    
      This file contains hidden or 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
    
  
  
    
  | import java.util.LinkedHashMap; | |
| import java.util.Map; | |
| import java.util.Set; | |
| import java.util.ArrayList; | |
| public class LinkedHashMapValueByIndexArrayList { | |
| public static void main(String []args){ | |
| LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>(); | |
| map.put("Qatar", 98814); | |
| map.put("Luxembourg", 78670); | |
| map.put("Singapore", 64584); | |
| map.put("Norway", 54947); | |
| map.put("Brunei", 53431); | |
| map.put("United States", 53101); | |
| map.put("Hong Kong", 52722); | |
| map.put("Switzerland", 46430); | |
| Set<Map.Entry<String, Integer>> mapSet = map.entrySet(); | |
| Map.Entry<String, Integer> elementAt5 = (new ArrayList<Map.Entry<String, Integer>>(mapSet)).get(5); | |
| System.out.println(elementAt5.getKey()); | |
| } | |
| } | 
  
    
      This file contains hidden or 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
    
  
  
    
  | import java.util.LinkedHashMap; | |
| import java.util.Map; | |
| import java.util.Set; | |
| import java.util.ArrayList; | |
| import java.util.Iterator; | |
| public class LinkedHashMapValueByIndexIterate { | |
| public static void main(String []args){ | |
| LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>(); | |
| map.put("Qatar", 98814); | |
| map.put("Luxembourg", 78670); | |
| map.put("Singapore", 64584); | |
| map.put("Norway", 54947); | |
| map.put("Brunei", 53431); | |
| map.put("United States", 53101); | |
| map.put("Hong Kong", 52722); | |
| map.put("Switzerland", 46430); | |
| Set<Map.Entry<String, Integer>> mapSet = map.entrySet(); | |
| Iterator<Map.Entry<String, Integer>> ite = mapSet.iterator(); | |
| int n = 5; | |
| while(n-- > 0) { | |
| if(ite.hasNext()) { | |
| ite.next(); | |
| } | |
| } | |
| if(n == -1 && ite.hasNext()) { | |
| System.out.println(ite.next().getKey()); | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment