栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

浅谈集合的应用

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

浅谈集合的应用

泛型

T和E可以是任何类型

class People{
    T age;//String
    E name;//int

    public T getAge() {
        return age;
    }

    public void setAge(T age) {
        this.age = age;
    }

    public E getName() {
        return name;
    }

    public void setName(E name) {
        this.name = name;
    }
}

进行调用:
下面两种都行

People p=new People();
People p=new People();
List Arraylist
        ArrayList a=new ArrayList();
        //等效方式:List list =new ArrayList();
        a.add("a");
		a.add("a");
		a.add("a");
        a.add("b");//向集合里增加元素
        List list =new CopyOnWriteArrayList();//写时复制
        a.set(2,"mama");//将集合里第二个元素替换为"mama"
        list.remove("a");//将特定的元素("a")删除
        //迭代器
        Iterator it=a.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }

        //集合中的a.size相当于a.length
        for(int i=0;iSystem.out.println(c));
set:
        Student s1 = new Student("小龙女", 23);
        Student s2 = new Student("任盈盈", 24);
        Student s3 = new Student("小龙女", 23);
        Student s4 = new Student("东方不败", 25);
        Student s5 = new Student("伊琳", 29);
        Student s6 = new Student("周芷若", 30);
        HashSet hashSet = new HashSet<>();
        hashSet.add(s1);
        hashSet.add(s2);
        hashSet.add(s3);
        hashSet.add(s4);
        hashSet.add(s5);
        hashSet.add(s6);//增加数据
        hashSet.remove("东方不败");//删除"东方不败"
        for (Student student : hashSet) {
            System.out.println(student.getName()+"=="+student.getAge());
        }
    }


map
  Map < String,Integer >map=new ConcurrentHashMap();
        map.put("bab",1);
        map.put("mam",2);//增加元素
        map.remove(1);//删除索引1指向的元素
        map.keySet().forEach(c->System.out.println(c));//只输出键
        map.values().forEach(c->System.out.println(c));//只输出值
        map.entrySet().forEach(c->System.out.println(c));//都输出
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/282571.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

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