第十一章 集合

1 ArrayList

定义一个ArrayList集合

        List list=new ArrayList();

        List list2=new ArrayList();

1.1增

list.add("Hello");

list.add("World");

list.addAll(list2);

1.2删

list.remove(1);

//list.clear();

1.3改

list.set(0,"Java");

1.4查

Object o=list.get(0);

System.out.println(o);

2 LinkedList

定义一个LinkedList集合

        List list =newLinkedList();

2.1增

list.add("Hello");

list.add("张三");

list.addAll(list2);

2.2删

list.remove(1);

list.removeFirst();

list.removeLsat();

//list.clear();

2.3改

list.set(0,"Java");

2.4查

System.out.println(list.get(1));

System.out.println(list.getFirst());

System.out.println(list.getLast();

3 HashSet

创建HashSet集合

        HashSet hasSet=new HashSet();

3.1增

        hashSet.add("java01");
        hashSet.add("java02");
        hashSet.add("java04");
        hashSet.add("java03");
        hashSet.add("java02");

        System.out.println(hashSet);

        HashSet set2=new HashSet();
        set2.add("刘德华");
        set2.add("张学友");
        set2.add("黎明");

        hashSet.addAll(set2);

3.2删

        hashSet.remove("java02");

        //hashSet.clear();//清空集合容器的内容

3.3查

System.out.println(hashSet.isEmpty());

System.out.println(hashSet.contains("刘德华"));

3.4HashSet的遍历

======foreach遍历=======

for(Object o: hashSet){

        System.out.println(o);

}

======通过迭代器来遍历=======

Iterator iterator=hashSet.iterator();

while(iterator.hasNext()){

       // System.out.println(iterator.next());

       Object next = iterator.next();//指定移动并获取当前的元素
       System.out.println(next);

}

4TreeSet

TreeSet 基于TreeMap 实现。TreeSet可以实现有序集合,但是有序性需要通过比较器实现。

创建一个TreeSet集合

        TreeSet treeSet=new TreeSet();

4.1增

        treeSet.add("java05");
        treeSet.add("java03");
        treeSet.add("java04");
        treeSet.add("java01");

4.2存储对象类型

public class Test{

        public static void main(Stirng[] args){

                TreeSet treeSet=new TreeSet();

                

                treeSet.add(new Student("李AA",20));
                treeSet.add(new Student("王CC",16));
                treeSet.add(new Student("李BB",16));
               treeSet.add(new Student("李AA",15));

               System.out.println(treeSet);

        }

}

class Student implements Cpmparable{

        

        private String name;
        private Integer age;

        public Student() {
        }

        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

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

        public int getAge() {
            return age;
        }

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

        @Override
        public String toString() {
            return this.name+"  "+this.age;
        }

        @Override
        public int compareTo(Object o) {
            Student stu= (Student) o;
            if(this.age> stu.age ||                 !this.name.equals(stu.name)){
                return 1;
            }else if(this.age< stu.age ||                 !this.name.equals(stu.name)){
                return -1;
            }
            return 0;
        }
}

5HashMap

创建HashMap集合(Key,Value)

Key是唯一的

        Map map=new HashMap();

        Map map2=new HashMap();

5.1增

map.put("k1","v1");

map.put("k2","v2");

map.put("name","张三");

map2.put("a1","b1");

map2.put("a2","b2");

map.putAll(map2);

5.2删

map.remove("k2");

//map.clear();清空map

5.3改

map.replace("name","李四");

//map.put("name","李四");

5.4查

map.containsKey("name");//返回值为true/false

map.containsValue("张三");

map.get("k1");

//遍历map集合

Set set=map.keySet();

for(Object obj:set){

        System.out.println(obj+"---->"+map.get(obj));

}

集合的通用方法

.isEmpty()   //判断list集合是否为空,返回true/false

.contains("Java")  //判断list集合中是否包含该内容,返回true/false

6泛型

6.1 案例1

package com.edu.day0419.Test02_2;

/**
 * @Author Li Qinghua
 * @Create 2022/4/19 20:18
 */
public class Student<String,Integer>{
    private String name;
    private Integer age;

    public Student() {
    }

    public void show(){
        System.out.println(this.getName()+" "+this.getAge());
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}
public static void main(String[] args) {
    Student<String,Integer> stu=new Student<>("张三",20);
    stu.show();

    Student stu2=new Student<String,Integer>();
    stu2.setAge(30);
    stu2.setName("李四");
    stu2.show();
}

6.2 案例2

package com.edu.day0419.Test02;

/**
 * @Author Li Qinghua
 * @Create 2022/4/19 14:32
 */
public class Point<E> {
    private E x;
    private E y;

    public void show(){
        System.out.println("坐标为:"+"("+getX()+","+getY()+")");
    }

    public E getX() {
        return x;
    }

    public void setX(E x) {
        this.x = x;
    }

    public E getY() {
        return y;
    }

    public void setY(E y) {
        this.y = y;
    }
}

public static void main(String[] args) {
    Point<Integer> p1=new Point<>();
    p1.setX(5);
    p1.setY(263);
    p1.show();
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值