您的位置:首页 > 职场人生

黑马程序员-集合类总结-No.03

2011-12-11 13:17 253 查看
----------------------
android培训、java培训、期待与您交流! ----------------------

1、Java的集合类主要由两个接口派生而出:Collection和Map,Colletion下面派生List和Set接口,Colletion保存数据的特点是单个值,而Map保存的数据都是key-value对。集合类实例中是不可以添加基本数据类型的,只能存对象。

2、List:有序集合,元素可以重复,有索引

!——ArrayList:数据结构使用的是数组结构,查询快,增删慢,长度可变;

!——LinkedList:底层数据结构使用的链表,增删速度快,查询慢,

Set:无序集合,元素不可以重复

!——HashSet:数据机构是哈希表,线程非同步,元素值可以是null;存放到此集合中的对象应该重写hashCode()和equals(),hashCode值是判断元素是否一致的唯一标准,因采用hash算法,效率比较高。

!——TreeSet:采用二叉树数据结构,可对Set集合中的元素排序;存放到此集合中的对象要么实现Comparable接口,要么可以在构造TreeSet对象的时候传递一个实现了Comparator接口的比较器对象。关于比较器的设计,需要实现Comparetor接口。参考代码:

public static void main(String[] args){
	 TreeSet ts = new TreeSet(new DemoComp()); //传一个比较器对象
	 	·········
}
	//自定义比较器
class DemoComp implements Comparator{
	public int compare(Object o1,Object o2){
		············//具体比较方法,返回正数、0、负数
	}
}

3、迭代器Iterator接口,用来遍历Collection集合元素,只有三个方法:

boolean hasNext():如果集合中还有没被遍历的,返回true;

Object next():返回集合里的下一个元素;

void remove():删除集合里上一次next方法返回的元素。

例:一段获取迭代器和遍历的方法

//获取集合对应的迭代器,ts指具体集合对象
Iterator it = ts.iterator();
while(it.hasNext()){
	System.out.println(it.next());//具体操作代码
}

4、LinkedList集合有自己特有迭代器ListIterator,ListIterator不仅有上述3个方法,还有其它操作集合的方法,如add()插入,previous()返回列表中的前一个元素等特殊方法。

5、Map:存储的是键值对,且保证键的唯一性

!——Hashtable:哈希表数据结构,不可以存入null键和null值,线程同步;

!——HashMap:哈希表数据结构,可以存null键和null值,线程不同步;

!——TreeMap:实现SortedMap接口,底层二叉树,不同步,对key排序。

Map集合类没有遍历集合的直接方法,只能通过Map对象的keySet()获取键的Set集合,在通过Set集合的迭代器获取键和值,或者调用entrySet()获取键和值的映射关系再存入到Set集合中,分别同Map.Entry接口中方法getKey()和getValue()获取键和值。当然,既然也可以通过增强for循环获取,具体三种方法代码如下:

package com.xiongmc.Collection;
/*
 * 存入学生(姓名,年龄,地址)
 * 按学生姓名拼音排序
 * 学生姓名和年龄相同视为同一个人
 */
import java.util.*;
public class HashMapTest {
	@SuppressWarnings("unchecked")
	public static void main(String[] args){
		Map<Students,String> hs = new HashMap();
		hs.put(new Students("zhangsan",23), "xizang");
		hs.put(new Students("lisi",20), "xinjiang");
		hs.put(new Students("wangwu",25), "neimenggu");
		hs.put(new Students("yangma",19), "guangxi");
		
		//第一种取出方式
		Set<Students> keyset = hs.keySet();
		Iterator<Students> it = keyset.iterator();
		while(it.hasNext()){
			Students s = it.next();
			System.out.println(s.toString()+".."+hs.get(s));
		}
		
		//第二种取出方式
		Set<Map.Entry<Students,String>> en = hs.entrySet();
	    Iterator<Map.Entry<Students, String>> iter = en.iterator();
	    while(iter.hasNext()){
	    	Map.Entry<Students,String> me = iter.next();
	    	Students stu = me.getKey();
	    	String addr = me.getValue();
	    	System.out.println(stu+"..."+addr);
	    }
	    
	    //第三种取出方式
	    for(Object key : hs.keySet()){
	    	System.out.print(key+"--->");
	    	System.out.println(hs.get(key));
	    }
	}
}

class Students implements Comparable<Students>{
	private String name;
	private int age;
	public Students(String name,int age){
		this.name = name;
		this.age = age;
	}
	
	public int compareTo(Students s){
		int num = this.name.compareTo(s.name);
		if(num==0){
			return new Integer(this.age).compareTo(s.age);
		}
		return num;
	}
		
	//以下hashCode()和equals()保证键的唯一性
	public int hashCode(){
		return name.hashCode()+age*5;
	}
	//重写equals方法
	public boolean equals(Object obj){
		if(!(obj instanceof Students)){
			throw new ClassCastException("类型不匹配!");
		}
		Students s = (Students)obj;
		return this.name.equals(s.getName()) && this.age==s.getAge();
	}
	
	//类的基本方法定义
	public String getName(){
		return name;
	}
	public int getAge(){
		return age;
	}
	public String toString(){
		return name+".."+age;
	}
}

6、Collections类,可以对Set、List和Map等集合进行操作的工具类,常用方法:

static void shuffle(List list):对list集合元素进行随机排序,eg:洗牌;

static void reverse(List list):反转list集合元素的顺序;

static void sort(List list,Comparator c):根据指定的比较器对list进行排序;

static void swap(List list,int i,int j):对list集合中的i处和j处元素互换;

static int binarySearch(List list,Object key):使用二分查找法找出key在list中的索引;

static Comparator<T> reverseOrder(比较器):强行逆转比较器;

static boolean replaceAll(List list , 旧对象,新对象):使用新对象替换list中所有指定的旧对象。

---------------------- android培训java培训、期待与您交流! ----------------------

详细请查看:http://edu.csdn.net/heima
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: