java学习笔记-集合

java学习笔记-集合

集合的特点

存储空间可变

集合的体系结构

集合
Collection
List
ArrayList
LinkedList
set
HashSet
LinkedSet
TreeSetSet
Map
HashMap

实现类:ArrayList,LinkedList,HashSet,LinkedSet,TreeSet,HashMap
接口:Collection,List,Set,Map

Collection

说明

单列集合,JDK不提供此接口的直接实现,通过其子类的实现类创建。

常用方法

方法说明
boolean add(E e)添加元素
boolean remove(Object o)删除指定元素
void clear()清空集合
boolean contains(Object o)判断集合中是否存在指定元素
boolean isEmpty()判断集合是否为空
int size()返回集合中元素个数
Iterator<E> iterator()返回集合元素迭代器
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionDemo {
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<>();
        System.out.println("initial:"+c);

        c.add("Hello");
        c.add("Hello");
        c.add("Hello");
        c.add("world");
        c.add("world");

        System.out.println("add:"+c);

        c.remove("Hello");
        System.out.println("remove:"+c);

//        c.clear();
//        System.out.println("clear:"+c);

        System.out.println("contains:"+c.contains("Hello"));

        System.out.println("isEmpty:"+c.isEmpty());

        System.out.println("size():"+c.size());

        Iterator<String> it = c.iterator();
        while (it.hasNext()){
            System.out.print(it.next()+" ");
        }
    }
}

运行结果:
initial:[]
add:[Hello, Hello, Hello, world, world]
remove:[Hello, Hello, world, world]
contains:true
isEmpty:false
size():4
Hello Hello world world

List

说明

有序集合,允许重复
有序:存储取出元素顺序一致

List集合特有方法

方法说明
void add(int index,E e)指定位置插入指定元素
E remove(int index)删除指定索引处元素,返回被删除元素
E set(int index,E e)修改指定索引处元素,返回被修改元素
E get(int index)返回指定索引处元素
ListIterator<E> listIterator​(int index)返回列表中的列表迭代器

Iterator不能在遍历时修改集合;
ListIterator允许沿任一方向遍历列表;可以在迭代期间通过迭代器自身的add方法向列表添加元素;可以获取列表中迭代器的当前位置

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListDemo {
    public static void main(String[] args) {
        List<Student> l = new ArrayList<>();

        Student s1 = new Student("张良",24);
        Student s2 = new Student("韩非",35);
        Student s3 = new Student("嬴政",27);

        l.add(s1);
        l.add(s2);
        l.add(s3);

        System.out.println("---iterator traversal---");
        Iterator<Student> it = l.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }

        System.out.println("---for traversal---");
        for (int i = 0; i < l.size(); i++) {
            System.out.println(l.get(i));
        }

        System.out.println("---foreach traversal---");
        for (Student s:l) {
            System.out.println(s);
        }
    }
}

运行结果:
—iterator traversal—
Student{name=‘张良’, age=24}
Student{name=‘韩非’, age=35}
Student{name=‘嬴政’, age=27}
—for traversal—
Student{name=‘张良’, age=24}
Student{name=‘韩非’, age=35}
Student{name=‘嬴政’, age=27}
—foreach traversal—
Student{name=‘张良’, age=24}
Student{name=‘韩非’, age=35}
Student{name=‘嬴政’, age=27}

实现类

ArrayList

底层是数组,有序,可重复

LinkedList

底层是链表,有序,可重复
特有方法

方法说明
void addFirst​(E e)在该列表开头插入指定的元素
void addLast​(E e)将指定的元素追加到此列表的末尾
E getFirst​()返回此列表中的第一个元素
E getLast​()返回此列表中的最后一个元素
E removeFirst​()从此列表中删除并返回第一个元素
E removeLast​()从此列表中删除并返回最后一个元素

Set

说明

set不包含重复元素;没有带索引的方法,不能使用普通for遍历

实现类

HashSet

无序
不重复:通过元素的hashcode()方法和equals()方法去重

LinkedHashSet

有序:链表保证有序(存取顺序一致)
不重复:hash表保证元素唯一

TreeSet

有序:不是存取顺序,根据构造方法的参数确定排序顺序
不重复:根据元素实现的排序器or构造方法排序器参数比较元素,构造器返回0,认定为重复不存储(构造器返回大于0升序,构造器返回小于0降序)。

public class Student {
    private String name;
    private int 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(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        return result;
    }

    @Override
    public String toString() {
        return name+","+age;
    }
}
import demo12hashset.Student;

import java.util.Comparator;

//集合内元素实现Comparable接口的compareTo方法,作为TreeSet无参构造方法的默认排序方式
public class StudentForCom extends Student implements Comparable{
    public StudentForCom() {
    }

    public StudentForCom(String name, int age) {
        super(name, age);
    }


    @Override
    public int compareTo(Object o) {
        StudentForCom s = (StudentForCom)o;
        if(getAge() == s.getAge()){
            return getName().compareTo(s.getName());
        }else{
            return getAge()-s.getAge();
        }
    }

    //Comparator的实现类,作为TreeSet构造方法参数指定排序顺序
    public class NameComparator implements Comparator{
        @Override
        public int compare(Object o1, Object o2) {
            return ((StudentForCom)o1).getName().compareTo(((StudentForCom)o2).getName());
        }
    }

    public class AgeComparator implements Comparator{
        @Override
        public int compare(Object o1, Object o2) {
            return ((StudentForCom)o1).getAge()-((StudentForCom)o2).getAge();
        }
    }


    public class StuComparator implements Comparator{
        @Override
        public int compare(Object o1, Object o2) {
            StudentForCom s1 = (StudentForCom)o1;
            StudentForCom s2 = (StudentForCom)o2;
            int num = s1.getAge()-s2.getAge();
            int num2 = num==0?s1.getName().compareTo(s2.getName()):num;
            return num2;
        }
    }
}

import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        TreeSet<StudentForCom> ts = new TreeSet<>(new StudentForCom().new StuComparator());

        StudentForCom s1 = new StudentForCom("Amy",17);
        StudentForCom s2 = new StudentForCom("Bill",17);
        StudentForCom s3 = new StudentForCom("Mary",16);
        StudentForCom s4 = new StudentForCom("Xixi",16);
        StudentForCom s5 = new StudentForCom("Xixi",15);
        StudentForCom s6 = new StudentForCom("Xixi",15);

        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);

        for (StudentForCom s : ts) {
            System.out.println(s);
        }
    }
}

运行结果:
Xixi,15
Mary,16
Xixi,16
Amy,17
Bill,17

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值