• Java集合+泛型:练习


    一、常见问题

    1. 如何遍历Map的key集,value集,Key-value集,使用上泛型。
    Map<String, Integer> map = new Hashmap<>();
    map.put(...);
    ...
    //遍历key
    Set<String> keySet = map.keySet();
    for (String key : keySet) {
    	System.out.println(key);
    }
    
    //遍历value
    Collection<Integer> values = map.values();
    Iterator<Integer> iterator = values.iterator();
    while (iterator.hasNext()) {
    	System.out.println(iterator.next());
    }
    
    //遍历key-value
    Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
    Iterator<Map.Entry<String, Integer>> iterator = entrySet.iterator();
    while (iterator.hasNext()) {
    	Map.Entry<String, Integer> entry = iterator.next();
    	String key = entry.getKey();
    	Integer value = entry.getValue();
    	System.out.println(key + "--->" + value);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    1. 提供一个方法,用于遍历获取HashMap中的所有value,并存放在List中返回。考虑上集合中泛型的使用。
    public List<String> getValueList(HashMap<String, String> hashmap) {
    	ArrayList<String> valueList = new ArrayList<>();
    	Collection<String> values = map.values();
    	for (String value : values) {
    		valueList.add(value);
    	}
    	return valueList;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. 创建一个与a.txt文件同目录下的另外一个文件b.txt。
    File file1 = new File("d:\\a\\a.txt");
    File file2 = new File(file1.getParent(), "b.txt");
    
    • 1
    • 2
    1. Map接口中的常用方法有哪些?
      增:put(K key, V value)
      删:V remove(K key)
      改:put(K key, V value)
      查:V get(K key)
      长度:int size()
      遍历:Map.Entry

    二、自定义泛型类练习

    定义一个泛型类 DAO,在其中定义一个 Map 成员变量,Map 的键为 String 类型,值为 T 类型。
    分别创建以下方法:
    public void save(String id, T entity):保存 T 类型的对象到 Map 成员变量中。
    public T get(String id):从 map 中获取 id 对应的对象。
    public void update(String id, T entity):替换 map 中 key 为 id 的内容,改为 entity 对象。
    public List list():返回 map 中存放的所有 T 对象。
    public void delete(String id):删除指定 id 对象。

    定义一个 User 类
    该类包含:private 成员变量(int 类型) id,age;(String 类型)name。

    定义一个测试类
    创建 DAO 类的对象,分别调用其 save、get、update、list、delete 方法来操作 User 对象。
    使用 Junit 单元测试类进行测试。

    public class DAO<T> {
        private Map<String, T> map = new HashMap<>();
    
        //public void save(String id, T entity):保存 T 类型的对象到 Map 成员变量中。
        public void save(String id, T entity) {
            map.put(id, entity);
        }
    
        //public T get(String id):从 map 中获取 id 对应的对象。
        public T get(String id) {
            return map.get(id);
        }
    
        //public void update(String id, T entity):替换 map 中 key 为 id 的内容,改为 entity 对象。
        public void update(String id, T entity) {
            if (map.containsKey(id)) {
                map.put(id, entity);
            }
        }
    
        //public List list():返回 map 中存放的所有 T 对象。
        public List<T> list() {
            //错误写法:不能强转为List,因为它本身就是Collection,而不是List多态化变成的Collection
    //        Collection values = map.values();
    //        return (List) values;
            ArrayList<T> list = new ArrayList<>();
            Collection<T> values = map.values();
            for (T entity : values) {
                list.add(entity);
            }
            return list;
        }
    
        //public void delete(String id):删除指定 id 对象。
        public void delete(String id) {
            map.remove(id);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    public class User {
    
        private int id;
        private int age;
        private String name;
    
        public User() {
        }
    
        public User(int id, int age, String name) {
            this.id = id;
            this.age = age;
            this.name = name;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", age=" + age +
                    ", name='" + name + '\'' +
                    '}';
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
    
            User user = (User) o;
    
            if (id != user.id) return false;
            if (age != user.age) return false;
            return name != null ? name.equals(user.name) : user.name == null;
        }
    
        @Override
        public int hashCode() {
            int result = id;
            result = 31 * result + age;
            result = 31 * result + (name != null ? name.hashCode() : 0);
            return result;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    public class DAOTest {
    
        @Test
        public void test1() {
            DAO<User> dao = new DAO<>();
    
            dao.save("1001", new User(1001, 34, "周杰伦"));
            dao.save("1002", new User(1002, 20, "昆凌"));
            dao.save("1003", new User(1003, 25, "蔡依林"));
    
            dao.update("1003", new User(1997, 18, "方文山"));
    
            dao.delete("1002");
    
            List<User> list = dao.list();
    //        System.out.println(list);
            list.forEach(System.out::println); //foreach遍历
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 相关阅读:
    【.NET 6+Loki+Grafana】实现轻量级日志可视化服务功能
    javaScript进阶面向对象ES6【正则表达式概述、正则表达式在 JavaScript 中的使用、正则表达式中的特殊字符、正则表达式中的替换】
    RocketMQ详细配置与使用
    轻量级的资源授权:基于 OAuth 规范
    408 | 【2016年】计算机统考真题 自用回顾知识点整理
    11、Microsoft Visual Studio 2022 Installer Projects踩坑一
    cesium开发入门(vue2)
    重学C++重构你的C++知识体系 升级版 学习笔记
    py15_Python 流程控制之 for-else 和 range() 步长以及 break/continue/return 跳转语句
    Docker配置镜像加速器
  • 原文地址:https://blog.csdn.net/m0_61467488/article/details/126218906