20150802-集合

来源:互联网 发布:三星高通9008端口救砖 编辑:程序博客网 时间:2024/06/16 21:56

    • 一集合的基本知识包javautil
    • 二用法
      • ArrarList类
      • HashSet类
      • HashMap类
    • 三总结

一、集合的基本知识(包java.util.*)

一)结构
注:Collection是接口;Collections是类。
这里写图片描述
各个集合的特点:
1.List:元素有序,元素可重复。

  • ArrayList:存放的元素内存连续。(优点:遍历快,缺点:插删慢)
  • LinkedList:存放的元素内存不连续。(优点:插删快)

2.Set:元素无序,元素不可重复。(无序是指元素存入和取出的顺序不一定一致)

  • HashMap:允许使用null键值和null值。

二)作用:
存储对象。与数组的区别:

  • 数组长度不变,集合长度不固定。
  • 数组只能存同一类型的对象,集合可存储不同类型的对象。

二、用法:

ArrarList类:

1.步骤:
1)建立对象:ArrayList al = new ArrayList<>(); //泛型
2)使用add(),addAll(),remove(),contains(),size()等方法。

ArrayList:import java.util.ArrayList;public class Test05 {    public static void main(String[] args) {        ArrayList<String> al = new ArrayList<>();        ArrayList<String> al2 = new ArrayList<>();        al.add("akd");        al.add("243");        al.add("3");        al2.addAll(al);  //必须相同类型的对象可以添加        System.out.println(al2);        al2.remove(1);        System.out.println(al);//其父类中的toString()方法,返回的是collection 的字符串表示形式。        System.out.println(al2);        System.out.println(al2.contains("3")); //是否包含某个元素        for(int i=0;i<al.size();i++)            System.out.println(al.get(i));    }}/*结果:[akd, 243, 3][akd, 243, 3][akd, 3]trueakd2433*/

2.延伸:
/对ArrayList的成员根据一定的规则(这里是年龄)来比较/
利用Collections中的sort()方法
步骤:
1).先创一个类,来实现Comparator接口,重写其compare方法(设定规则)。
2).使用时:通过调用Collections中的sort方法,传入要排序的对象和规则。

1./*Student类*/    public class Student {    private String name;    private int age;    public Student(String name,int age){        this.name = name;        this.age = age;    }    public Student(String name){        this.name=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;    }
2./* * 创建一个类继承Comparator接口,进行比较 * */import java.util.Collection;import java.util.Comparator;public class CollectionSort implements Comparator<Student> {    @Override    public int compare(Student stu1, Student stu2) {        return stu1.getAge()-stu2.getAge();    }}
3.//进行比较import java.util.ArrayList;import java.util.Collections;public class TestArrayList {    public static void main(String[] args) {        ArrayList<Student> al = new ArrayList();        ArrayList<Student> al2 = new ArrayList();        al.add(new Student("zhangsan",19));        al.add(new Student("lisi",19));        al.add(new Student("wangwu",34));        al.add(new Student("zhaoli",20));        al.add(new Student("xiaohong",10));        /*         * 1.通过Collections类中的sort方法进行排序,         * 2.排序要求按照年龄,这里需要自己有比较器,CollectionSort即为自己创建的比较器,他实现了Comparator接口中的compare方法         * */        Collections.sort(al, new CollectionSort());        for(Student p:al)  //打印出list        {        System.out.println(p.getName()+"的年龄"+p.getAge());        }    }}

HashSet类:

解释:
1.此类允许使用null。
2.没有重复的值。
3.学会使用迭代器。

/*HashSet范例*/import java.util.HashSet;import java.util.Iterator;import java.util.Random;/*HashSet范例*/public class Test06 {    public static void main(String[] args) {        HashSet<Integer> set = new HashSet<>();        Random random = new Random();        while(set.size()<10){            int i = random.nextInt(90);//返回的是两位数            set.add(i);        }        System.out.println(set);   //传入的可能有重复的值        System.out.println(set.size());        set.remove(50); //如果set张包含50那么删除。        System.out.println(set);        System.out.println();        //迭代器        Iterator<Integer> it = set.iterator(); //返回迭代器,set中不会有重复的值        while(it.hasNext()){            System.out.println(it.next());        }        System.out.println();        System.out.println(set.size());    }}/*结果:*/[64, 17, 2, 50, 22, 43, 45, 46, 30, 47]10[64, 17, 2, 22, 43, 45, 46, 30, 47]641722243454630479
/*小知识*/for(Integer j:set){ //冒号前面j表示下面定义的变量,冒号后面表示集合    System.out.println(j);}

HashMap类:

1.构造:HashMap<Integer,Student> hm = new HashMap<>();
HashMap

import java.util.HashMap;import java.util.Iterator;import java.util.Set;public class Test_HashMap {    public static void main(String[] args) {        HashMap<Integer,Student> hm = new HashMap<>();        hm.put(0, new Student("张丹"));        hm.put(1, new Student("张三"));        hm.put(null, new Student("王五"));        //hm.put(4, null);        System.out.println(hm.size()); //得到hm的长度        /*通过转成set,获得键值来获取value。         *   Set<K> keySet()  返回此映射中所包含的键的 Set 视图。          */         Set<Integer> keys = hm.keySet(); //1.通过keySet获得键值视图         Iterator it = keys.iterator();  //2.通过迭代器         while(it.hasNext()){             Integer key = (Integer) it.next();             System.out.println(hm.get(key).getName());//获得student的名字         }    }}/*结果:3张丹王五张三*/

三、总结:

1.list集合判断元素是否相同,根据元素的equals方法,当需要改变判断条件时,就重写equals方法。
2.Set集合中:
HashSet的比较:通过hashcode和equals方法。
TreeSet的比较:通过比较器Comparable接口
3.TreeSet:
1)底层数据结构是二叉树,保证元素唯一的依据:compareTo方法 return 0 ;
2)排序的第一种防止:让自身具备比较性,元素要实现Compare接口,覆盖compareTo方法。(这种方式也成为默认顺序)

0 0