package collection;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* 打印在数组中出现n/2以上的元素
* 利用一个HashMap来存放数组元素及出现的次数
* @author Administrator
*
*/
public class HashMapTest {
public static void main(String[] args) {
//int [] a = {2,3,2,2,1,4,2,2,2,7,9,6,2,2,3,1,0};
int[] a = new int[]{2,3,2,2,1,4,2,2,2,7,9,6,2,2,3,1,0};
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < a.length; i++) {
if (map.containsKey(a[i])) {
int temp = map.get(a[i]);
temp+=1;
map.put(a[i], temp);
}else {
map.put(a[i], 1);
}
}
//输出元素
Set<Integer> set = map.keySet();
for (Integer integer : set) {
if (map.get(integer) >= a.length/2) {
System.out.println(integer);
}
}
//遍历方法1
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("key="+entry.getKey()+
"---->value="+entry.getValue().toString());
}
//遍历方法2
Iterator<Integer> it = map.keySet().iterator();//取得键对象
while (it.hasNext()) {
System.out.println("it.hashNext的数据值是:"+ map.get(it.next()));//获得键所对应的值
}
//遍历方法3
Iterator<Entry<Integer, Integer>> i = map.entrySet().iterator();
while (i.hasNext()) {
Entry<Integer, Integer> entry = i.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(key + ":" + value);
}
//遍历方法4
Collection<Integer> c = map.values();
Iterator it2 = c.iterator();
for (; it2.hasNext();) {
System.out.println(it2.next());
}
//利用keyset进行遍历,它的优点在于可以根据你所想要的key值得到你想要的 values,更具灵活性!!
Set<Integer> key = map.keySet();
for(Iterator it3 = key.iterator();it3.hasNext();){
int n = (Integer) it3.next();
System.out.println(map.get(n));
}
//比较复杂的一种遍历在这里,呵呵~~他很暴力哦,它的灵活性太强了,想得到什么就能得到什
Set<Map.Entry<Integer, Integer>> set2 = map.entrySet();
for(Iterator<Map.Entry<Integer, Integer>> it4=set2.iterator();it4.hasNext();){
Map.Entry<Integer, Integer> entry = it4.next();
System.out.println(entry.getKey()+" : "+entry.getValue());
}
}
}
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* 打印在数组中出现n/2以上的元素
* 利用一个HashMap来存放数组元素及出现的次数
* @author Administrator
*
*/
public class HashMapTest {
public static void main(String[] args) {
//int [] a = {2,3,2,2,1,4,2,2,2,7,9,6,2,2,3,1,0};
int[] a = new int[]{2,3,2,2,1,4,2,2,2,7,9,6,2,2,3,1,0};
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < a.length; i++) {
if (map.containsKey(a[i])) {
int temp = map.get(a[i]);
temp+=1;
map.put(a[i], temp);
}else {
map.put(a[i], 1);
}
}
//输出元素
Set<Integer> set = map.keySet();
for (Integer integer : set) {
if (map.get(integer) >= a.length/2) {
System.out.println(integer);
}
}
//遍历方法1
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("key="+entry.getKey()+
"---->value="+entry.getValue().toString());
}
//遍历方法2
Iterator<Integer> it = map.keySet().iterator();//取得键对象
while (it.hasNext()) {
System.out.println("it.hashNext的数据值是:"+ map.get(it.next()));//获得键所对应的值
}
//遍历方法3
Iterator<Entry<Integer, Integer>> i = map.entrySet().iterator();
while (i.hasNext()) {
Entry<Integer, Integer> entry = i.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(key + ":" + value);
}
//遍历方法4
Collection<Integer> c = map.values();
Iterator it2 = c.iterator();
for (; it2.hasNext();) {
System.out.println(it2.next());
}
//利用keyset进行遍历,它的优点在于可以根据你所想要的key值得到你想要的 values,更具灵活性!!
Set<Integer> key = map.keySet();
for(Iterator it3 = key.iterator();it3.hasNext();){
int n = (Integer) it3.next();
System.out.println(map.get(n));
}
//比较复杂的一种遍历在这里,呵呵~~他很暴力哦,它的灵活性太强了,想得到什么就能得到什
Set<Map.Entry<Integer, Integer>> set2 = map.entrySet();
for(Iterator<Map.Entry<Integer, Integer>> it4=set2.iterator();it4.hasNext();){
Map.Entry<Integer, Integer> entry = it4.next();
System.out.println(entry.getKey()+" : "+entry.getValue());
}
}
}