九、java容器

目录

一、容器的概念

、Cpllection接口

、Iterator接口

、增强的for循环

、Set接口

、List接口和Comparable接口

、Map接口

、自动打包/解包

十、泛型(JDK1.5以后新增)

一、容器的概念和容器API

容器:java API所提供的一系列的实例,用于在程序中存放对象

JDK所提供的容器API位于java.util包内

容器API的类图结构如下图所示:

image

左边的容器都是一个个往里装,右边的容器也就是map这一边的是一对一对往里装

Collection接口定义了存取一组对象的方法,他代表了一个集合,他下面分了两个子接口set和list。

装东西的时候set是没有顺序并且不可以重复,list是有顺序可以重复,所谓的重复是指两个对象equals

Map接口定义了存储“键(key) –值(value)”  映射对的方法

、Collection接口

  • Collection接口中所定义的方法:
int size();
boolean isEmpty();
void clear();
boolean contains(Object element);//查看是否包含某一个对象
bllean add(Object element);
boolean remove(Object element);
Iterator iterator();
boolean containsAll(collection c);
bllean addAll(Collection c);
boolean removeAll(Collection c);
boolean retainAll(Collection c);//交集
object[] toArray();

看一个例子:

public class Test{
    
    public static void main(String[] args) {
        Collection c = new ArrayList();//父类引用指向子类对象
        //可以放入不同类型的对象
        c.add("hello");
        c.add(new Name("f1","l1"));
        c.add(new Integer(100));        
        System.out.println(c.size());
        System.out.println(c);
    }    
    
}
//输出结果:
//3
//[hello, f1 l1, 100]
public class Test{
    
    public static void main(String[] args) {
        Collection c = new ArrayList();
        c.add("hello");
        c.add(new Name("f1","l1"));
        c.add(new Integer(100));
        c.remove("hello");
        c.remove(new Integer(100));
        System.out.println(c.remove(new Name("f1","l1")));
        System.out.println(c);
    }        
}
//输出结果:
//false
//[f1 l1]
//因为两个Name("f1","l1")指向的不是同一个对象,所以equals方法返回false

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

注意:相等的对象应该具有相等的hashCode

增加Name类的equals和hashCode方法如下:

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();
}

这样那个例子代码结果就会变为true 和[]了

、Iterator接口 

  • 所有实现了collection接口的容器类都由一个interator方法用以返回一个实现了Interator接口的对象
  • Interator对象称作迭代器,用以方便的实现对容器内元素的遍历操作
  • Iterator接口定义了如下方法:
boolean hasNext();//判断游标右边是否有元素

Object next();//返回游标右边的元素并将游标移动到下一个位置

void remove();//删除游标左面的元素,在执行完next之后

image

看一个例子:

public class Test{
    
    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("f3","l3"));
        //遍历
        Iterator i = c.iterator();
        while(i.hasNext()) {
            //由于next()的返回值为Object类型,需要转换为相应类型
            Name n = (Name)i.next();
            System.out.print(n.getFirstName()+" ");
        }        
    }
}

//输出结果
//f1 f2 f3
  • Iterator对象的remove方法是在迭代过程中删除元素的唯一的安全方法
public class Test{
    
    public static void main(String[] args) {
        
        Collection c = new HashSet();
        c.add(new Name("ffff1","lllll1"));
        c.add(new Name("f2","l2"));
        c.add(new Name("fff3","llll3"));
        
        for (Iterator i = c.iterator(); i.hasNext();) {
            Name name = (Name)i.next();
            if (name.getFirstName().length()<3) {
                i.remove();
                //如果换成c.remove(name);会产生例外
            }
        }
        
        System.out.println(c);
    }
}

//输出结果:[fff3 llll3, ffff1 lllll1]

 

、增强的for循环

增强的for循环对于遍历array或Collection的时候非常简便

看一个例子先:

public class Test{
    
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        for(int i : arr) {
            System.out.print(i + " ");//遍历输出
        }
        
        System.out.println();
        
        Collection c = new ArrayList();
        c.add(new String("aaa"));
        c.add(new String("bbb"));
        c.add(new String("vvv"));
        for (Object o : c) {
            System.out.print(o + " ");//遍历输出
        }
    }
}

缺陷:

  • 对于数组来说不能方便的访问下标值
  • 对于集合来说,与使用Iterator来说,不能方便的删除集合中的内容(在for循环内部也调用Itertor)

