首页 新闻 赞助 找找看

为什么这样提前放在map里面的属没了

0
悬赏园豆:10 [已关闭问题] 关闭于 2012-12-02 18:06
Map map = new HashMap();
        List list = new ArrayList();

        Session session = HibernateSessionFactory.getSession();
        Connection connection = session.connection();
        PreparedStatement statement = null;
        ResultSet set = null;
        Imgpath imgpath = null;
        Keyword keyword = null;

        String sql = "select pathname,picturename,turnurl from pfb.dbo.Imgpath where justone = '"
                + justone + "'";
        System.out.println(sql);
        try {
            set = connection.prepareStatement(sql).executeQuery();
            while (set.next()) {
                imgpath = new Imgpath();
                imgpath.setPathname(set.getString("pathname"));
                imgpath.setPicturename(set.getString("picturename"));
                imgpath.setTurnurl(set.getString("turnurl"));
                list.add(imgpath);
                map.put("Imgpath", list);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        sql = "select keyword,picturename,turnurl from pfb.dbo.Keyword where justone = '"
                + justone + "'";
        try {
            set = connection.prepareStatement(sql).executeQuery();
            while (set.next()) {
                keyword = new Keyword();
                keyword.setKeyword(set.getString("keyword"));
                keyword.setPicturename(set.getString("picturename"));
                keyword.setTurnurl(set.getString("turnurl"));
                //list.clear();
                //list.add(keyword);
                map.put("Keyword", list.add(keyword));
            }

上面存储的数为什么到下面就没了,求指点

ycty的主页 ycty | 初学一级 | 园豆:112
提问于:2012-12-01 15:33
< >
分享
所有回答(2)
0
map.put 使用了同一个键,后面的就把前面的覆盖了
2012 | 园豆:21228 (高人七级) | 2012-12-01 17:50

我debug也发现了,本想把前面的键去除,但还是不行

支持(0) 反对(0) ycty | 园豆:112 (初学一级) | 2012-12-01 18:04

@ycty: 本想把前面的键去除?什么意思?map默认会覆盖相同的键的啊!

支持(0) 反对(0) Vincent.Pei | 园豆:156 (初学一级) | 2012-12-02 16:28

@peislin: map就是存键值对的,键必须唯一,如果不想唯一,那使用其他的数据结构

支持(0) 反对(0) 2012 | 园豆:21228 (高人七级) | 2012-12-03 08:25
0

既然覆盖相同键的值,那楼主这样肯定不行

chenping2008 | 园豆:9836 (大侠五级) | 2012-12-02 16:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册