當(dāng)前位置:
首頁(yè) >
Java从零开始学二十三(集合Map接口)
發(fā)布時(shí)間:2025/7/25
69
豆豆
生活随笔
收集整理的這篇文章主要介紹了
Java从零开始学二十三(集合Map接口)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、Map接口
Collection、Set、List接口都屬于單值的操作,即:每次只能操作一個(gè)對(duì)象,而Map與它們不同的是,每次操作的是一對(duì)對(duì)象,即二元偶對(duì)象,Map中的每個(gè)元素都使用key à value的形式存儲(chǔ)在集合之中二、常用方法
| No. | 方法或類 | 類型 | 描述 |
| 1 | public void clear() | 普通 | 清空Map集合 |
| 2 | public boolean containsKey(Object key) | 普通 | 判斷指定的key是否存在 |
| 3 | public boolean containsValue(Object value) | 普通 | 判斷指定的value是否存在 |
| 4 | public Set<Map.Entry<K,V>> entrySet() | 普通 | 將Map對(duì)象變?yōu)镾et集合 |
| 5 | public boolean equals(Object o) | 普通 | 對(duì)象比較 |
| 6 | public V get(Object key) | 普通 | 根據(jù)key取得value |
| 7 | public int hashCode() | 普通 | 返回哈希碼 |
| 8 | public boolean isEmpty() | 普通 | 判斷集合是否為空 |
| 9 | public Set<K> keySet() | 普通 | 取得所有的key |
| 10 | public V put(K key, V value) | 普通 | 向集合中加入元素 |
| 11 | public void putAll(Map<? extends K,? extends V> t) | 普通 | 將一個(gè)Map集合中的內(nèi)容加入到另一個(gè)Map |
| 12 | public V remove(Object key) | 普通 | 根據(jù)key刪除value |
| 13 | public int size() | 普通 | 取出集合的長(zhǎng)度 |
| 14 | public Collection<V> values() | 普通 | 取出全部的value |
三、例子
package com.pb.demo2;public class Person {private String name;private int age;public Person() {}public Person(String name, int age) {this.name = name;this.age = age; }public String getName() {return name; } public void setName(String name) {this.name = name; } public int getAge() {return age; } public void setAge(int age) {this.age = age; }} package com.pb.demo2;import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set;import com.pb.demo1.Person;public class MapTest {public static void main(String[] args) {/** 1.創(chuàng)建多個(gè)Person對(duì)象并賦值*/Person p1 = new Person("張三",21);Person p2 = new Person("李四",22);Person p3 = new Person("王五",23);Person p4 = new Person("趙六",24);Person p5 = new Person("錢七",25);//2.創(chuàng)建保存鍵--值對(duì)的集合對(duì)象Map<String,Person> perMap=new HashMap<String,Person>();//3.使用put將英文名與對(duì)象按照鍵-值對(duì)的方式在座存儲(chǔ)在HashMap中perMap.put("Denny", p1);perMap.put("Jony", p2);perMap.put("Rose", p3);perMap.put("Kitter", p4);perMap.put("Boby", p5);//4.打印鍵集System.out.println("++++++++打印鍵集+++++++++");System.out.println(perMap.keySet());//5.打印值集System.out.println("=========打印值集=======");System.out.println(perMap.values());//6.打印鍵-值對(duì)集合System.out.println("=========打印鍵-值對(duì)集合=======");System.out.println(perMap);//7.判斷是否存在"Denny"這個(gè)鍵if(perMap.containsKey("Denny")){//8.如果存在,根據(jù)鍵獲取相應(yīng)的值Person p =perMap.get("Denny");System.out.println("姓名: "+p.getName());System.out.println("年齡: "+p.getAge());}System.out.println("=======遍歷HashMap=======");//遍歷HashMap//首先遍歷key集合,keySet方法返回的是Set集合Set<String> keySet=perMap.keySet();Iterator<String> iterator=keySet.iterator();while(iterator.hasNext()){String key=iterator.next();System.out.print("英文名:"+key);//根據(jù)key值取出值Person p =perMap.get(key);System.out.print("\t姓名: "+p.getName());System.out.println("\t年齡: "+p.getAge());}}}?
轉(zhuǎn)載于:https://www.cnblogs.com/liunanjava/p/4300094.html
總結(jié)
以上是生活随笔為你收集整理的Java从零开始学二十三(集合Map接口)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: zoj 2760 How Many Sh
- 下一篇: Java并发基础框架AbstractQu