《Thinking in java》阅读笔记4—集合

myhalfsea posted @ 2011年2月22日 09:11 in JAVA , 1212 阅读

集合不等同于容器,只是容器中的一部分。

集合分为3大类:List、Map、Set。其中每一种之下又有不同的实现。比如常见的,List下有ArrayList、LinkedList、Vector,Map下有HashMap、TreeMap、HashTable,Set下有HashSet, TreeSet等等。

ArrayList:

ArrayList其实就是用数组保存数据,加入数据时,如果数组已满,就新建一个数组,其长度为原来的2倍。

平时我们在遍历ArrayList时,喜欢用迭代器Iteretor,向后移动。还有一个值得注意的迭代器ListIterator<E>,之前居然完全不知道有这个东东,其只能用于List,可以向后、向前移动。还可以改变当前只想的值。如下例子:

public class Test{
    public static void main(String[] args) {
        Test t = new Test();
        List list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        ListIterator<Integer> it = list.listIterator();
        while( it.hasNext() ){
            Integer i = it.next();
            System.out.println(it.nextIndex()+":"+ i);
            if( i.equals(2)) it.set(22);
        }
        Iterator it2 = list.iterator();
        while( it2.hasNext() ){
            System.out.println(it2.next());
        }
    }
}

输出:

1:1
2:2
3:3
1
22
3

Vector:

Vector中的数据不允许重复,其容量也是可变的,Vector 的大小可以根据需要增大或缩小,以适应创建 Vector 后进行添加或移除项的操作。 Vector是线程安全的,在不用考虑现成安全的情况下,通常用ArrayList

关于HashTable 和 HashMap的区别:

1、HashTable中,键或值必须是非null对象

2、HashTable是同步的,是线程安全的;若果使用:Map m = Collections.synchronizedMap(new HashMap(...)),HashMap才是线程安全的,如果直接是Map m = new HashMap(...),则它必须 保持外部同步。

Set:

set内部元素不能重复,无次序,set实际上是一颗红黑树,最常用的set是hashSet,用hashSet存放对象时,该对象必须重写equals()方法,同事也需要实现hashCode()方法,不然就达不到set的目的(即判断重复)。举例说明:

package test;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class TestSort{
    public static void main(String[] args){
        Set<Student> hs=new HashSet<Student>();
        hs.add(new Student("rose",2));
        hs.add(new Student("Jack",5));
        hs.add(new Student("Lily",22));
        hs.add(new Student("Jack",5));
        Iterator<Student> it=hs.iterator();
        while(it.hasNext()){
            System.out.print(it.next().name+" ");
        }
    }
}
class Student{
     int age;
     String name;
     public Student(String name,int age){
         this.name=name;
         this.age=age;
     }
     public int hashCode(){   //返回一个能够唯一标识一个对象的值
         return age+name.hashCode();
     }
     public boolean equals(Object o){// 这两个方法一定要同时实现
        Student st=(Student)o;
        return age==st.age && name.equals(st.name);
     }
}

输出:Lily Jack rose

优先队列:如果队列里面存放的是对象的引用,那么该对象必须实现Comparable,重写compareTo()方法。例如:

import java.util.PriorityQueue;

public class TestSort extends PriorityQueue<TestSort.Point>{
    
    static class Point implements Comparable<Point>{ //实现Comparable接口
       
        private Integer a;
        Point(){}
        Point( Integer a){
            this.a = a;
        }
        public Integer getA() {
            return a;
        }
        public void setA(Integer a) {
            this.a = a;
        }
        @Override
        public int compareTo(Point o) { //重写compareTo方法
            if( a > o.a ) return 1;
            if( a == o.a ) return 0;
            return -1;
        }
    }
    
    public static void main(String[] args) {
        TestSort t = new TestSort();
        t.add(new Point(10));
        t.add(new Point(21));
        t.add(new Point(3));
        t.add(new Point(55));
        while( !t.isEmpty() ){
            System.out.print(t.remove().a+" ");
        }
       
    }
}

输出:3 10 21 55

 

 

seo service london 说:
2023年12月09日 22:37

 I care for such information much. I was seeking this certain information for a long time. Thank you and good luck


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta
Butterfly Theme | Design: HRS Hersteller of mobile Hundeschule.