Talking about the love and hatred of the Collection interface family!

Collection interface (single value storage)

Insert picture description here

java.util.Collection接口:子接口: --〉List集合,Set集合
	定义的是所有单列集合中的共性方法,所有的单列集合都可以使用的共性方法
	共性方法:(抽象方法)
		public boolean add(E e):把给定的对象中添加元素
		
		public void clear();清空集合中所有的元素
		
		public boolean remove();把给定的对象在当前的集合中删除
		
		public boolean contains();判断当前集合是否包含给定的元素
		
		public boolean isEmpty();判断集合是否为空,为空返回true
		
		pubic int size();返回集合中元素的个数
		
		public Object[]  toAaary();把集合中的元素存储到数组中
		
		addALL(Collection c):将参数c中的元素,都添加到调用者集合中
		
		removeAll(Collection c):从调用者集合中,删除那些也存在参数c中的元素
		
		containsAll(Collection c):判断调用者,是否包含参数c中的所有元素
		
		retainAll(Collection c):参数c中有哪些元素,就在调用者中保留那些元素(交集)

List interface (duplicate allowed)

重写了Collection接口的一些列函数。
继承了add和addAll两个函数的规则。
新增了一些列函数。

Features: The
elements are placed in order, and the elements can be repeated.

For example, the order of storing elements is 11, 22, 33. Then in the set, the storage of the elements is done in the order of 11, 22, 33).

It is an indexed collection, and the elements in the collection can be accurately manipulated by indexing (the same
reason as the index of the array ).
There can be repeated elements in the set, and the equals method of the elements is used to compare whether they are repeated elements.

Common methods in the list interface (new)
public void add(int index, E element) : 将指定的元素,添加到该集合中的指定位置上。
public E get(int index) :返回集合中指定位置的元素。
public E remove(int index) : 移除列表中指定位置的元素, 返回的是被移除的元素。
public E set(int index, E element) :用指定元素替换集合中指定位置的元素,返回值的更新
前的元素。

Implementation class:

ArrayList:(常用)
底层数据结构是数组。线程不安全
LinkedList:
底层数据结构是链表。线程不安全
Vector:
底层数据结构是数组。线程安全(如果不需要线程安全用ArrayList)

ArarryList (implementation)

When implementing, we must pay attention to the validity of the coordinates and do not exceed the length of the collection. The
Vector implementation is the same as it.

public class Demo1 {
    public static void main(String[] args) {
        /**
         * ArrayList 数组结构 查找 增加和删除慢
         */
        ArrayList<Integer> data = new ArrayList<>();
        //添加 100 200 300
        data.add(100);
        data.add(200);
        data.add(300);
        //通过坐标添加元素 400
        data.add(1,400);
        //指定对象删除      即 删除 坐标为1的元素
        data.remove(1);
        //返回指定位置的元素 即 获取 坐标为1的元素
        data.get(1);
        //指定对象替换      即 替换 坐标为1的元素
        data.set(2,500);
        //判断是否包含此对象
        boolean flag =data.contains(200);
        //判断是否为空
        boolean flag1 =data.isEmpty();
        //返回集合长度
        int size =data.size();
        System.out.println(flag);
        System.out.println(flag1);
        System.out.println(data);
        System.out.println(size);
        //将集合中的元素存到数组里面
        Object [] arr=data.toArray();
        for (Object s: arr){
            System.out.print(s+" ");
        }

    }
    /**
     * true
     * false
     * [100, 200, 500]
     * 3
     * 100 200 500
     */

}

Implementation of LinkedList

public class Demo2 {
    public static void main(String[] args) {
        /**
         * LinkedList : 双向链表  增删快 查找慢
         */
        LinkedList<Integer> data = new LinkedList<>();
        // 双向队列模式  头插 尾取

        data.addFirst(100);
        data.addFirst(200);
        int a = data.removeLast();
        System.out.println(a);
        //双向队列模式  尾插 头取
        data.addLast(300);
        data.addLast(400);
        int b =data.removeFirst();
        System.out.println(b);
        System.out.println(data);
        //压栈 push
        data.push(600);
        data.push(700);
        //弹栈 栈顶元素
        int c =data.pop();
        System.out.println(c);
        //判断是否包含元素 即:是否为空
        System.out.println(data.isEmpty());

    }
}

