9 HashSet HashCode 迭代器 TreeSet Colletions类 HashMap


1. 当向ArrayList添加一个对象时,实际上就是将该对象放置到了ArrayList底层所维护的数组当中;当向LinkedList中添加一个对象时,实际上LinkedList内部会生成一个Node对象,该Node对象的结构为
void linkLast(E e) {
        final Node<E> l = last;
        final Node<E> newNode = new Node<>(l, e, null);
        last = newNode;
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
    }
private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

其中的Object类型的元素element就是我们向LinkedList中所添加的元素,然后Node又构造好了向前与向后的引用previous、next,最后将生成的这个Node对象加入到了链表当中。换句话说,LinkedList中所维护的是一个个的Node对象。

2. 关于Object类的equals方法的特点

a) 自反性:x.equals(x)应该返回true
b) 对称性:x.equals(y)为true,那么y.equals(x)也为true。
c) 传递性:x.equals(y)为 true并且y.equals(z)为true,那么x.equals(z)也应该为true。
d) 一致性:x.equals(y)的第一次调用为true,那么x.equals(y)的第二次、第三次、第n次调用也应该为true,前提条件是在比较之间没有修改x也没有修改y。

e) 对于非空引用x,x.equals(null)返回false。


3. 关于Object类的hashCode()方法的特点:


a) 在Java应用的一次执行过程当中,对于同一个对象的hashCode方法的多次调用,他们应该返回同样的值(前提是该对象的信息没有发生变化)。


b) 对于两个对象来说,如果使用equals方法比较返回true,那么这两个对象的hashCode值一定是相同的


c) 对于两个对象来说,如果使用equals方法比较返回false,那么这两个对象hashCode值不要求一定不同(可以相同,可以不同),但是如果不同则可以提高应用的性能。


d) 对于Object类来说,不同的Object对象的hashCode值是不同的(Object类的hashCode值表示的是对象的地址)。

4. 当使用HashSet时,hashCode()方法就会得到调用,判断已经存储在集合中的对象的hash code值是否与增加的对象的hash code值一致;如果不一致,直接加进去;如果一致,再进行equals方法的比较,equals方法如果返回true,表示对象已经加进去了,就不会再增加新的对象,否则加进去。

HashSet set = new HashSet();
		
//		set.add(new People("zhangsan"));
//		set.add(new People("lisi"));
//		set.add(new People("zhangsan"));
		
//		People p1 = new People("zhangsan");
//		
//		set.add(p1);
//		set.add(p1);
		
		String s1 = new String("a");
		String s2 = new String("a");
		
		System.out.println("hash code: " + (s1.hashCode() == s2.hashCode()));
		
		set.add(s1);
		set.add(s2);
		
		System.out.println(set)

String的HashCode方法:

/**
     * Returns a hash code for this string. The hash code for a
     * {@code String} object is computed as
     * <blockquote><pre>
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]  String类型的HasCode计算法
     * </pre></blockquote>
     * using {@code int} arithmetic, where {@code s[i]} is the
     * <i>i</i>th character of the string, {@code n} is the length of
     * the string, and {@code ^} indicates exponentiation.
     * (The hash value of the empty string is zero.)
     *
     * @return  a hash code value for this object.
     */
    public int hashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;

            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }



5. 如果我们重写一个类的equals方法,那么也要重写hashCode方法,反之亦然。

重写HashCode是为了在集合中应用。

Eclipse 自动重写HashCode()和equals();Source->Generate....

迭代器:(类似于GIS里面的cursor)

通常希望循环通过类集中的元素。例如,可能会希望显示每一个元素。到目前为止,处理这个问题的最简单方法是使用iterator,iterator是一个或者实现Iterator或者实现ListIterator接口的对象。Iterator可以完成循环通过类集,从而获得或删除元素。ListIterator扩展Iterator,允许双向遍历列表,并可以修改单元.


