List、Set、数据结构、Collections

第一章 List集合

1.1 Java集合体系结构

1).Collection(单列集合):

|--List(接口--新增4个方法):1).有序的;2).可以存储重复元素;

|--ArrayList(子类):数组实现

|--LinkedList(子类--新增2个方法):链表实现

|--Set(接口--无特有方法):1).无序的;2).不能存储重复元素;

|--HashSet(子类):哈希表实现

|--LinkedHashSet(子类):链表、哈希表实现。

2).Map(双列集合):

1.2 List接口介绍

1).特点:

1).存取有序;

2).可以存储重复元素;

 3).带有索引;

 

1.3 List接口中常用方法

1).增:

public void add(int index, E ele) : 将元素ele添加到index位置。

2).删:

public E remove(int index) : 移除列表中指定位置的元素, 返回的是被移除的元素。

3).改:

public E set(int index, E ele) :用指定元素替换集合中指定位置的元素,返回值的更新前的元素。

4).查:

public E get(int index) :返回集合中指定位置的元素。

注意:上述四个方法,都会用到一个index作为索引值,注意不要超出集合范围,否则抛异常

 

第二章  数据结构

2.1 概念及结构

1).“数据结构”:是存储数据的方式;

2).“数据结构”的作用:不同的存储方式,会影响增、删、改、查操作的效率

2.2数组

1).“数组:在内存中是连续的 里面的每个元素都是一个挨着一个依次存储的。可以通过索引访问里面的元素;

2).特点:查询快;增删慢;

 

2.3 链表

1).特点:

1).多个节点,通过地址连接;

2).查询慢;增、删快;

2.4栈和队列

1),栈

1).特点:

1).受限的线性表:添加、删除元素都在同一端(栈顶);

2).先进后出;

 

2),队列

1).特点:

1).受限的线性表,添加、删除各占一端;

2).先进先出;

 

2.5红黑树

第三章  List的子类

3.1 ArrayList集合

1).ArrayList内部:数组实现--查询快;增删慢;

3.2 LinkedList集合

1).LinkedList:链表实现--查询慢;增删快;

  注意:LinkedList链表有索引值。

2).特有方法:

1).push():模拟压栈(将元素放在第一个位置)(无返回值)

2).poll():模拟弹栈(检索删除此列表的第一个元素)(有返回值

3)public E pop() :从此列表所表示的堆栈处弹出(删除)一个元素(列表为空时,容易报错

3)共有方法:(将方法稍微变形)

 public void addFirst(E e) :将指定元素插入此列表的开头。

public void addLast(E e) :将指定元素添加到此列表的结尾。

public E getFirst() :返回此列表的第一个元素。

public E getLast() :返回此列表的最后一个元素。

public E removeFirst() :移除并返回此列表的第一个元素。

public E removeLast() :移除并返回此列表的最后一个元素

 

第四章 Set接口

4.1 HashSet存储字符串:

1).Set集合的特点:

1).无序的;(所以,Set集合取出元素的方式采用:迭代器、增强for。)

2).不能存储重复元素;(基本类型元素字符串重复,则存储失败,运行不报错)

注意:Set集合与 Collection 接口中的方法基本一致,没有对 Collection 接口进行功能上的扩充。

4.2 HashSet存储自定义对象:

HashSet中存放自定义类型元素时,需要重写对象中的hashCode和equals方法,建立自己的比较方式,才能保证HashSet集合中的对象唯一。

如果不重写,可以运行,自定义元素可以添加到HashCode中。(因为对象的地址不一样)。

1).定义Student类(注意:重写:hashCode()和equals())

public class Student {
    private String name;
    private int 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);
    }
}

2).测试类:

public static void main(String[] args) {
    HashSet<Student> set = new HashSet<>();
    set.add(new Student("张三", 18));
    set.add(new Student("李四", 19));
    set.add(new Student("李四", 19));//先判断hashCode(),相同,再判断equals(),相同:不存
    System.out.println("集合大小:" + set.size());
}

注意:打印对象时,要重写toString()方法;

4.3 数据结构_哈希表:

特点:

1).采用:数组 + 链表 + 红黑树实现

2).验证元素的唯一性:

1).先判断元素的hashCode()
不同:存

相同:再判断元素的equals()

不同:存

相同:不存

4.4 LinkedHashSet的特点及使用:

1).LinkedHashSet:它链表、哈希表的实现;

  它是Set的特例:有序的哈希表;

4.5 Set接口_可变参数

1).在一个方法中最多只能有一个“可变参数”,而且必须位于参数列表的末尾

public static int sum(int a,String b,int ... nums){//OK的}

public static int sum(int...nums,int a,int b){//错误}

public static int sum(String...s,int ... nums){//错误}

第五章 Collections工具类

5.1常用功能:

1).Collections(工具类):里面包含了对Collection集合操作的一些工具性方法。

2).常用方法:

  1).public static <T> boolean addAll(Collection<T> c, T. . . elements) :往集合中添加一些元素。

  2).public static void shuffle(List<?> list) 打乱顺序 :打乱集合顺序。

  3).public static <T> void sort(List<T> list) :将集合中元素按照默认规则排序。

1),ArrayList<String> strList = new ArrayList<>();

ArrayList<Integer> strList = new ArrayList<>();

直接调用Collections.sort()方法。

2)集合里是自定义类型时,必须实现Comparable<自定义类>接口,并且,重写compareTo()方法。

  4).public static <T> void sort(List<T> list,Comparator<? super T>):将集合中元素按照指定规则排序。

3).示例代码:

public static void main(String[] args) {

    ArrayList<String> strList = new ArrayList<>();

    strList.add("abcdef");

    strList.add("bca");

    strList.add("cab");

    Collections.sort(strList);

    System.out.println(strList);

    //对自定义对象排序-->前提:Student必须实现Comparable接口

    ArrayList<Student> stuList = new ArrayList<>();

    stuList.add(new Student("张三1", 18));

    stuList.add(new Student("张三2", 19));

    stuList.add(new Student("张三3", 17));

    Collections.sort(stuList);//内部会调用compareTo()方法进行比较

    System.out.println(stuList);

}

5.2 实现比较方式一_自定义类添加比较功能_Comparable接口

1).public static <T> void sort(List<T> list) :将集合中元素按照默认规则排序。

示例代码:

public class Student implements Comparable<Student>{

    private String name;

    private int age;

     ......

注意:打印对象时,都需要重写toString()方法。

    @Override

    public int compareTo(Student o) {

        int n = o.age - this.age;

        return n;

    }

}

测试代码:

ArrayList<Student> stuList = new ArrayList<>();

stuList.add(new Student("张三1", 18));

stuList.add(new Student("张三2", 19));

stuList.add(new Student("张三3", 17));

Collections.sort(stuList);//sort会去调用Student的compareTo()方法进行依次的比较

System.out.println(stuList);

5.3 实现比较方式二_比较器_Comparator接口

4).public static <T> void sort(List<T> list,Comparator<? super T>):将集合中元素按照指定规则排序。

示例代码:

class MyCom implements Comparator<Student>{

    @Override

    public int compare(Student o1, Student o2) {

        return o1.getAge() - o2.getAge();

    }

}

interface 汽车{

    public void run();

}

class 奔驰 implements 汽车{

    @Override

    public void run() {

        System.out.println("奔驰时速320公里.....");

    }

}

class 奥迪 implements 汽车{

    @Override

    public void run() {

        System.out.println("奥迪时速280公里...");

    }

}

class 自行车 implements 汽车{

    @Override

    public void run() {

    }

}

class 司机 {

    public void 开车(汽车 qc){

        qc.run();

    }

}

public class Demo {

    public static void main(String[] args) {

        /*司机 sj = new 司机();

        sj.开车(new 奔驰());

        sj.开车(new 奥迪());

        sj.开车(new 自行车());

        sj.开车(new 汽车(){

                @Override

                public void run() {

                    System.out.println("三蹦子跑起来时速80公里.......");

                }

        });*/

 

        ArrayList<Student> stuList = new ArrayList<>();

        stuList.add(new Student("张三1", 18));

        stuList.add(new Student("张三2", 19));

        stuList.add(new Student("张三3", 17));

        stuList.add(new Student("张三4", 10));

 

//      Collections.sort(stuList, new MyCom());

        Collections.sort(stuList, new Comparator<Student>() {

                @Override

                public int compare(Student o1, Student o2) {

                    return o1.getAge() - o2.getAge();

                }

        });

        System.out.println(stuList);

    }

}

 

posted on 2018-10-11 18:31  水漾月  阅读(147)  评论(0编辑  收藏  举报

导航