Advertisement
JeffGrigg

Java AbstractMap toString method

Aug 12th, 2018
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. package java.util;
  2.  
  3. public abstract class AbstractMap<K,V> implements Map<K,V> {
  4.  
  5. /**
  6. * Returns a string representation of this map. The string representation
  7. * consists of a list of key-value mappings in the order returned by the
  8. * map's <tt>entrySet</tt> view's iterator, enclosed in braces
  9. * (<tt>"{}"</tt>). Adjacent mappings are separated by the characters
  10. * <tt>", "</tt> (comma and space). Each key-value mapping is rendered as
  11. * the key followed by an equals sign (<tt>"="</tt>) followed by the
  12. * associated value. Keys and values are converted to strings as by
  13. * {@link String#valueOf(Object)}.
  14. *
  15. * @return a string representation of this map
  16. */
  17. public String toString() {
  18. Iterator<Entry<K,V>> i = entrySet().iterator();
  19. if (! i.hasNext())
  20. return "{}";
  21.  
  22. StringBuilder sb = new StringBuilder();
  23. sb.append('{');
  24. for (;;) {
  25. Entry<K,V> e = i.next();
  26. K key = e.getKey();
  27. V value = e.getValue();
  28. sb.append(key == this ? "(this Map)" : key);
  29. sb.append('=');
  30. sb.append(value == this ? "(this Map)" : value);
  31. if (! i.hasNext())
  32. return sb.append('}').toString();
  33. sb.append(',').append(' ');
  34. }
  35. }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement