Java Map to String mapToString(Map m)

Here you can find the source of mapToString(Map m)

Description

map To String

License

Open Source License

Declaration

public static <K, V> String mapToString(Map<K, V> m) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;
import java.util.Map.Entry;

public class Main {
    public static <K, V> String mapToString(Map<K, V> m) {
        if (m == null)
            return "null";
        Set<Entry<K, V>> set = m.entrySet();
        int max = set.size() - 1;
        if (max == -1)
            return "{}";

        StringBuilder sb = new StringBuilder();
        Iterator<Entry<K, V>> it = set.iterator();

        sb.append('{');
        for (int i = 0;; i++) {
            Entry<K, V> e = it.next();
            K key = e.getKey();// ww  w.  j a va2s  . c om
            V value = e.getValue();
            sb.append(key == m ? "(this Map)" : (key == null ? "null" : key.toString()));
            sb.append('=');
            sb.append(value == m ? "(this Map)" : (value == null ? "null" : value.toString()));

            if (i == max)
                return sb.append('}').toString();
            sb.append(", ");
        }

    }

    public static String toString(Object o) {
        return String.valueOf(o);
    }
}

Related

  1. mapToString(Map map)
  2. mapToString(Map map)
  3. mapToString(Map map)
  4. mapToString(Map map)
  5. mapToString(Map acs)
  6. mapToString(Map map)
  7. mapToString(Map criteria)
  8. MapToString(Map map)
  9. mapToString(Map map)