集合体、增强for、Collection集合、迭代器、List集合、Set集合

集合(Collection(单列集合)(LIst集合(可重复)(ArrayList;LinkedList;);Set集合(不可重复)(HasheSet;TreeSet;););Map(双列集合)(HashMap))

Collection(单列集合):是单例集合的顶层接口,他表示一组对象,这些对象也称为Collection元素

JDK不提供此接口的任何直接实现,他提供更具体的子接口(如set、list)实现

创建Collection集合的对象

多态的方式

具体的实现类ArrayList

  public static void main(String[] args) {

        //创建 Collection集合对象
        Collection<String> a = new ArrayList<String>();
        //添加元素 boolean add(E e)
        a.add("hello");
        a.add("world");
        //输出
        System.out.println(a);


    }

Collection集合常用发方法: 

bollean add(E e);                        添加元素

bollean remove(Object o)           从集合中移除指定的元素

void clear()                                  清空集合中的元素

boolean contains(Object o)        判断集合中是否存在指定的元素

boolean isEmpty()                      判断集合是否为空

int size()                                     集合的长度,也就是集合中元素的个数

alt+7  打开一个窗口,能够看到类的所有信息

Collection集合的遍历

iterator:迭代器,集合的专用遍历方式

Iterator<E>iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到

迭代器是通过集合的iterator()方法得到的,所以我们说他是依赖于集合而存在的

Iterator中的常用方法

E next():返回迭代中的下一个元素

boolean hasNext():如果迭代具有更多元素,则返回true

    public static void main(String[] args) {

        //创建 Collection集合对象
        Collection<String> a = new ArrayList<String>();
        //添加元素 boolean add(E e)
        a.add("hello");
        a.add("world");
        a.add("java");
        //遍历1
//        Iterator<String> it = a.iterator();
//        for (int i=0;i<a.size();i++) {
//                System.out.println(it.next());
//        }
        //遍历2
        Iterator<String> it = a.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }



    }

LIstj集合概述和特点

List集合概述:1.有序集合(也成为序列),用户可以精确控制列表中每个元素的插入位置。用户可以通过整数索引访问元素,并搜索列表中的元素

                       2.与set集合不同v,列表通常允许重复的元素

List集合特点:1.有序:存出和取出的元素顺序一致

                        2.可重复:存储的元素可以重复

List集合特有方法:

void add(int index,E element)     再此集合中的特定位置插入指定的元素

E remove(int index)                     删除指定索引处的元素,返回被删除的元素

E set(int index,E element)           修改指定索引处的元素,返回被修改的元素

E get (index)                                返回指定索引处的元素

List并发修改异常

ConcurrentModificationException

产生原因:迭代器遍历过程中,通过集合对象修改了集合中元素的长度,造成了迭代器获取元素中判断预期修改值和实际修改值不一致

解决方案:用for循环遍历,然后对集合对象做对应的操作即可

ListIterator:列表迭代器

通过List集合的ListIterator()方法得到的,他是List集合特有的迭代器

用于允许程序员沿任意方向遍历列表的列表迭代器,在迭代期间修改列表,并获取列表中迭代器的当前位置

ListIterator中的常用方法

E next();                        返回迭代中的下一个元素

Boolean hasNext();       如果迭代具有更多元素,则返回true

E previous();                  返回列表中的上一个元素

boolean hasPrevious(); 如果此列表迭代器在相反方向遍历列表时具有更多元素,则返回true

void add(E e);               将指定的元素插入列表(不会出现并发修改异常)

增强for循环:简化数组和Collection集合的遍历

                        1.实现Iterable接口的类允许其成为增强型for语句的目标

                        2.它是JDK5之后出现的,其内部原理是一个Iterator迭代器

增强for的格式:

for(元素数据类型   变量名 : 数组或者Collection集合){

//  在此处使用变量即可,该变量就是元素

}

例:

int [] arr = {1,2,3,4,5};

for(int i : arr){

System.out.println(i);

}

public static void main(String[] args) {
        int [] arr = {1,2,3,4,5};
        for(int i : arr){
            System.out.println(i);
        }
        String [] str = {"hello","world","java"};
        for (String s:str){
            System.out.println(s);
        }
         List<String> list = new ArrayList<String>();
        list.add("hello");
        list.add("world");
        list.add("java");
        for (String a : list){
            System.out.println(a);
        }


    }
  public static void main(String[] args) {
        //创建老师类对象  测试
        List<Student> list = new ArrayList<Student>();
      Student s1 = new Student("同学1",20);
      Student s2 =new Student("同学2",22);
      Student s3 =new Student("同学3",24);
      //把学生添加到集合
        list.add(s1);
        list.add(s2);
        list.add(s3);
      //迭代器
      Iterator<Student> it = list.iterator();
      while (it.hasNext()){
            Student s = it.next();
            System.out.println(s.getName()+","+s.getAge());
      }
     //普通for
        for (int i=0;i<list.size();i++){
            Student s = list.get(i);
            System.out.println(s.getName()+","+s.getAge());
        }
     //增强for
        for (Student s : list){
            System.out.println(s.getName()+","+s.getAge());
        }

    }

