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)); }
上面存储的数为什么到下面就没了,求指点
map.put 使用了同一个键,后面的就把前面的覆盖了
我debug也发现了,本想把前面的键去除,但还是不行
@ycty: 本想把前面的键去除?什么意思?map默认会覆盖相同的键的啊!
@peislin: map就是存键值对的,键必须唯一,如果不想唯一,那使用其他的数据结构
既然覆盖相同键的值,那楼主这样肯定不行