2013年7月31日 星期三

[JAVA]比對Collection的值是否一樣

可以用containsKey去檢查map的key是否一樣
如此就不用一個一個去比對


http://www.roseindia.net/tutorial/java/core/hashMap/containsKey.html

HashMapContainsKey.java
package net.roseindia.java;
import java.util.HashMap;
public class HashMapContainsKey {
public static void main(String[] arr) {
/* Create object of HashMap */
HashMap<Integer, String> obMap = new HashMap<Integer, String>();
/* Add value in HashMap */
obMap.put(new Integer(1), "Bharat");
obMap.put(new Integer(2), "Gyan");
obMap.put(new Integer(4), "Vrishti");
obMap.put(new Integer(3), "Sarika");
obMap.put(new Integer(5), "Rohit");
obMap.put(new Integer(6), "Parineeta");
/* Check key present in HashMap or not */
System.out.println("Returns :" + obMap.containsKey(1));
System.out.println("Returns :" + obMap.containsKey(8)); } }

沒有留言:

張貼留言