| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1102 人关注过本帖
标题:Java中的HashMap,为什么输出竟然是有序的?
只看楼主 加入收藏
mosquiyan
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2014-12-12
结帖率:85.71%
收藏
已结贴  问题点数:6 回复次数:1 
Java中的HashMap,为什么输出竟然是有序的?
import java.util.*;

public class MapTest {
   
    private String word;
    private String id;
    public MapTest(String id,String word){
        this.id=id;
        this.word=word;
    }
   
    public String GetWord(){
        return word;
    }
   
    public String GetId(){
        return id;
    }
   
    public static void main(String[] args) {
        Map<String,String> map=new HashMap<>();//由HashMap实现的Map对象
        MapTest a1=new MapTest("004","apple");
        MapTest a2=new MapTest("008","banana");
        MapTest a3=new MapTest("003","dog");
        MapTest a4=new MapTest("010","cat");
        MapTest a5=new MapTest("005","hello");
        MapTest a6=new MapTest("006","english");
        MapTest a7=new MapTest("009","math");
        MapTest a8=new MapTest("002","morning");
        MapTest a9=new MapTest("007","good");
        MapTest a10=new MapTest("001","excellent");
        map.put(a1.GetId(),a1.GetWord());
        map.put(a2.GetId(),a2.GetWord());
        map.put(a3.GetId(),a3.GetWord());
        map.put(a4.GetId(),a4.GetWord());
        map.put(a5.GetId(),a5.GetWord());
        map.put(a6.GetId(),a6.GetWord());
        map.put(a7.GetId(),a7.GetWord());
        map.put(a8.GetId(),a8.GetWord());
        map.put(a9.GetId(),a9.GetWord());
        map.put(a10.GetId(),a10.GetWord());
        
        Set<String> set=map.keySet();
        Iterator <String> it=set.iterator();
        System.out.println("HashMap类实现的Map集合,无序");
        while(it.hasNext()){
            String str=(String)it.next();
            String eng=(String)map.get(str);
            System.out.println(str+" "+eng);
        }
    }
}
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: public private import apple return 
2015-11-20 15:52
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:6 
HashMap类实现的Map集合,无序
010 cat
008 banana
009 math
004 apple
005 hello
006 english
007 good
001 excellent
002 morning
003 dog

剑栈风樯各苦辛,别时冰雪到时春
2015-11-20 17:28
快速回复:Java中的HashMap,为什么输出竟然是有序的?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018914 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved