Java容器、泛型



Collection: 存放独立元素的序列。


Map:存放key-value型的元素对。(这对于一些需要利用key查找value的程序十分的重要!)


Collection定义了Collection类型数据的最基本、最共性的功能接口,而List对该接口进行了拓展。


其中各个类的适用场景有很大的差别,在使用时,应该根据需要灵活的进行选择。此处介绍最为常用的四个容器:


LinkedList :其数据结构采用的是链表,此种结构的优势是删除和添加的效率很高,但随机访问元素时效率较ArrayList类低。


ArrayList:其数据结构采用的是线性表,此种结构的优势是访问和查询十分方便,但添加和删除的时候效率很低。


HashSet: Set类不允许其中存在重复的元素(集),无法添加一个重复的元素(Set中已经存在)。HashSet利用Hash函数进行了查询效率上的优化,其contain()方法经常被使用,以用于判断相关元素是否已经被添加过。


HashMap: 提供了key-value的键值对数据存储机制,可以十分方便的通过键值查找相应的元素,而且通过Hash散列机制,查找十分的方便。



/**********************************************************************************************************************************/

Array读快改慢

Linked改快读慢

Hash两者之间


/**
*Iterator接口:所有实现了Collection接口的容器类都有一个iterator方法
*用以返回一个实现了Iterator接口的对象
*Iterator对象称作迭代器,用以方便的实现对容器内元素的遍历操作
*/
import java.util.*;
public class Test
{
	public static void main (String[] args)
	{
		Collection c = new HashSet();
		c.add(new Name("f1","l1"));
		c.add(new Name("f21","l2"));
		c.add(new Name("f321","l3"));
		
		Iterator i = c.iterator();
		while (i.hasNext())
		{
			//next()的返回值为Object类型,需要转换为相应类型
			Name name = (Name)i.next();
			if(name.getFirstName().length()<3)
			{
				i.remove();
			}
		System.out.print(name.getFirstName()+" ");
		}
		System.out.println("\n"+c);
	}
}
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;
	}
}


/**
*map接口和自动打包解包
*/
import java.util.*;
public class TestMap
{
	public static void main(String args[])
	{
		Map m1 = new HashMap();
		Map m2 = new HashMap();
		//m1.put("one",new Integer(1));
		m1.put("one",1);//jdk1.5开始支持在合适位置自动打包
		m1.put("two",2);
		m1.put("three",3);
		
		m2.put("A",1);
		m2.put("B",2);
		
		System.out.println(m1.size());
		System.out.println(m1.containsKey("one"));
		//System.out.println(m2.containsValue(new Integer (1));
		System.out.println(m2.containsValue(1));
		
		if (m1.containsKey("two"))
		{
			//int i = ((Integer)m1.get("two")).intValue();
			int i =(Integer)m1.get("two");//自动解包
			System.out.println(i);
		}
		
		Map m3 = new HashMap(m1);
		m3.putAll(m2);
		System.out.println(m3);
	}
}

泛型:使集合能够记住集合内元素各类型,且能够达到只要编译时不出现问题,运行时就不会出现“java.lang.ClassCastException”异常

/**
*泛型
*/
import java.util.*;
public class TestMap2
{
	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 =(Integer)m1.get("two").intValue();int i =(Integer)m1.get("two").intValue();
			int i =m1.get("two");
			System.out.println(i);
		}
	}
}

详见:http://www.cnblogs.com/lwbqqyumidi/p/3837629.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值