数据结构:是计算机存储、组织数据的方式。是指互相之间存在一种或多种特定关系的数据元素的集合

通常情况下,选择合适的数据结构可以带来更高的运行或者存储效率

栈:先进后出   队列:先进先出

数组: 查询效率高、删除效率低、添加效率极低              链表:增删快,查询慢(对比数组)

List集合子类特点:

ArrayList:底层数据结构是数组,查询慢,增删快

LinkedList:底层数据结构是链表,查询慢,增删快

LinkedList集合的特有方法:

public void addFirst(E e);                  在该列表开头插入指定的元素

public void addLast(E e);                  将指定的元素追加到此列表的末尾

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

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

public E removeFirst();                     从此列表中删除并返回第一个元素

public E removeLast();                     从此列表中删除并返回最后一个元素

 public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<String>();
        list.add("hello");
        list.add("world");
        list.add("java");
        System.out.println(list);
        list.addFirst("javase");
        System.out.println(list);
        list.addLast("javase");
        System.out.println(list);
        System.out.println(list.removeFirst());
        System.out.println(list.removeLast());
        System.out.println(list.getFirst());
        System.out.println(list.getLast());

    }

Set集合概述和特点:不包含重复元素的集合;没有带索引的方法,所以不能使用普通for循环遍历

HashSet:对集合的迭代顺序不做任何保证

 public static void main(String[] args) {
   Set<String> set = new HashSet<String>();
   set.add("hello");
   set.add("world");
   set.add("java");
   set.add("world");
   for (String s : set){
       System.out.println(s);
   }
   System.out.println(set);

    }

哈希值:是JDK根据对象的地址或者字符串捉着数字算出来的int类型的数值

Object类中有一个方法可以获取对象的哈希值

public int hashCode();                 返回对象的哈希码值

  Student s1 = new Student("同学1",20);
      Student s2 =new Student("同学2",22);
      Student s3 =new Student("同学1",20);
        System.out.println(s1.hashCode());
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
        System.out.println(s3.hashCode());

结果:

460141958
460141958
1163157884
1956725890

哈希值特点 

默认情况下,不同对象哈希值不同

同一对象多次调用hashCode方法返回的哈希值相同

通过方法重写 可以实现不同对象哈希值相同

HashSet集合概述和特点

1.底层数据结构式哈希表

2.对集合的迭代顺序不做任何保证,也就是说不保证存出和取出的元素顺序一致

3.没有带索引的方法,所以不能使用普通for循环遍历

4.由于是set集合,所以是不包含重复元素的集合

HashSet存储集合并遍历

  public static void main(String[] args) {
   HashSet<String> set = new HashSet<String>();
   set.add("hello");
   set.add("world");
   set.add("java");
   set.add("world");
   for (String s : set){
       System.out.println(s);
   }
   System.out.println(set);

    }

HashSet集合存储元素:要保证元素唯一性,需要重写hashCode()和equals()方法

哈希表:JDK8之前,底层采用数组+链表实现,可以说是一个元素为链表的数组

              JDK8之后,在长度比较长的时候,底层实现了优化

LinkedHashSet集合特点:1.哈希表和链表实现的Set接口,具有可预测的迭代次序

                                            2.有链表保证元素有序,也就是元素的存储和去除顺序是一致的

                                            3.有哈希表保证元素唯一,就是没有重复的元素

 TreeSet集合特点:1.元素有序:这里的顺序不是只存储和取出的顺序,而是按照一定的规则进行

                                                   排序,具体排序方式取决于构造方法

                                                  TreeSet();根据其元素的自然排序进行排序

                                                  TreeSet(Comparator comparator);根据指定的比较器进行排序

                                2.没有带索引的方法,所以不能使用普通for循环遍历

                                3.由于是Set集合,所以不包含重复元素的集合   

   public static void main(String[] args) {
      TreeSet<Integer> m = new TreeSet<Integer>();
      m.add(16);
      m.add(21);
      m.add(18);
      m.add(25);
      m.add(22);
      for (Integer s : m){
          System.out.println(s);
      }
        System.out.println(m);

    }

