我是靠谱客的博主 文艺鸭子,最近开发中收集的这篇文章主要介绍Map,List 迭代器的使用,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

    public HashMap<String,User> selectUser(){
        String sql = "select u.*, d.* from user u , dept d where u.udid = d.id order by u.uid asc";
        ResultSet rs = my.selectAction(sql, null);
        HashMap<String,User> list = new HashMap<String,User>();
        try {
            while (rs.next()){
                User user = new User(rs.getInt("uid"),rs.getString("name"),rs.getInt("password"),rs.getString("gender"),rs.getString("interest"),rs.getString("address"),rs.getString("area"),rs.getInt("udid"));
                Dept d = new Dept(rs.getInt("id"),rs.getString("d_name"));
                user.setDept(d);
                String string = user.toString();
                list.put(string,user);
            }
            return list;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    public static void main(String[] args) {
        UserControl uc = new UserControl();
        HashMap<String,User> map = new HashMap<String,User>();
        map = uc.selectUser();
        Iterator<String> KeyIt = map.keySet().iterator();
        while (KeyIt.hasNext()) {
            String string = KeyIt.next();
            User user = map.get(string);
            System.out.println("String:"+user.getName()+"/"+user.getId());
        }
    }
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


    public HashMap<String,List<User>> selectUser(){
        String sql = "select u.*, d.* from user u , dept d where u.udid = d.id order by u.uid asc";
        ResultSet rs = my.selectAction(sql, null);
        List<User> list = new ArrayList<User>();
        HashMap<String,List<User>> map = new HashMap<String,List<User>>(); 
        try {
            while (rs.next()){
                User user = new User(rs.getInt("uid"),rs.getString("name"),rs.getInt("password"),rs.getString("gender"),rs.getString("interest"),rs.getString("address"),rs.getString("area"),rs.getInt("udid"));
                Dept d = new Dept(rs.getInt("id"),rs.getString("d_name"));
                user.setDept(d);
                list.add(user);  
            }
             map.put("a",list);
            return map;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    public static void main(String[] args) {
        UserControl uc = new UserControl();
        HashMap<String,List<User>> map = new HashMap<String,List<User>>();
        map = uc.selectUser();
        Iterator<String> string = map.keySet().iterator();
        while (string.hasNext()) {
            String string2 = string.next();
            List<User> user = map.get(string2);
            Iterator<User> list = user.iterator();
            while (list.hasNext()) {
                User user2 =list.next();
                System.out.println(user2.getId()+"/"+user2.getName());
            }
        }

    }
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    public List<HashMap<String,User>> selectUser(){
        String sql = "select u.*, d.* from user u , dept d where u.udid = d.id order by u.uid asc";
        ResultSet rs = my.selectAction(sql, null);
        List<HashMap<String,User>> list = new ArrayList<HashMap<String,User>>();
        HashMap<String,User> map = new HashMap<String,User>(); 
        try {
            while (rs.next()){
                User user = new User(rs.getInt("uid"),rs.getString("name"),rs.getInt("password"),rs.getString("gender"),rs.getString("interest"),rs.getString("address"),rs.getString("area"),rs.getInt("udid"));
                Dept d = new Dept(rs.getInt("id"),rs.getString("d_name"));
                user.setDept(d);
                map.put(user.getId()+user.getName(), user);         
                }
            list.add(map);
            return list;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    public static void main(String[] args) {
        UserControl uc = new UserControl();
        List<HashMap<String,User>> list = uc.selectUser();
        Iterator<HashMap<String,User>> map = list.iterator();
        while (map.hasNext()) {
            HashMap<String,User> hashMap = (HashMap<String,User>) map.next();
            Iterator<String> string = hashMap.keySet().iterator();
            System.out.println("string:"+hashMap.size());
            while (string.hasNext()) {
                String string2 = string.next();
                User user = hashMap.get(string2);
                System.out.println(user.getId()+"/"+user.getName());
            }
        }
    }
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    public List<HashMap<String,String>> selectUser(){
        String sql = "select u.*, d.* from user u , dept d where u.udid = d.id order by u.uid asc";
        ResultSet rs = my.selectAction(sql, null);
        List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
        try {
            while (rs.next()){
                HashMap<String,String> map = new HashMap<String,String>(); 
                map.put("uid",String.valueOf(rs.getInt("uid")));
                System.out.println("String.valueOf(rs.getInt('uid')):"+String.valueOf(rs.getInt("uid")));
                list.add(map);
            }
            return list;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    public static void main(String[] args) {
        UserControl uc = new UserControl();
        List<HashMap<String,String>> list = uc.selectUser();
        System.out.println("list:"+list);
        Iterator<HashMap<String,String>> list1 = list.iterator();
        while (list1.hasNext()) {
            HashMap<String,String> hashMap = (HashMap<String,String>) list1.next();
            Iterator<String> string = hashMap.keySet().iterator();
            while (string.hasNext()) {
                String string2 = (String) string.next();
                System.out.println(string2+":"+hashMap.get(string2));   
            }


        }
    }

最后

以上就是文艺鸭子为你收集整理的Map,List 迭代器的使用的全部内容,希望文章能够帮你解决Map,List 迭代器的使用所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部