java hashMap, ArrayList

一. List接口

    ArrayList,对象加入之后大都是为了取出,而不会长做删除或插入的动作,则使用ArrayList效率会更加好,但是经常在容器里面做删除添加动作,则使用LinkList会更加好(该类是利用链表实现的),故增加了像addFirst()、addLast()、getFirst()、getLast()、removeFirst()、removeFast()等,这样适合实现堆栈和队列。

二、Set接口

List容器中允许重复的对象,但是Set容器中的对象都是唯一的,介入Set 容器必须重新定义equals(),hasCode()方法,Set容器有自己一套排序规则,其规则是根据哈希法,所以加入HashSet容器的对象还必须重新定义hasCode()方法.

Set<String> set=new HashSet<String>();
Iterator iterator=set.iterator();
while(iterator.hasNext()){    iterator.next()   };

也可以利用加强型进行访问元素:for(String name : set){  System.out.println(name);  }

如果想要以加入的顺序显示,则可以用LinkedHashSet,此类是HashSet的子类,它在内部类实现时使用哈希玛进行排序但是在迭代的行为上像LinkList那样.

TreeSet实现了Set接口和SortSet接口,SortSet提供的相关方法让您有序的去处对应位置的对象,像first()、last()等方法,TreeSet是J2SE中唯一实现了SortSet接口的类,它使用了红黑树结构来加入对象将进行排序。如果对对象有自己的一套排序,要定义一个实现java.util,Comparator接口的对象,要实现compare()方法.

三、Map接口

Map接口的对象会将键(Key)映射至值(Value),值指的是要存入Map容器的对象,其对象不能有重复的键,Map拥有自己的排序机制。

HashMap实现Map接口,在内部实现使用了哈希法,如果在迭代的时候想要以添加的顺序来排序,则可以用HashMap子类LinkedHashMap。

其访问对象的代码:

Map<String,String> map=new HashMap<String,String>();
for(String value :map.values){
System.out.println(value);
}
TreeMap实现Map接口和SortMap接口,SortMap提供相关的方法让你有序地取出对应位置的对象,像firstKey(),lastKey()等方法,TreeMap是唯一实现SortedMap接口的类,它使用了红黑树结构来对加入的对象进行排序。

四、hashCode的作用





 

   小心内存泄露问题

用容器的时候为什么要覆盖equals()和hasCode()方法?上面是解释的原因

什么时候需要覆盖hashCode方法?

下面用例子进行证明:

package cn.itcast.day1;

import java.util.Date;

public class ReflectPoint {
	private Date birthday = new Date();
	private int x;
	public int y;
	public String str1 = "ball";
	public String str2 = "basketball";
	public String str3 = "itcast";
	
	public ReflectPoint(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final ReflectPoint other = (ReflectPoint) obj;
		if (x != other.x)
			return false;
		if (y != other.y)
			return false;
		return true;
	}
	@Override
	public String toString(){
		return str1 + ":" + str2 + ":" + str3;
	}
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
}

package cn.itcast.day1;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;

public class ReflectTest2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		/*getRealPath();
		一定要记住用完整的路径,但完整的路径不是硬编码,而是运算出来的。*/
		//InputStream ips = new FileInputStream("config.properties");
		//InputStream ips = ReflectTest2.class.getClassLoader().getResourceAsStream("cn/itcast/day1/config.properties");
		//InputStream ips = ReflectTest2.class.getResourceAsStream("resources/config.properties");
	/*	InputStream ips = ReflectTest2.class.getResourceAsStream("/cn/itcast/day1/resources/config.properties");
		Properties props = new Properties();
		props.load(ips);
		ips.close();
		String className = props.getProperty("className");
		Collection collections = (Collection)Class.forName(className).newInstance();
		*/
		Collection collections = new HashSet();
		ReflectPoint pt1 = new ReflectPoint(3,3);
		ReflectPoint pt2 = new ReflectPoint(5,5);
		ReflectPoint pt3 = new ReflectPoint(3,3);	
		collections.add(pt1);
		collections.add(pt2);
		collections.add(pt3);
		collections.add(pt1);
		//pt1.y = 7;		
		//collections.remove(pt1);
		
		System.out.println(collections.size());
	}

}
转自:http://he-wen.iteye.com/blog/760263



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值