?
? ? 1. 编写一个单元测试类
? ? ? ??
? ? ? ??
class="java"> @Test
    public void tt() {
        Map<Object, Object> map = new HashMap<>();
        map.put("11", "aaaa");
        map.put("22", "bbbb");
        map.put("33", "cccc");
        map.put("44", "dddd");
        System.out.println(map);
        System.out.println("--------------- 改装map.toString方法测试 start ---------------");
        long btime1 = System.currentTimeMillis();
        String str = toString2(map);
        System.out.println(str);
        long etime1 = System.currentTimeMillis();
        System.out.println("共花费了: "+(etime1-btime1) + " milliseconds");
        System.out.println("--------------- 改装map.toString方法测试 end ---------------");
        System.out.println("--------------- map.toString 源码方法测试 start ---------------");
        long etime2 = System.currentTimeMillis();
        String str2 = toString3(map);
        System.out.println(str2);
        long btime2 = System.currentTimeMillis();
        System.out.println("共花费了: "+(etime2-btime2) + " milliseconds");
        System.out.println("--------------- map.toString 源码方法测试 end ---------------");
    }
?
?
? 2.通过源码我们找到了代码实现(如下:)
?
??
?
? ?通过源码解读,我们可以知道,map.toString()方法主要是
?
? ? 通过map的迭代器判断集合是否有值,如果没有值就直接返回,如果有值我们就通过for(;;) 死循环 和?
?
? ?StringBuilder (不需要考虑线程安全问题, 并且考虑到效率,所以使用StringBuilder 不使用StringBuffer)
?
? 字符串缓冲区的拼接,最后加一个判断,当游标遍历到最后一个说明没有值了,直接返回字符串数据.
?
? 4 思考:于是看到这里,我们就去想源码拿出来 ? ?再把for(;;) 死循环 换成 while(true)进行测试
?
(最终代码实现)
?
??
 @Test
    public void tt() {
        Map<Object, Object> map = new HashMap<>();
        map.put("11", "aaaa");
        map.put("22", "bbbb");
        map.put("33", "cccc");
        map.put("44", "dddd");
        System.out.println(map);
        System.out.println("--------------- 改装map.toString方法测试 start ---------------");
        long btime1 = System.currentTimeMillis();
        String str = toString2(map);
        System.out.println(str);
        long etime1 = System.currentTimeMillis();
        System.out.println("共花费了: "+(etime1-btime1) + " milliseconds");
        System.out.println("--------------- 改装map.toString方法测试 end ---------------");
        System.out.println("--------------- map.toString 源码方法测试 start ---------------");
        long etime2 = System.currentTimeMillis();
        String str2 = toString3(map);
        System.out.println(str2);
        long btime2 = System.currentTimeMillis();
        System.out.println("共花费了: "+(etime2-btime2) + " milliseconds");
        System.out.println("--------------- map.toString 源码方法测试 end ---------------");
    }
    public String toString2(Map<Object, Object> map) {
        Iterator<Map.Entry<Object, Object>> i = map.entrySet().iterator();
        if (!i.hasNext()) return "{}";
        StringBuilder sb = new StringBuilder();
        sb.append('{');
        while (true) {
            Map.Entry<Object, Object> e = i.next();
            Object key = e.getKey();
            Object value = e.getValue();
            sb.append(key == this ? "(this Map)" : key);
            sb.append('=');
            sb.append(value == this ? "(this Map)" : value);
            if (!i.hasNext()) return sb.append('}').toString();
            sb.append(',').append(' ');
        }
    }
    /**
     * @param map
     * @return
     */
    public String toString3(Map<Object, Object> map) {
        Iterator<Map.Entry<Object, Object>> i = map.entrySet().iterator();
        if (!i.hasNext())
            return "{}";
        StringBuilder sb = new StringBuilder();
        sb.append('{');
        for (; ; ) {
            Map.Entry<Object, Object> e = i.next();
            Object key = e.getKey();
            Object value = e.getValue();
            sb.append(key == this ? "(this Map)" : key);
            sb.append('=');
            sb.append(value == this ? "(this Map)" : value);
            if (!i.hasNext())
                return sb.append('}').toString();
            sb.append(',').append(' ');
        }
    }
?
? 5.测试结果如下:
?
??
?
?
? ?
?
?
?
??
?
? ?测试结果:失败 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ?总结: ?(1) map.toString ?是通过 迭代器 ?和 StringBuilder(字符串缓冲区) ?和 ?for(;;) 死循环实现的;
? ?
? ? ? ? ? ? ? (2) for(;;) 死循环的效率 比 ?white(true) 死循环的效率高;
?
?
?
?
?
?
?
?
?
?
?
?
?
?
??
?
? ?
?
? ?
?