Java 集合

1.集合的体系结构图

Java 集合_第1张图片


1.List集合

java的List和C#的IList类似

ArrayList: 底层数据结构是数组,查询快,增删慢。 线程不安全,效率高。

Vector: 底层数据结构是数组,查询快,增删慢。 线程安全,效率高。(单线程被ArrayList替代)

LinkList: 底层数据结构是链表,查询慢,增删快。 线程不安全,效率高。

简单的集合遍历

 //使用迭代器遍历list集合
    public static void  iteratorTest() {
        List list=new ArrayList();
        list.add("clow1");
        list.add("jop1");
        Iterator iterator=list.iterator();
        while (iterator.hasNext()){
            System.out.println((String)iterator.next());
        }
    }

    //ListIterator是Iterator的子类,它有previous和add功能
    public static void  listIteratorTest() {
        List list=new ArrayList();
        list.add("clow3");
        list.add("jop3");
        ListIterator iterator=list.listIterator();
        //在遍历的适合添加一个元素
        while (iterator.hasNext()){
            String s=(String)iterator.next();
            if("clow3".equals(s)){
                iterator.add("bellows");
            }
        }
        System.out.println(list); //[clow3, bellows, jop3]
    }

    //使用for遍历list集合
    public static void listForTest() {
        List list=new ArrayList();
        list.add("clow2");
        list.add("alex2");
        list.add("jop2");
        //注意:list里面没有和C#类似的索引(list[i])
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        list.add(1,"bellows2");
        System.out.println(list);
    }

1.2 集合对象排序

Java的排序和C#的类似,提供了2个比较好用的接口Comparator和Comparable。

如下是根据Student的年龄升序序,若年龄相同,则再根据name字符串升序。

1.实体类基础Comparable,重写compareTo方法。

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

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

    @Override
    public int compareTo(Student s) {
        int r1=this.age-s.age;
        return r1!=0 ? r1:this.name.compareTo(s.name);
    }

    @Override
    public String toString() {
        return this.name+" "+ String.valueOf(this.age);
    }
}

public class ListDemo2 {
    public static void main(String[] args) {
        Student student1 = new Student("clow1", 20);
        Student student2 = new Student("clow2", 40);
        Student student3 = new Student("clow3", 30);
        Student student4 = new Student("2clow4", 10);
        Student student5 = new Student("1clow4", 10);
        List<Student> list = new ArrayList<Student>();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);
        list.add(student5);
        Collections.sort(list);
        for (Student s : list) {
            System.out.println(s);
        }
//        1clow4 10
//        2clow4 10
//        clow1 20
//        clow3 30
//        clow2 40

    }
}

2.comparator,需要定义一个实现了Comparator的子类并重写了其compare方法

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

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return this.name+" "+ String.valueOf(this.age);
    }
}


public class ListDemo2 {
    public static void main(String[] args) {
        Student student1 = new Student("clow1", 20);
        Student student2 = new Student("clow2", 40);
        Student student3 = new Student("clow3", 30);
        Student student4 = new Student("2clow4", 10);
        Student student5 = new Student("1clow4", 10);
        List<Student> list = new ArrayList<Student>();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);
        list.add(student5);
        //使用了局部内部类的方法实例化了一个实现了Comparator接口的子类对象
        list.sort(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int r = o1.getAge() - o2.getAge();
                return r != 0 ? r : o1.getName().compareTo(o2.getName());
            }
        });
        for (Student s : list) {
            System.out.println(s);
        }
//        1clow4 10
//        2clow4 10
//        clow1 20
//        clow3 30
//        clow2 40

    }
}



2.Set集合

Set集合的特定:无序,唯一

2.1 Hashset集合

A:底层数据结构是哈希表(是一个元素为链表的数组)
B:哈希表底层依赖两个方法:hashCode()和equals()
 执行顺序:
首先比较哈希值是否相同
相同:继续执行equals()方法
返回true:元素重复了,不添加
返回false:直接把元素添加到集合
不同:就直接把元素添加到集合
C:如何保证元素唯一性的呢?
由hashCode()和equals()保证的
D:开发的时候,代码非常的简单,自动生成即可。
E:HashSet存储字符串并遍历
F:HashSet存储自定义对象并遍历(对象的成员变量值相同即为同一个元素)

2.2TreeSet集合

A:底层数据结构是红黑树(是一个自平衡的二叉树)
B:保证元素的排序方式
a:自然排序(元素具备比较性)
让元素所属的类实现Comparable接口
b:比较器排序(集合具备比较性)
让集合构造方法接收Comparator的实现类对象


3. Map 集合

Map类似于C#里面的Dictionary,用来存储键值对

Map的子类有HashMap TreeMap ,这些都可以对应参考Set下的相应子类

public class MapDemo {
    public static void main(String[] args) {
        Map<String,String> map=new HashMap<String,String>();
        map.put("clow","123");
        map.put("jop","456");
        map.put("alex","789");
        Set<String> setKeys=map.keySet();
        for (String key:setKeys){
            String num=map.get(key);
            System.out.println(key+"---"+num);
        }
//        alex---789
//        jop---456
//        clow---123
    }
}




你可能感兴趣的:(Java 集合)