在通过迭代函数访问类集之前,必须得到一个迭代函数。每一个Collection类都提供一个iterator()函数,该函数返回一个对类集头的迭代函数。通过使用这个迭代函数对象,可以访问类集中的每一个元素,一次一个元素。通常,使用迭代函数循环通过类集的内容,步骤如下
–1. 通过调用类集的iterator( )方法获得对类集头的迭代函数。
–2. 建立一个调用hasNext( )方法的循环,只要hasNext( )返回true,就进行循环迭代。
–3. 在循环内部,通过调用next( )方法来得到每一个元素

public static void main(String[] args)
	{
		HashSet hash = new HashSet();
		hash.add("I");
		hash.add(" Miss ");
		hash.add(" You ");
		hash.add(" WangBingJia");
		Iterator iterator = hash.iterator();
		while (iterator.hasNext())
		{
			System.out.println(iterator.next().toString());
		}
	}
TreeSet 有顺序的集合,使用前通常需要定义 Comparator,即实现Comparator接口中Compare方法

public class TreeSetTest
{
	
	public static void main(String[] args)
	{
		Compare compare=new Compare();
		TreeSet ts=new TreeSet(compare);
		ts.add((new Student(10)));
		ts.add(new Student(20));
		System.out.println(ts);
		
	}
	

}
class Compare implements Comparator
{

	@Override
	public int compare(Object o1, Object o2)
	{
		// TODO Auto-generated method stub
		Student s1=(Student) o1;
		Student s2=(Student) o2;
		if (s1 .socore<s2.socore)
		{
			return -1;
		}
		else if (s1 .socore==s2.socore)
		{
			return 0;
		}
		else {
			return 1;
		}
		
	}
	}
class Student
{
	 int socore;
	Student(int socore){
		this.socore=socore;
	}
	@Override
	public String toString()
	{
		// TODO Auto-generated method stub
		return Integer.toString(socore);
	}
	}

Collections类的一些静态方法

reverseOrder() 反序排列

shuffle  打乱顺序

public static void main(String[] args)
	{
		LinkedList list = new LinkedList();
		
		list.add(new Integer(-8));
		list.add(new Integer(20));
		list.add(new Integer(-20));
		list.add(new Integer(8));
		
		Comparator r = Collections.reverseOrder();
		
		Collections.sort(list, r);
		
		for(Iterator iter = list.iterator(); iter.hasNext();)
		{
			System.out.println(iter.next() + " ");
		}
		
		System.out.println();
		
		Collections.shuffle(list);
		
		for(Iterator iter = list.iterator(); iter.hasNext();)
		{
			System.out.println(iter.next() + " ");
		}
		
		System.out.println("minimum value: " + Collections.min(list));
		System.out.println("maximum value: " + Collections.max(list));
	}

6. Map(映射):实现类HashMap

Map的keySet()方法会返回key的集合,因为Map的键是不能重复的因此keySet()方法的返回类型是Set;而Map的值是可以重复的,因此values()方法的返回类型是Collection,可以容纳重复的元素。


HashMap hashMap=new HashMap();
hashMap.put("a","Bingjia");
hashMap.put("a","I");
hashMap.put("b","Bingjia");
System.out.println(hashMap);


通过hashMap的KeySet方法,实现对hashMap的遍历

Set set=hMap.keySet();
		Iterator iterator=set.iterator();
		while (iterator.hasNext())
		{
			System.out.println(hMap.get(iterator.next()));
			
		}

HashMap中储存的是一个一个Map.Entry对象,每一个Map.Entry维护了一对key和value

所以对HashMap的遍历可以遍历取出Map.Entry

hMap.put("1","I");
		hMap.put("2","Miss");
		hMap.put("3","You");
		hMap.put("4","WangBingjia");
		
		
		Set set =hMap.entrySet();
		Iterator iterator=set.iterator();
		while (iterator.hasNext())
		{
			Map.Entry entry=(Entry) iterator.next();
			System.out.println("key "+entry.getKey()+" values "+ entry.getValue());
			
		}





利用hashMap统计数字

HashMap hMap=new HashMap();
for(int i=0;i<args.length;i++)
{
if (hMap.containsKey(args[i]))
{
Object o1= hMap.get(args[i]);
Integer integer=(Integer )o1;
integer ++;
hMap.put(args[i],integer);

}
else {
hMap.put(args[i],1);
}
}
System.out.println(hMap);





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值