所以,除了简单遍历并读出其中的内容外,不建议使用增强for

、Set接口

Set接口是Collection的子接口,Set接口没有提供额外的方法,但实现Set接口的容器类中的元素是没有有顺序的,而且不可以重复

Set容器可以与数学中的集合的概念相对应

J2SDK 的API中所提供的Set容器类有HashSwt,TreeSet等

 

看一个例子:

public class Test{
    
    public static void main(String[] args) {
        
        Set s = new HashSet();
        s.add("hello");
        s.add("world");
        s.add(new Name("f1", "l1"));
        s.add(new Integer(100));
        s.add(new Name("f1", "l1"));//相同元素不会被加入
        s.add("hello");//相同元素不会被加入
        System.out.print(s + "    ");
    }
}
//输出结果:[world, 100, hello, f1 l1]

再看一个例子:

public class Test{
    
    public static void main(String[] args) {
        
        Set s1 = new HashSet();
        Set s2 = new HashSet();
        
        s1.add("a");
        s1.add("b");
        s1.add("c");
        
        s2.add("a");
        s2.add("b");
        s2.add("d");
        
        Set sn = new HashSet(s1);
        sn.retainAll(s2);//交集 
        Set su = new HashSet(s1);
        su.addAll(s2);
        
        System.out.println("su = "+ su);
        System.out.println("sn = "+ sn);
        
    }
}
//输出结果:
//su = [a, b, c, d]
//sn = [a, b]

 

、List接口和Comparable接口

1.List接口

List接口是Collection的子接口,实现List接口的俄容器类中的元素是有顺序的,而且可以重复

List的容器重点额元素都对应一个整数型的序号级再其在容器中的位置,可以根据序号存取容器中的元素

J2SDK所提供的List容器类有ArrayList,LinkedList等

Object get(int index);
Object set(int index,Object lement);
void add(int index,Object element);
Object remove(int index);
int indexOf(Object o);
int lastIndexOf(Object o);

看一个例子:

public class Test{
    
    public static void main(String[] args) {
        
        LinkedList l1 = new LinkedList();
        for (int i=0; i<=5; i++) {
            l1.add("a" + i);
        }
        System.out.println(l1);//[a0, a1, a2, a3, a4, a5]
        l1.add(3,"a100");
        System.out.println(l1);//[a0, a1, a2, a100, a3, a4, a5]
        l1.set(6, "a200");
        System.out.println(l1);//[a0, a1, a2, a100, a3, a4, a200]
        System.out.println((String)l1.get(2)+ " ");//a2
        System.out.println(l1.indexOf("a3"));//4
        l1.remove(1);
        System.out.println(l1);//[a0, a2, a100, a3, a4, a200]
        
    }
}

List常用算法

void sort(List) //对list容器内的元素排序
void shuffle(List) //对list容器内的对象进行随机排列
void reverse(List) //岁list容器内的对象进行逆序排列
void fill(List,Object) //用一个特定的对象重写整个List容器
void copy(List dest,list src) //将srcList容器内容拷贝到destList容器
void binarySearch(List,Object) //对于顺序的list容器,采用着办查找的方法查找特定对象

看一个例子:

public class Test{
    
    public static void main(String[] args) {
        
        LinkedList l1 = new LinkedList();
                
        for(int i=0; i<=9; i++) {
            l1.add("a" + i);
        }
        
        System.out.println(l1);//[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]
        Collections.shuffle(l1);//随机排列
        System.out.println(l1);//[a9, a2, a7, a4, a1, a5, a3, a6, a8, a0]
        Collections.reverse(l1);//逆序
        System.out.println(l1);//[a0, a8, a6, a3, a5, a1, a4, a7, a2, a9]
        Collections.sort(l1);//排序
        System.out.println(l1);//[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]
        System.out.println(Collections.binarySearch(l1, "a5"));//折半查找,结果为5        
        
    }
}

2.Comparable接口

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

public int compareTo(Object obj);

该方法:

  • 返回0表示this == obj
  • 返回正数表示this>obj
  • 返回负数表示this <obj

实现了Comparable接口的类通过实现compataTo方法从而确定该类对象的排序方式

、Map接口

实现Map接口的类用来存储键-值对

Map接口的实现类有HashMap和TreeMap等

Map类中存储的键-值对通过键来标识,所以键值不能重复

