栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > C/C++/C#

Java集合+泛型:练习

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java集合+泛型:练习

一、常见问题
  1. 如何遍历Map的key集,value集,Key-value集,使用上泛型。
Map map = new Hashmap<>();
map.put(...);
...
//遍历key
Set keySet = map.keySet();
for (String key : keySet) {
	System.out.println(key);
}

//遍历value
Collection values = map.values();
Iterator iterator = values.iterator();
while (iterator.hasNext()) {
	System.out.println(iterator.next());
}

//遍历key-value
Set> entrySet = map.entrySet();
Iterator> iterator = entrySet.iterator();
while (iterator.hasNext()) {
	Map.Entry entry = iterator.next();
	String key = entry.getKey();
	Integer value = entry.getValue();
	System.out.println(key + "--->" + value);
}
  1. 提供一个方法,用于遍历获取HashMap中的所有value,并存放在List中返回。考虑上集合中泛型的使用。
public List getValueList(HashMap hashmap) {
	ArrayList valueList = new ArrayList<>();
	Collection values = map.values();
	for (String value : values) {
		valueList.add(value);
	}
	return valueList;
}
  1. 创建一个与a.txt文件同目录下的另外一个文件b.txt。
File file1 = new File("d:\a\a.txt");
File file2 = new File(file1.getParent(), "b.txt");
  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 {
    private Map 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 list() {
        //错误写法:不能强转为List,因为它本身就是Collection,而不是List多态化变成的Collection
//        Collection values = map.values();
//        return (List) values;
        ArrayList list = new ArrayList<>();
        Collection 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);
    }
}
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;
    }
}
public class DAOTest {

    @Test
    public void test1() {
        DAO 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 list = dao.list();
//        System.out.println(list);
        list.forEach(System.out::println); //foreach遍历
    }
}
转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1037555.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号