Set interface

The elements are not placed in order, and the elements cannot be repeated.

并没有新增自己的函数
重写了Collection接口的部分函数
继承了add函数和addAll函数

Implementation class:

- HashSet:底层数据结构是哈希表(是一个元素为链表的数组) 即为散列表
- TreeSet:底层数据结构是红黑树(是一个自平衡的二叉树)。保证元素的排序方式
- LinkedHashSet:底层数据结构由哈希表和链表组成。

HashSet implementation

public static void main(String[] args) {
        //HashSet : 散列存放
        HashSet<String> set = new HashSet<>();
        boolean flag =set.add("当 我和世界不一样");
        boolean flag1 =set.add("当 我和世界不一样");
        set.add("坚持对我来说就是以刚克刚");
        set.add("最美的愿望 一定最疯狂");
        System.out.println(flag);
        System.out.println(flag1);
        Iterator<String> it =set.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }

TreeSet implementation

public static void main(String[] args) {
        TreeSet<String> set =new TreeSet<>();
        set.add("D");
        set.add("A");
        set.add("C");
        set.add("B");
        //按照ASCII码值排列
        for (String a: set) {
            System.out.println(a);
        }

When it is a reference type, you need to rewrite the method:

public static void main(String[] args) {
        TreeSet<Person> data =new TreeSet<>();
        Person person =new Person("陈潮",19);
        Person person1 =new Person("陈2",13);
        Person person2 =new Person("陈3",16);
        data.add(person);
        data.add(person1);
        data.add(person2);
        for (Person p:data) {
            System.out.println(p);
        }
    }
    static class Person implements Comparable<Person>{
        private String name;
        private int age;

        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }

        @Override
        public int compareTo(Person o) {
            //this 与 o 比较
            if(this.age>o.age){
                return 1;
            }else if(this.age  ==o.age){
                return 0;
            }
            return -1;
        }

        public Person() {
        }

        public Person(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;
        }
    }

Iterator interface

The general access method of Collection collection elements. Before fetching an element, it is necessary to determine whether there is an element in the set. If there is, take this element out, continue to judge, and then take it out if there is any. All the elements in the set are always taken out. The technical term for this retrieval method is called iteration.

boolean hasNext()  如果迭代器中还有元素那么返回true。
E next()  返回迭代器中游标的下一元素
void remove()  从迭代器指向的 collection 中移除迭代器返回的最后一个元素。每次调用 next 后只能调用一次此方法。如果进行迭代时用调用此方法之外的其他方式修改了该迭代器所指向的 collection,则迭代器的行为是不确定的。

achieve

public static void main(String[] args) {
    
    
        //使用多态方式创建对象
        Collection<String> list = new ArrayList<>();
        //添加元素到集合
        list.add("hello");
        list.add("world");
        list.add("java");
        //创建迭代器对象
        Iterator<String> it = list.iterator();
        //遍历操作
        while(it.hasNext()){
    
    
            String s = it.next();
            System.out.println(s);
        }
    }

Enhanced for loop

Enhanced for loop (also known as for each loop) is an advanced for loop after JDK1.5, specifically used to traverse arrays and collections. Its internal principle is actually an Iterator iterator, so in the process of traversal, elements in the collection cannot be added or deleted.

It is used to traverse Collection and array. Usually only traverse elements, do not add or delete collection elements during the traversal process.

Traverse the array:

public static void main(String[] args) {
    
    
        int [] arr ={
    
    2,3,4,5};
        for (int s :arr){
    
    
            System.out.println(s);
        }
    }

Traverse the collection:

 public static void main(String[] args) {
    
    
        Collection<String> collection = new ArrayList<>();
        collection.add("四面楚歌");
        collection.add("司马八荒");
        collection.add("一生牵挂");
        for(String s : collection){
    
    
            System.out.println(s);
        }

Guess you like

Origin blog.csdn.net/AzirBoDa/article/details/113525578