JavaSE集合框架(List、Set、Map)

一、单列集合 
        Collection(List  Set)
        List (ArrayList  LinkedList   Vector ) 
        Set(HashSet   LinkedHashSet)
            List:存取有序、有索引,可以存取重复元素
                  ArrayList:底层是数组实现,有索引,查询快,增删慢
                  LinkedList:底层是链表实现的,有索引,查询慢,增删快
                  
                  思考:同样都有索引为什么一个查询快一个查询慢?
            Set:存取无序,没有索引,不可以存取重复元素
                 HashSet:底层是哈希表+红黑树实现的,存取无序,没有索引,不可以存取重复元素
                 LinkedHashSet:底层是链表+哈希表实现的,存取有序,没有索引,不可以存取重复元素
            Set集合不重复的原理
                第一种情况:哈希值不同,直接存储
                第二种情况:哈希值相同,但是equals方法不同。以桶结构存储
                第三种情况:哈希值相同,但是equals方法也相同。不存储了
                 
                 在Set中添加自定义的对象时需要重写equals()和hashCode()。
            在TreeSet中添加自定义的对象时需要重写比较器。
                 
        Collection中常用的共性功能:
            boolean add(E e);            向集合中添加元素
            void clear();                清空集合所有的元素
            boolean remove(E e);        删除指定的元素
            boolean contains(E e);        判断集合中是否包含传入的元素
            boolean isEmpty();            判断集合是否为空
            int size();                    获取集合的长度
            Object[] toArray();            将集合转成数组
            
        
            
二、泛型
    自定义一个泛型类
    public class GenericClass<T>
    {
        private E name;

            public E getName() {
                return name;
            }

            public void setName(E name) {
                this.name = name;
            }
    }
       
    自定义一个泛型方法
    
    public class GenericMethod 
    {
            //定义一个含有泛型的方法
            public <M> void method01(M m){
                System.out.println(m);
            }

            //定义一个含有泛型的静态方法
            public static <S> void method02(S s){
                System.out.println(s);
            }
    }
    
    自定义一个泛型接口
    public interface GenericInterface<I> 
    {
            public abstract void method(I i);
        }

        //第一种实现类的方式
        public class GenericInterfaceImpl1 implements GenericInterface<String>{
            @Override
            public void method(String s) {
                System.out.println(s);
            }
        }

        //第二种实现类的方式
        public class GenericInterfaceImpl2<I> implements GenericInterface<I> {
            @Override
            public void method(I i) {
                System.out.println(i);
            }
    }
    
三、集合工具类

    1.Collections工具类
        - public static <T> boolean addAll(Collection<T> c, T... elements):往集合中添加一些元素。
        - public static void shuffle(List<?> list) 打乱顺序:打乱集合顺序。
        - public static <T> void sort(List<T> list):将集合中元素按照默认规则排序。
        - public static <T> void sort(List<T> list,Comparator<? super T> ):将集合中元素按照指定规则排序。
            --通常排序自定义的对象类时,需要实现Comparable<>接口,重写compareTo()方法,或者sort(list,new Comparator<>).

 

四、双列集合
    Map:接口提供三种collection 视图,允许以键集、值集或键-值映射关系集的形式查看某个映射的内容。
        映射顺序 定义为迭代器在映射的 collection 视图上返回其元素的顺序。
        某些映射实现可明确保证其顺序,如 TreeMap 类;另一些映射实现则不保证顺序,如 HashMap 类。 
    Map<object obj1,Object obj2>传入两个对象类型,Map没有索引,其有两种遍历方式:
        1、 Set<Map.Entry<String, String>> eset = map.entrySet();
            for (Map.Entry<String, String> entry : eset)
            {
                String key = entry.getKey();
                String value = entry.getValue();
                System.out.println(key+" ---> "+value);
            }
        2、Set<String> set = map.keySet();
            for (String s : set)
            {
                String value = map.get(s);
                System.out.println(s+" ---> "+value);
            }

关于ArrayList、HashSet、TreeSet  操作Studen类的实例:

     先定义一个Studen类,其中重写的equals()方法和hashCode()方法是为了在HashSet中不添加同一个学生

import java.util.Objects;

public class Student
{
    String name;
    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 String toString()
    {
        return "Student{" +
                "name='" + name + '\'' +
                ", 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;
        return age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode()
    {

        return Objects.hash(name, age);
    }
}
用ArrayList实现学生的年龄升序排序:(也可以在Student类中implements Comparable<Student>接口,然后重写compareTo()方法)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class StudentList
{
    public static void main(String[] args)
    {
        ArrayList<Student> list = new ArrayList<>();
        Student s1 = new Student("张三",18);
        Student s2 = new Student("李四",19);
        Student s3 = new Student("王五",20);
        Student s4 = new Student("赵六",17);

        Collections.addAll(list,s1,s2,s3,s4);
        System.out.println(list);
        Collections.sort(list, new Comparator<Student>()
        {
            @Override
            public int compare(Student s1, Student s2)
            {
                return s1.getAge() - s2.getAge();
            }
        });
        System.out.println(list);
    }
}

运行结果:

[Student{name='张三', age=18}, Student{name='李四', age=19}, Student{name='王五', age=20}, Student{name='赵六', age=17}]
[Student{name='赵六', age=17}, Student{name='张三', age=18}, Student{name='李四', age=19}, Student{name='王五', age=20}]

重写了equals()和hashCode()方法后在HashSet中添加:

import java.util.Collections;
import java.util.HashSet;

public class StudentSet
{
    public static void main(String[] args)
    {
        HashSet<Student> set = new HashSet<>();
        Student s1 = new Student("张三",18);
        Student s2 = new Student("李四",19);
        Student s3 = new Student("王五",20);
        Student s4 = new Student("赵六",17);
        Student s5 = new Student("张三",18);

        Collections.addAll(set,s1,s2,s3,s4,s5);
        System.out.println(set);

    }
}

运行结果:s5没有被添加进去

[Student{name='张三', age=18}, Student{name='赵六', age=17}, Student{name='王五', age=20}, Student{name='李四', age=19}]

在TreeSet中重写比较器按年龄升序排序(与List的区别是TreeSet不会添加相同元素,并且实现排序功能。其比较器的重写方式可以在构造方法里面也可以在待排序方法中实现Compareable接口)

import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;

public class StudentTreeSet
{
    public static void main(String[] args)
    {
        //在构造方法里面传入比较器,或者在泛型类中实现Comparable接口重写compareTo()方法
        TreeSet<Student> tset = new TreeSet<>(new Comparator<Student>()
        {
            @Override
            public int compare(Student s1, Student s2)
            {
                return s1.getAge() - s2.getAge();
            }
        });
        Student s1 = new Student("张三",18);
        Student s2 = new Student("李四",19);
        Student s3 = new Student("王五",20);
        Student s4 = new Student("赵六",17);
        Student s5 = new Student("张三",18);
        Collections.addAll(tset,s1,s2,s3,s4,s5);
        System.out.println(tset); //如果不重写比较器会抛出异常
                                    //Exception in thread "main" java.lang.ClassCastException:
                                 // com.demoCollections.Student cannot be cast to java.base/java.lang.Comparable
    }
}

输出结果:

[Student{name='赵六', age=17}, Student{name='张三', age=18}, Student{name='李四', age=19}, Student{name='王五', age=20}]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值