Java集合

Collection、List、set

  1. collection接口中的实现类ArrayList
  2. List接口中的实现类ArrayList,linkedList,List
    一些方法:add、remove、Iterator(迭代)、contains(判断是否包含)isEmpty(判断是否为空)
  3. 泛型
    泛型类Geberic:书写方式:public class a{};1.可以为数据类型创建变量。2.可以作为方法入参3.可以作为方法的返回值
    泛型接口:书写方式:public interface MyGenericjk {},不能创建泛型静态常量
    泛型方法:书写方式:public void show(){}
  4. Set集合中实现类:继承colllection方法,hashSet、treeSet
  5. Comparator接口(定制比较器)在匿名内部类中定制规则。
  6. Map集合中的实现方法:HashMap,TreeMap
    小结:集合的概念:对象的容器,和数组类似,定义对多个对象进行操作的常用方法
    List集合:有序、有下标、元素可以重复(ArrayList、LinkedList、Vector)
    Set集合:无序、无下标、元素不可以重复、(HashSet、TreeSet)(linkedHashSet,是有序的)
    Map集合:存储一对数据,无序、无下标、键不可以重复、值可以重复(HashMap、HashTable、TreeMap)
    Colletions:集合工具类,定义除了存取以外的集合常用方法

1. Collection collection=new ArrayList();

package com.f.www.Gather;
import java.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Demo01 {
    public static void main(String[] args) {
    // 创建一个集合
        Collection collection=new ArrayList();
        //添加元素
        collection.add("苹果");
        collection.add("香蕉");
        collection.add("梨");
        System.out.println("元素个数:"+collection.size());
        System.out.println(collection);
        //删除元素
        collection.remove("梨");
        System.out.println("元素个数:"+collection.size());
        System.out.println(collection);
        collection.clear();
        System.out.println("元素个数:"+collection.size());
        //遍历元素
        for (Object o:collection
             ) {
            System.out.println("元素个数:"+collection.size());
            System.out.println(o);
        }
        System.out.println("--------迭代器————————");
        //迭代器
        //三种方法,hasNext();判断有没有下个元素
        //next();获取下一个元素
        //remove();删除当前元素
        Iterator iterator = collection.iterator();
        while (iterator.hasNext()){
            String s=(String)iterator.next();
            //不能使用collection删除方法
            //collection.remove(s);
            iterator.remove();
        }
        System.out.println("元素个数:"+collection.size());
        //判断
        System.out.println(collection.contains("西瓜"));//判断是否包含当前元素
        System.out.println(collection.isEmpty());//collection.isEmpty();判断是否为空

        Collection c=new ArrayList<>();
        Student s1=new Student("张三",20);
        Student s2=new Student("张四",21);
        Student s3=new Student("张无",22);
        //1.添加学生数据
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
        System.out.println("元素个数"+collection.size());
        System.out.println(collection.toString());
        //删除
        //collection.remove(s1);
        System.out.println("元素个数:"+collection.size());
        collection.remove(new Student("张三",20) );
        System.out.println();
    }
}

2. List list1=new ArrayList<>();

package com.f.www.Gather;

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

public class ListDemo {
    public static void main(String[] args) {
        List list1=new ArrayList<>();
        //添加元素
        list1.add("苹果");
        list1.add("小米");
        list1.add("华为");
        System.out.println("元素个数:"+list1.size());
        System.out.println(list1.toString());
        //删除元素
        list1.remove("苹果");
        list1.remove(0);
        System.out.println("删除之后"+list1.size());
        System.out.println(list1.toString());
        //3.遍历
        //使用for遍历
        for (int i = 0; i < list1.size(); i++) {
            System.out.println(list1.get(i));

        }
        //使用增强for
        for (Object o:list1
             ) {
            System.out.println(o);

        }
        //使用迭代器
        Iterator it =list1.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
        //使用列表迭代器,可以向前向后遍历,可以添加元素,删除,修改元素
        ListIterator lit=list1.listIterator();
        System.out.println("----从前往后迭代------");
        while (lit.hasNext()){
            System.out.println(lit.nextIndex()+":"+lit.next());
        }
        System.out.println("----从后往前迭代------");
        while (lit.hasPrevious()){
            System.out.println(lit.previousIndex()+":"+lit.previous());
        }
        //判断
        System.out.println(list1.contains("苹果"));
        System.out.println(list1.isEmpty());
        //获取位置
        System.out.println(list1.indexOf("苹果"));
    }
}

3. LinkedList linkedList=new LinkedList();

package com.f.www.Gather;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

/**
 * LinledList使用
 * 链表结构:双向链表
 *@author luci
 */
public class LinkedListDemo {
    public static void main(String[] args) {
        //创建集合
        LinkedList linkedList=new LinkedList();
        //1.添加元素
        Student s1=new Student("张三",20);
        Student s2=new Student("张四",21);
        Student s3=new Student("张无",22);
        linkedList.addLast(s1);
        linkedList.addLast(s2);
        linkedList.addLast(s3);
        System.out.println("元素个数:"+linkedList.size());
        System.out.println(linkedList.toString());
        // 删除
        linkedList.remove(new Student("张三",20));
        System.out.println("删除之后:"+linkedList.size()+linkedList.toString());
        System.out.println("------- for-----");
        for (int i = 0; i < linkedList.size(); i++) {
            System.out.println(linkedList.get(i));
        }
        System.out.println("------- 使用增强for-----");
        for (Object o:linkedList
             ) {Student student=(Student)o;
            System.out.println(student.toString());
        }
        System.out.println("------- 使用迭代器-----");
        Iterator it= linkedList.iterator();
        while (it.hasNext()){
            Student student=(Student)it.next();
            System.out.println(student.toString());
        }

        System.out.println("--------使用列表迭代器-------");
        ListIterator listIterator= linkedList.listIterator();
        while (listIterator.hasNext()){
            Student student=(Student)listIterator.next();
            System.out.println(student.toString());
        }
        //判断
        System.out.println(linkedList.contains("张三"));
        System.out.println(linkedList.isEmpty());
        //获取
        System.out.println(linkedList.indexOf(s1));
    }
}

4. ArrayList arrayList=new ArrayList();

package com.f.www.Gather;

import jichuyufa.Array.Arrayslei;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class ListRealize {
    /**
     * ector 数组结构实现,jdk1.0 线程安全 查询快,增删慢
     * ArrayList 数组结构实现 jdk1.2 线程不安全 查询快,增删慢
     * LinkedList 链表结构实现,增删快,查询快
     *
     */
    public static void main(String[] args) {
        ArrayList arrayList=new ArrayList();
        Student s1=new Student(" 老聂",25);
        Student s2=new Student(" 老郑",30);
        Student s3=new Student(" 老刘",22);

        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        System.out.println("元素个数:"+arrayList.size());
        System.out.println(arrayList.toString());
        //删除元素
       // arrayList.remove(new Student("老郑 ",30)); //equals (this==object)
        System.out.println("删除之后元素个数:"+arrayList.size());
        System.out.println("删除之后:"+arrayList.toString());
        //使用迭代器
        System.out.println("---------使用迭代器---------");
        ListIterator l=arrayList.listIterator();
        while(l.hasNext()){
            Student s =(Student)l.next();
            System.out.println(s.toString());
        }
        System.out.println("---------使用列表迭代器-------");
        ListIterator li= arrayList.listIterator();
        while (li.hasNext()){
            Student s11=(Student)li.next();
            System.out.println(s11.toString());
        }
        System.out.println("-----使用列表迭代器逆序-------");
        while (li.hasPrevious()){
            Student s11=(Student)li.previous();
            System.out.println(s11.toString());
        }
        //判断
        System.out.println(arrayList.contains(new Student("老聂",25)));
        System.out.println(arrayList.isEmpty());//判断集合是否为空
        // 查找
        System.out.println(arrayList.indexOf(s1));
    }
}

5.泛型类

package com.f.www.Gather;

/**
 * 泛型类
 * 语法,类名<T ,R,Y>
 * T是类型占位符,是一种引用类型,如果编写多个,用逗号隔开。
 */
public class MyGeneric <T>{
    // 使用泛型
    //创建变量
    T t;
    //2.作为方法的参数
    public void show(T t){
        System.out.println(t
        );
        //3.使用泛型作为方法的返回值
    }

    public T getT() {
        return t;
    }
}

6.泛型接口

package com.f.www.Gather;
/**
 * 泛型接口
 * 语法,接口名<T>
 *
 */
public interface MyGenericjk<T> {
    String name="张三";
     T s(String string);
}

7.泛型接口的实现类

package com.f.www.Gather;

import javax.print.DocFlavor;

public class MyGennericImpl<T> implements MyGenericjk<DocFlavor.STRING> {

    @Override
    public DocFlavor.STRING s(String string) {
        System.out.println(string);
        return null;
    }
}

}

8.泛型方法

package com.f.www.Gather;

/**
 * 泛型方法
 * 语法:<T> 返回值类型
 * @author luci
 */
public class MyGenericMethod {
    //泛型方法
    public <T>T show(T t){
        System.out.println("泛型方法"+t);
        return t;
    }
}

9. TestGenneric测试泛型

package com.f.www.Gather;

public class TestGenneric {
    public static void main(String[] args) {
        //使用泛型类创建对象
        //泛型只能使用引用类型
         MyGeneric<String>myGeneric=new MyGeneric<String>();
         myGeneric.t="hello";
        System.out.println(myGeneric.t);
         myGeneric.show("hello1");
        String string=myGeneric.getT();
        System.out.println(string);
        System.out.println("----泛型接口使用-----");
        MyGennericImpl impl=new MyGennericImpl();
        impl.s("xxxxxx");
        System.out.println("---------泛型方法------");
        MyGenericMethod myGenericMethod=new MyGenericMethod();
        myGenericMethod.show("中国加油");
        myGenericMethod.show(1);
    }
}

10.vector集合使用

package com.f.www.Gather;


import java.util.Enumeration;
import java.util.Vector;

/**
 * 演示vector集合使用
 * 存储结构,数组
 * @author luci
 *
 */
public class Vetoruse {
    public static void main(String[] args) {
        //创建集合
        Vector vector=new Vector();
        //1.添加集合
        vector.add("草莓");
        vector.add("苹果");
        vector.add("香蕉");
        System.out.println("元素个数:"+vector.size());
        System.out.println(vector.toString());
        //2.删除
        vector.remove(0);
        vector.remove("苹果");
        vector.clear();
        //3.遍历
        //使用枚举器
        Enumeration enumeration=vector.elements();
        while (enumeration.hasMoreElements()){
            String o=(String) enumeration.nextElement();
            System.out.println(o);
        }
        //4.判断
        System.out.println(vector.contains("苹果"));
        System.out.println(vector.isEmpty());
        //5.其他
        vector.firstElement();
        vector.lastElement();
        vector.get(5);

    }
}

11.用到的类Student类

package com.f.www.Gather;

import java.util.Objects;

public class Student implements Comparable<Student> {
    private String name;
    private int age;



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

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

    public int getAge() {
        return age;
    }

    public Student setAge(int age) {
        this.age = age;
        return this;
    }

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

    @Override
    public boolean equals(Object o) {
        //判断是否为同一个对象
        if(this==o){
            return true;
        }
        //判断是否为空
        if (o==null){
            return false;
        }
        //判断是否为Student类型
        if (o instanceof Student){
            Student s=(Student)o;
            //4.比较属性
        if (this.name.equals((s.getName()))&&this.age==s.getAge());
        return true;
        }
        return false;
    }



    @Override
    public int hashCode() {
        int n1=this.name.hashCode();
        int n2=this.age+31;
        return n1+n2;
    }
//先按姓名比,再按年龄比
    @Override
    public int compareTo(Student student) {
        int n1=this. getName().compareTo(student.getName());
        int n2=age-student.age ;
        return n1==0?n2:n1;
    }
 /*   @Override
    public boolean equals(Object o) {
        //判断是否为同一个对象
        if(this==o){
            return true;
        }
        //判断是否为空
        if (o==null){
            return false;
        }
        //判断是否为Student类型
        if (o instanceof Student){
            Student s=(Student)o;
            //4.比较属性
            if (this.name.equals((s.getName()))&&this.age==s.getAge());
            return true;
        }
        return false;
    }*/
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luci Aristide 哎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值