结果:16
18
21
22
25
[16, 18, 21, 22, 25]

自然排序Comparable的使用

public class Student implements Comparable<Student>{
    public Student() {
    }

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

    private int age;
    private String name;

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

    @Override
    public int compareTo(Student s) {
        //按照年龄大小排序
        int num = this.age - s.age;
        int num2 = num==0?this.name.compareTo(s.name):num;
        return num2;
    }
}
import java.util.TreeSet;

public class Demo {

    public static void main(String[] args) {
        //创建老师类对象  测试
        TreeSet<Student> list = new TreeSet<Student>();
        Student s1 = new Student("huangtongxue",20);
        Student s2 =new Student("lvtongxue",22);
        Student s3 =new Student("zitongxue",20);
        //把学生添加到集合
        list.add(s1);
        list.add(s2);
        list.add(s3);
        //增强for
        for (Student s : list){
            System.out.println(s.getName()+","+s.getAge());
        }

    }

}

用TreeSet集合存储自定义对象,无参构造方法使用的是自然排序对元素进行排序

自然排序,就是让元素所属的类实现Comparable接口,重写compareTo(T  o )方法

重写方法时,一定要注意排序规则必须按照要求的主要他和次要条件来写

比较器排序Comparator的使用

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

public class Demo {

    public static void main(String[] args) {
        //创建老师类对象  测试
        TreeSet<Student> list = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
             int num = s1.getAge() - s2.getAge();
             int num2 = num==0?s1.getName().compareTo(s2.getName()):num;
             return num2;
            }
        });
        Student s1 = new Student("huangtongxue",20);
        Student s2 =new Student("lvtongxue",22);
        Student s3 =new Student("zitongxue",20);
        //把学生添加到集合
        list.add(s1);
        list.add(s2);
        list.add(s3);
        //增强for
        for (Student s : list){
            System.out.println(s.getName()+","+s.getAge());
        }

    }

}

用TreeSet集合存储自定义独享,带参构造方法使用的是比较器排序对元素进行排序的

比较器派小组,就是让几何构造方法接受Comparator的实现类对象,重写compare( to1   to2)方法

重写方法时,一定要注意排序规则必须按照要求的主要条件和次要条件来写

同学成绩排序

public class Student{
    public Student() {
    }

    public Student(String name, int chinese, int math) {
        this.name = name;
        this.chinese = chinese;
        this.math = math;
    }

    private String name;
    private int chinese;
    private  int math;

    public String getName() {
        return name;
    }

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

    public int getChinese() {
        return chinese;
    }

    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }
}

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

public class Demo {

    public static void main(String[] args) {
        //创建老师类对象  测试
        TreeSet<Student> list = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
             int num1 = s1.getChinese()+s1.getMath();
             int num2 = s2.getChinese()+s2.getMath();
             int num = num2 - num1;
             int num3 = num==0?s1.getName().compareTo(s2.getName()):num;
             return  num3;
            }
        });
        Student s1 = new Student("huangtongxue",88,96);
        Student s2 =new Student("lvtongxue",80,95);
        Student s3 =new Student("zitongxue",82,99);
        Student s4 =new Student("hongtongxue",81,88);
        Student s5 =new Student("lantongxue",69,91);
        Student s6 =new Student("qingtongxue",95,89);
        //把学生添加到集合
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        list.add(s5);
        list.add(s6);
        //增强for
        for (Student s : list){
            System.out.println(s.getName()+","+s.getChinese()+","+s.getMath());
        }

    }

}

比较器

public class Student implements Comparable<Student>{
    public Student() {
    }

    public Student(String name, int chinese, int math) {
        this.name = name;
        this.chinese = chinese;
        this.math = math;
    }

    private String name;
    private int chinese;
    private  int math;

    public String getName() {
        return name;
    }

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

    public int getChinese() {
        return chinese;
    }

    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    @Override
    public int compareTo(Student s) {
            int num1 = this.getChinese()+this.getMath();
            int num2 = s.getChinese()+s.getMath();
            int num = num2 - num1;
            int num3 = num==0?this.getName().compareTo(s.getName()):num;
            return  num3;
    }
}

获取十个随机数(1-20) 

  Set<Integer> s = new HashSet<Integer>();
        Random r = new Random();
        while(s.size()<10){
        int number =  r.nextInt(20)+1;
        s.add(number);
        }
        for (Integer m :s){
            System.out.println(m);
        }
        System.out.println(s);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值