首页 > 技术文章 > Java Collection

dear_diary 2017-03-26 11:06 原文

Java API 所提供的一系列类的实例,用于在程序中存放对象。

Collection接口

定义了存取一组对象的方法,其子接口 Set 和 List 分别定义了存储方式。

Collection接口中所定义的方法:

容器类对象在调用remove、contains 等方法时需要比较对象是否相等,这会涉及到对象类型的 equals 方法和 hashCode 方法;对于自定义的类型,需要重写 equals 和 hasCode 方法以实现自定义对象相等规则。

示例:

class Name{
    private String firstName,lastName;
    public Name(String firstName,String lastName){
        this.firstName=firstName;
        this.lastName=lastName;
    }

    public String getFirstName(){return firstName;}
    public String getLastName(){return lastName;}

    public String toString(){return firstName +" " +lastName;}  //打印的时候会自动编译

    public boolean equals(Object obj){
        if(obj instanceof Name){
            Name name = (Name) obj;
            return (firstName.equals(name.firstName))
                    &&(lastName.equals(name.lastName));
        }
        return super.equals(obj);
    }

    public int hashCode(){
        return firstName.hashCode();
    }
}

 

 

Iterator接口

  • 所有实现了Collection接口的容器都有一个iterator方法用以返回一个实现了Iterator接口的对象。
  • Iterator对象称做迭代器,用以方便的实现对容器内元素的遍历操作
  • Iterator接口定义了如下方法:

实例:

import java.util.*;
public class test1 {
    public static void main(String[] args){
        Collection c = new HashSet();
        c.add(new Name("f1","l1"));
        c.add(new Name("f2","l2"));
        c.add(new Name("f2","l2"));
        Iterator i = c.iterator();
        while(i.hasNext()){
            Name n = (Name) i.next();
            System.out.println(n);
        }
    }
}

 

  • Set接口是Collection的子接口, Set接口中的数据对象没有顺序且不可以重复,Set接口没有提供额外的方法。(HashSet, TreeSet等)
  • List 中的数据对象有顺序且可以重复。(ArrayList,LinkedList等),List提供的方法:

List常用算法:

Comparable接口

所有可以“排序”的类都实现了 java.lang.Comparable 接口,Comparable 接口中只有一个方法:

public int compareTo(Object obj)
//返回0表示 this == obj
//返回正数表示 this > obj
//返回负数表示 this < obj
  • 实现了 Comparable 接口的类通过实现 comparaTo 方法从而确定该类对象的排序方式。
  • 改写 Name 类如下:
import java.util.*;
import java.lang.Comparable;
class Name implements Comparable{
    .....
        public int compareTo(Object o) {
            Name n = (Name)o;
            int lastCmp = lastName.compareTo(n.lastName);
            return (lastCmp!=0 ? lastCmp :firstName.compareTo(n.firstName));
        }
}

 test示例:

public class test{
    public static void main(String[] args){
        List l1 = new LinkedList();
        l1.add(new Name("Karl","M"));
        l1.add(new Name("Steven","Lee"));
        l1.add(new Name("John","O"));
        l1.add(new Name("Tom","M"));
        System.out.println(l1);
        Collections.sort(l1);
        System.out.println(l1);
    }
}

 输出:

[Karl M, Steven Lee, John O, Tom M]
[Steven Lee, Karl M, Tom M, John O]

 如何选择数据结构:

  • Array读快改慢
  • List改快读慢
  • Hash两者之间

Map接口

  • 定义了存储“键(key) -- 值(value)映射对“的方法。
  • Map接口的实现类有 HashMap 和 TreeMap。
  • Map的键值对通过键来标识,所有键值不能重复。

方法:

 

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class Test_Map
{
    public static void main(String[] args)
    {
        HashMap map = new HashMap();

        map.put("a","aaaa");
        map.put("b","bbbb");
        map.put("c","cccc");
        map.put("d","dddd");

        Set set = map.keySet();

        for(Iterator it = set.iterator(); it.hasNext();)
        {
            String key = (String)it.next();
            String value = (String)map.get(key);
            System.out.println(key+"键对应值"+value);
        }
    }
}

 

Auto-boxing/unboxing

  • 在合适的时机自动打包、解包(自动基础类型转换为对象、自动将对象转换为基础类型)

推荐阅读