Object put(Object key,Object value);
Object get(Object key);
Object remove(Object key);
boolean containsKey(Object key);
boolean containsValue(Object value);
int size();
boolean isEmpty();
void putAll(Map t);
void clear();

看一个例子:

public class Test{
    
    public static void main(String[] args) {
        
        Map m1 = new HashMap();
        Map m2 = new TreeMap();
        
        m1.put("one", new Integer(1));
        m1.put("two", new Integer(2));
        m1.put("three", new Integer(3));
        
        m2.put("A", new Integer(1));
        m2.put("B", new Integer(2));
        
        System.out.println(m1.size());//3
        System.out.println(m1.containsKey("one"));//true
        System.out.println(m2.containsValue(new Integer(1)));//true
        
        if (m1.containsKey("two")) {
            int i = ((Integer)m1.get("two")).intValue();
            System.out.println(i);//2
        }
        
        Map m3 = new HashMap(m1);
        m3.putAll(m2);
        System.out.println(m3);//{A=1, B=2, two=2, three=3, one=1}
    }
}

 

、自动打包/解包

在合适的时机自动打包、解包(JDK1.5之后的版本支持)

  • 自动将基础类型转换为对象(打包)
  • 自动将对象转换为基础类型(解包)

如此,上面的例子可以这么写:

public class Test{
    
    public static void main(String[] args) {
        
        Map m1 = new HashMap();
        Map m2 = new TreeMap();
        
        m1.put("one", 1);
        m1.put("two", 2);
        m1.put("three", 3);
        
        m2.put("A", 1);
        m2.put("B", 2);
        
        System.out.println(m1.size());//3
        System.out.println(m1.containsKey("one"));//true
        System.out.println(m2.containsValue(1));//true
        
        if (m1.containsKey("two")) {
            int i = ((Integer)m1.get("two"));
            System.out.println(i);//2
        }
        
        Map m3 = new HashMap(m1);
        m3.putAll(m2);
        System.out.println(m3);//{A=1, B=2, two=2, three=3, one=1}
    }
}

看一个练习例子:

/*
 * 注释掉的代码是使用了自动打包和解包进行的代码简化
 */
public class Test{
    
    private static final Integer ONE = new Integer(1);
    //private static final int ONE = 1;

    public static void main(String[] args) {
        
        Map m = new HashMap();
        
        for(int i=0; i<args.length; i++) {
            Integer freq = (Integer)m.get(args[i]);
            //int freq = (Integer)m.get(args[i]);
            m.put(args[i], (freq == null ? ONE : new Integer(freq.intValue() +1)));
            //m.put(args[i], freq==0 ? ONE : freq + 1);
        }
        
        System.out.println(m.size() + " distinct words detected: ");
        System.out.println(m);
        
    }
}

这是一个统计传外部参次数的例子

十、泛型(JDK1.5以后新增)

起因:

JDK1.4以前类型不明确:

  • 装入集合的类型都被当作Object对待,从而时区自己的实际类型
  • 从集合中取出时往往需要转型,效率低,容易产生错误

解决办法:

  • 在定义集合的时候同事定义集合中对象的类型
  • 可以在定义Collection的时候指定
  • 也可以在循环时用Iterator指定

好处:

  • 增强程序的可读性和稳定性

看一个例子:

public class Test{    
    public static void main(String[] args) {
        
        List<String> c = new ArrayList<String>();
        c.add("aaa");
        c.add("bbb");
        c.add("ccc");        
        for(int i=0; i<c.size(); i++) {
            String s = c.get(i);
            System.out.println(s);
        }
        
        
        Collection<String> c2 = new HashSet<String>();
        c2.add("aaa");
        c2.add("bbb");
        c2.add("ccc");        
        for(Iterator<String> it = c2.iterator(); it.hasNext();) {
            String s = it.next();
            System.out.println(s);
        }    
    }
}

在看一个例子:

public class Test{    
    public static void main(String[] args) {
        
        Map<String,Integer> m1 = new HashMap<String,Integer>();
        m1.put("one", 1);
        m1.put("two", 2);
        m1.put("three", 3);
        
        System.out.println(m1.size());
        System.out.println(m1.containsKey("one"));
        
        if(m1.containsKey("two")) {            
            int i = m1.get("two");
            System.out.println(i);
        }        
    }
}
posted @ 2016-08-07 18:42  许忠慧  阅读(535)  评论(0编辑  收藏  举报