6.Map
6.1put()方法
public class Map01 {
public static void main(String[] args) {
Map map = new HashMap();
map.put("1","英语");
map.put("2","中文");
map.put("2","中文1");//当有相同的key时,等价于替换
map.put("3","英语");//value可以重复
System.out.println(map);
}
}
6.2 源码分析
public class Map02_MapSource {
public static void main(String[] args) {
Map map= new HashMap();
map.put("1","英语");
map.put("2","中文");
/* 1.k-v 最后是 HashMapNode node=newNode(hash,key,value,null)
2.k-v 为例便于程序员的遍历,还会创建EntrySet集合,该集合的类型是Entrya,而一个Entry对象就有k,v,封装在EntrySet<k,v>
* 3.在EntrySet 中,定义的类型是Map.Entry,但是实际上存放的是HashMapNode
4.当吧HashMapNode对象存放到EntrySet就方便我们的遍历,因为Map,Entry提供了重要方法
K GetKey();V get Value();
* */
Set set = map.entrySet();
System.out.println(set.getClass());//Has hMap$EntrySet
for (Object obj : set) {
// System.out.println(o.getClass());//HashMap$Node
Map.Entry entry=(Map.Entry) obj;
System.out.println(entry.getKey()+"-"+entry.getValue());
}
}
}
6.3 遍历方法
public class Map03_MapTraverse {
public static void main(String[] args) {
Map map= new HashMap();
map.put("1","英语");
map.put("2","中文");
// 第一组: 西安取出所有的key,再根据key取出value
// 1,先取所有的key,在根据key找出对应的value
Set set = map.keySet();
System.out.println("1111111111111");
for (Object o : set) {
System.out.println(map.get(o));
}
// 2.迭代器
System.out.println("2222222222222222");
Iterator iterator = set.iterator();
while (iterator.hasNext()){
Object key= iterator.next();
System.out.println(key+"-"+map.get(key));
}
// 第二组:把所有value取出
// 1.增强for
System.out.println("3333333333333333");
Collection values = map.values();
for (Object value : values) {
System.out.println(value);
}
// 2.迭代器
System.out.println("44444444444444");
Iterator iterator2 = values.iterator();
while (iterator2.hasNext()) {
Object next = iterator2.next();
System.out.println(next);
}
System.out.println("5555555555");
Set entrySet = map.entrySet();
for (Object o : entrySet) {
Map.Entry m=(Map.Entry) o;
System.out.println(m.getKey()+"-"+m.getValue());
}
Iterator iterator1 = entrySet.iterator();
while (iterator1.hasNext()){
Object next = iterator1.next();
System.out.println(next.getClass());
}
}
}
6.4 Map方法
public class Map04_MapMethod {
public static void main(String[] args) {
HashMap map = new HashMap();
// put()方法
System.out.println("======");
map.put("1","English");
map.put("2","简体中文");
map.put("1","英语");
map.put("3","日语");
System.out.println(map);
// remove()
System.out.println("===remove()===");
map.remove(1);
System.out.println(map);
// get()
System.out.println("===get()===");
System.out.println(map.get(3));
// size()
System.out.println("===size()===");
System.out.println(map.size());
// isEmpty()
System.out.println("===isEmpty()===");
System.out.println(map.isEmpty());
// ContainsKey()
System.out.println("===ContainsKey()===");
System.out.println(map.containsKey("1"));
// clear()
System.out.println("===clear()===");
map.clear();
}
}
6.5 练习
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WCWfem2I-1659967542402)(…/img/image-20220807223830338.png)]
public class Map05_Excesice {
public static void main(String[] args) {
Map hashMap = new HashMap();
hashMap.put(1,new Emp("Jack", 6000, 1));
hashMap.put(2,new Emp("Tom", 3000, 2));
hashMap.put(3,new Emp("Sary", 8000, 5));
// 增强for
System.out.println("=======增强for======");
for (Object key : hashMap.keySet()) {
Emp emp =(Emp) hashMap.get(key);
System.out.println(emp.toString());
}
// 迭代器
System.out.println("=======迭代器======");
Set set = hashMap.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()){
Map.Entry next = (Map.Entry) iterator.next();
Emp emp = (Emp) next.getValue();
System.out.println(emp.toString());
}
}
}
class Emp{
private String name;
private double sal;
private int age;
public Emp(String name, double sal, int age) {
this.name = name;
this.sal = sal;
this.age = age;
}
@Override
public String toString() {
return "Emp{" +
"name='" + name + '\'' +
", sal=" + sal +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
6.6 HashMap
public class Map06_HashMap01 {
public static void main(String[] args) {
Map map= new HashMap();
map.put("1","英语");
map.put("2","中文");
System.out.println(map);
}
}
public class Map07_HashMap02Source {
public static void main(String[] args) {
HashMap hashMap = new HashMap();
for (int i = 1; i <= 12; i++) {
hashMap.put(new A(i),i);
}
System.out.println(hashMap);
}
}
class A{
private int num;
public A(int num) {
this.num = num;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
@Override
public String toString() {
return '\n'+"A{" +
"num=" + num +
'}';
}
}
7.HashTable
-
存放的元素是键值对:即K-V
-
hashtable的键和值都不能为null,否则会抛出NullPointerException
-
hashTable使用方法基本上和HashMap一样
-
hashTable是线程安全的(synchronized). hashMap 是线程不安全的
public class Hashtable01 { public static void main(String[] args) { /* * 1.底层是一个数组 HashTableEntry[] 初始化大小为11 * 2.临界值 threshold =8 11*0.75 * 3.扩容:按照自己的扩容机制进行即可 * 4。执行方法: addEntry(hash, key, value, index);添加k-v到Entry * private void addEntry(int hash, K key, V value, int index) { modCount++; Entry<?,?> tab[] = table; if (count >= threshold) { // Rehash the table if the threshold is exceeded rehash(); tab = table; hash = key.hashCode(); index = (hash & 0x7FFFFFFF) % tab.length; } // Creates the new entry. @SuppressWarnings("unchecked") Entry<K,V> e = (Entry<K,V>) tab[index]; tab[index] = new Entry<>(hash, key, value, e); count++; } * */ Hashtable hashtable = new Hashtable(); hashtable.put("John",100); hashtable.put("lucy",100); hashtable.put("Lic",100); hashtable.put("Lic",200);//替换 for (int i = 0; i < 5; i++) { hashtable.put("hello"+i,i); } hashtable.put("5","5"); System.out.println(hashtable); } }