在一组数据中匹配是否含有某数据

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;




public class test {

	public static void main(String[] args) {
		List<String> sList = new ArrayList<String>();
		Set<String> set = new HashSet<String>();
		StringBuilder stringBuilder = new StringBuilder();
		
		String test = null;
		
		
		for (int i = 0; i < 10000; i++) {
			for (int j = 0; j < 1000; j++) {
				sList.add("jiujiujiu" + i + "i" + j + "j");
				set.add("jiujiujiu" + i + "i" + j + "j");
				stringBuilder.append("jiujiujiu" + i + "i" + j + "j");
				test = "jiujiujiu" + i + "i" + j + "j";
			}
		}
		
		System.out.println("测试开始》》》》");
		
		
		long startTime1 = System.currentTimeMillis(); 
		for (String string : sList) {
			if (string.equals(test)) {
				long endTime = System.currentTimeMillis(); 
				System.out.println("增强for循环,List消耗时间:" + (endTime - startTime1));
			}
		}
		
		long startTime2 = System.currentTimeMillis(); 
		for (int i = 0; i < 10000000; i++) {
			if (sList.get(i).equals(test)) {
				System.out.println("普通for循环,List消耗时间:" + (System.currentTimeMillis() - startTime2));
			}
		}
		
		long startTime3 = System.currentTimeMillis(); 
		for (String string : set) {
			if (string.equals(test)) {
				System.out.println("增强for循环,Set消耗时间:" +  (System.currentTimeMillis() - startTime3));
			}
		}
		
		long startTime4 = System.currentTimeMillis(); 
		if (set.contains(test)) {
			System.out.println("Set.contains()消耗时间:" +  (System.currentTimeMillis() - startTime4));
		}
		
		String cString = stringBuilder.toString();
		long startTime5 = System.currentTimeMillis(); 
		if (cString.contains(test)) {
			System.out.println("String.contains()消耗时间:" +  (System.currentTimeMillis() - startTime5));
		}
		
		
	}
	
}

控制台输出:

 

为啥Set.contains()这个方法这么快呢?

刚刚set是由Hashset实现的,所以这个contains()方法其实是hashset里面的方法

 


//看HashSet中的部分源码   

/**
* HashSet中几乎所有的方法都是调用了HashMap中关于key的方法
* 所以也可以说HashSet就是HashMap中的Key
*/

public class HashSet<E>{

    private transient HashMap<E,Object> map;

    public HashSet() {
        map = new HashMap<>();
    }

    public boolean contains(Object o) {
        return map.containsKey(o);          
    }
}

原来是调用了HashMap中的containsKey()方法,往下看这个方法的实现

//HashMap中部分源码

/**
* 通过计算得出的hash值来匹配获取key,然后进行比较
* 所以极大的节省了时间
*/

public class HashMap<K,V>{

    public boolean containsKey(Object key) {
        return getEntry(key) != null;
    }

    final Entry<K,V> getEntry(Object key) {
        if (size == 0) {
            return null;
        }

        int hash = (key == null) ? 0 : hash(key);
        for (Entry<K,V> e = table[indexFor(hash, table.length)];
             e != null;
             e = e.next) {
            Object k;
            if (e.hash == hash &&
                ((k = e.key) == key || (key != null && key.equals(k))))
                return e;
        }
        return null;
    }


    final int hash(Object k) {
        int h = hashSeed;
        if (0 != h && k instanceof String) {
            return sun.misc.Hashing.stringHash32((String) k);
        }

        h ^= k.hashCode();

        // This function ensures that hashCodes that differ only by
        // constant multiples at each bit position have a bounded
        // number of collisions (approximately 8 at default load factor).
        h ^= (h >>> 20) ^ (h >>> 12);
        return h ^ (h >>> 7) ^ (h >>> 4);
    }
}

综上:

需要在一组数据集合中匹配是否含有某数据时,可选择存入HashSet中,使用利用HashMap中的containsKey()方法来提高效率。

【HashMap中效率高的原因,可查阅数据结构中哈希表的实现原理。】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值