美文网首页
Collection(集合)

Collection(集合)

作者: Veteor | 来源:发表于2016-09-25 13:02 被阅读0次

Collection接口

Collection接口包括List接口和Set接口

1 list接口: 包括三个实现类 (集合元素可以重复)

  • ArrayList类:通过依赖数组实现的,对随机访问的性能很好,但进行大量插入,删除操作,性能很差, 因为操作之后后续元素需要移动 。
  • LinkedList类:内部是依赖双链表实现的, 因此有很好的插入和删除性能,但随机访问元素的性能很差。
  • Vector类:非常类似ArrayList,相对于ArrayList和LinkdeList它是线程安全的
    <blockquote>
线程不安全转化为线程安全
list = Collections.synchronizedList(list);

2 Set接口: 包括一个子接口和三个实现类(集合元素不能重复)

  • HashSet类:里面不能存放重复元素,而且采用散列的存储方法,所以没有顺序。这里所说的没有顺序是指:元素插入的顺序与输出的顺序不一致。
    <blockquote>
public static void main(String[] args) {
        Set<Integer> set = new HashSet<Integer>();
        set.add(3);
        set.add(4);
        set.add(7);
        set.add(5);
        set.add(4);
        set.add(34);
        for(int  i : set)
            System.out.print(i+" ");
    }
运行结果:
34 3 4 5 7 
  • LinkedHashSet类:LinkedHashSet集合同样是根据元素的hashCode值来决定元素的存储位置,但是它同时使用链表维护元素的次序。这样使得元素看起 来像是以插入顺序保存的,也就是说,当遍历该集合时候,LinkedHashSet将会以元素的添加顺序访问集合的元素
  • TreeSet类:实现了SortSet接口(对TreeSet中的元素排序有两种方法)
    1.TreeSet放入对象的时候该对象的类实现Comparable接口(TreeSet无参数构造)
    2.利用外部类实现Comparator接口(TreeSet有参数构造)
    <blockquote>
//实现Comparable接口===================
class Person1 implements Comparable<Person1>{
    private String name;
    private int age;
    private int score;
    public Person1(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    public int getScore() {
        return score;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + score;
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof Person1))
            return false;
        Person1 p = (Person1) obj;
        if(p.age == this.age&&this.name==p.name&&this.score==p.score)
            return true;
        return false;
    }
    @Override
    public String toString() {
        return "Person1 [name=" + name + ", age=" + age + ", score=" + score + "]";
    }
    @Override
    public int compareTo(Person1 o) {
        // TODO Auto-generated method stub
        return o.getName().compareTo(this.getName());
    }
}
//实现Comparator接口===============================
class sortNameComparator implements Comparator<Person1>{
@Override
    public int compare(Person1 o1, Person1 o2) {
        // TODO Auto-generated method stub
        if(o1.getName().equals(o2.getName()))
            return 1;
        return o1.getName().compareTo(o2.getName());
    }
}
public class LearnComparator{
    public static void main(String [] args){
        System.out.println("按名字排序-------------");
        Set<Person1> personSet1 = new TreeSet<Person1>(new sortNameComparator());
        personSet1.add(new Person1("cat",13,30));
        personSet1.add(new Person1("dog",17,70));
        personSet1.add(new Person1("monky",15,90));
        personSet1.add(new Person1("cat",16,55));
        personSet1.add(new Person1("mouse",14,35));
        for(Person1 P:personSet1)
            System.out.println(P);
  //System.out.println("按名字排序-------------");
//  无参数的TreeSet构造方法
//      Set<Person1> personSet1 = new TreeSet<Person1>();
//      personSet1.add(new Person1("cat",13,30));
//      personSet1.add(new Person1("dog",17,70));
//      personSet1.add(new Person1("monky",15,90));
//      personSet1.add(new Person1("cat",16,55));
//      personSet1.add(new Person1("mouse",14,35));
//      for(Person1 P:personSet1)
//          System.out.println(P);
//  }
}
运行结果:
按名字排序-------------
Person1 [name=cat, age=13, score=30]
Person1 [name=cat, age=16, score=55]
Person1 [name=dog, age=17, score=70]
Person1 [name=monky, age=15, score=90]
Person1 [name=mouse, age=14, score=35]

相关文章

  • Java基础之常用集合

    集合分为单列集合(Collection)和双列集合(Map),先说单列集合Collection Collecti...

  • JAVA基础---Collection集合 List

    ##一 Collection集合 ### 01 集合体系结构 a:Collection 集合(单身汉集合) ...

  • 哪些类继承了Collection接口

    Collection集合的基本结构: 1、Collection接口 Collection是最基本集合接口,它定义了...

  • 3/10day07_Collection_迭代器_泛型_数据结构

    今日内容 Collection集合 迭代器 泛型 数据结构 Collection集合[重点] 集合的介绍,集合和数...

  • 集合框架

    Collection Collection集合是单列集合的顶层接口。Collection表示一组对象,这些对象也称...

  • 用流收集数据

    Collection、Collector和Collect的区别: Collection:Collection是集合...

  • Java Collection集合

    Collection集合 collection集合作为List集合和Set集合共有的父类拥有着List集合和Set...

  • Java实战开发篇-8 集合

    集合 一、简介 1.集合分为Collection集合和List集合(1)Collection是集合类的一个接口,它...

  • JAVA基础(五)

    集合的由来? 集合和数组的区别? Collection集合的功能概述? Collection集合存储字符串并遍历?...

  • JJJJJava集合

    集合与数组的区别 Collection集合的方法 常用集合的分类 Collection 接口的接口 对象的集合(单...

网友评论

      本文标题:Collection(集合)

      本文链接:https://www.haomeiwen.com/subject/xerfyttx.html