public class Demo {
public static void main(String[] args) {
HashMap hashMap = new HashMap();
//重复向数组中加入hash值相等,但对象不同的元素
for (int i = 0; i <8 ; i++) {//同一节点位置形成一条8个长度的链表
hashMap.put(new A("s"),100);
}
hashMap.put(new A("s"),100);//再增加同一节点,节点数组扩容至32个
hashMap.put(new A("s"),100);//再增加同一节点,节点数组扩容至个
hashMap.put(new A("s"),100);//再增加同一节点,这个链表转化成红黑树!
System.out.println(hashMap);
}
}
class A{
private String name;
public A(String name) {
this.name = name;
}
@Override
public int hashCode() {
return 100;
}
}
public class Demo {
@SuppressWarnings("all")
public static void main(String[] args) {
//定义一个Map类
Map haMa = new HashMap();
haMa.put("no1","ssss");
haMa.put("no4","swx");
haMa.put("no8","gsdsd");
haMa.put("no13","gsds");
haMa.put("no1222","sddsg");
//第一种遍历:利用Map.keySet
Set set = haMa.keySet();
//1.增强for
for (Object key:set) {
System.out.println(key + "-"+haMa.get(key));
}
System.out.println();
//2.迭代器
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
System.out.println(key + "-"+haMa.get(key));
}
System.out.println();
//第二种遍历:利用Map.Values
Collection v1 = haMa.values();
//增强for
for (Object value:
v1) {
System.out.println(value);
}
System.out.println();
//迭代器
Iterator iterator1 = v1.iterator();
while (iterator1.hasNext()) {
Object value = iterator1.next();
System.out.println(value);
}
System.out.println();
//第三种遍历:map.entrySet()
Set set1 = haMa.entrySet();
//1.增强for
//set1里面数据类型是entry,向上转型用Object接收
for (Object entry:set1
) {
//向下转型,用Map.Entry接收,就可以使用Map.Entry里面的方法
Map.Entry en = (Map.Entry) entry;
System.out.println(en.getKey()+"-"+en.getValue());
}
System.out.println();
//2.迭代器
Iterator iterator2 = set1.iterator();
while (iterator2.hasNext()) {
Object entry = iterator2.next();
//向下转型,用Map.Entry接收,就可以使用Map.Entry里面的方法
Map.Entry en = (Map.Entry) entry;
System.out.println(en.getKey()+"-"+en.getValue());
}
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- igbc.cn 版权所有 湘ICP备2023023988号-5
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务