Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. HashMap<A, B> myMap = new HashMap<A, B>();
  2.  
  3. ...
  4. myMap.put(...)
  5. ...
  6.  
  7. Set<A> keys = myMap.keySet();
  8.  
  9. HashMap<A, B> myMap = new HashMap<A, B>();
  10.  
  11. ...
  12. myMap.put(key, value);
  13. ...
  14.  
  15. for (Entry<A, B> e : myMap.entrySet()) {
  16. A key = e.getKey();
  17. B value = e.getValue();
  18. }
  19.  
  20. //// or using an iterator:
  21.  
  22. // retrieve a set of the entries
  23. Set<Entry<A, B>> entries = myMap.entrySet();
  24. // parse the set
  25. Iterator<Entry<A, B>> it = entries.iterator();
  26. while(it.hasNext()) {
  27. Entry<A, B> e = it.next();
  28. A key = e.getKey();
  29. B value = e.getValue();
  30. }
  31.  
  32. HashMap<A, B> myMap = new HashMap<A, B>();
  33.  
  34. ...
  35. myMap.put(key, value);
  36. ...
  37.  
  38. for (A key : myMap.keySet()) {
  39. B value = myMap.get(key); //get() is less efficient
  40. } //than above e.getValue()
  41.  
  42. // for parsing using a Set.iterator see example above
  43.  
  44. HashMap<A, B> myMap = new HashMap<A, B>();
  45.  
  46. ...
  47. myMap.put(key, value);
  48. ...
  49.  
  50. for (B value : myMap.values()) {
  51. ...
  52. }
  53.  
  54. //// or using an iterator:
  55.  
  56. // retrieve a collection of the values (type B)
  57. Collection<B> c = myMap.values();
  58. // parse the collection
  59. Iterator<B> it = c.iterator();
  60. while(it.hasNext())
  61. B value = it.next();
  62. }
  63.  
  64. Set<A> keys = myMap.keySet();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement