Map集合不繼承Collection,Map是映射鍵值對(duì),存儲(chǔ)對(duì)象的時(shí)候,一次性存儲(chǔ)兩個(gè)對(duì)象,一個(gè)作為鍵(K),一個(gè)作為值(V)。
區(qū)別:
Collection一個(gè)對(duì)象存儲(chǔ)方式是add
Map 兩個(gè)對(duì)象存儲(chǔ)方式 put()
Map集合中,不能出現(xiàn)重復(fù)鍵,每一個(gè)鍵最多只能映射一個(gè)值,但是不同鍵可以有相同值
一、Map中的方法
直接上demo
package map;
/*
* Map中的方法
*/
import java.util.*;
public class MapDemo {
public static void main(String[] args) {
/*method1();
method2();
method3();
method4();*/
method5();
}
/*
* put(k,v)將鍵值存儲(chǔ)到集合中
*/
public static void method(){
Map<String,Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 1);
map.put("c", 1);
map.put("d", 4);
map.put("e", 6);
System.out.println(map);
}
/*
* putAll(Map map)把一個(gè)集合全部插入另一個(gè)集合
*
*/
public static void method1(){
Map<String,Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 1);
map.put("c", 1);
Map<String,Integer> map2 = new HashMap<>();
map2.put("e", 2);
map2.put("f", 3);
map2.put("g", 4);
map.putAll(map2);
System.out.println(map);
}
/*
* V get(Key) 傳遞一個(gè)鍵,返回鍵對(duì)應(yīng)的值,如果此鍵不存在,返回為空
*/
public static void method2(){
Map<String,Integer> map = new HashMap<>();
map.put("a", 234);
map.put("b", 1);
map.put("c", 1);
System.out.println(map.get("z"));
}
/*
* void clear() 清除所有對(duì)象
* void isEmpty() 判斷是為空
* int size() 返回映射關(guān)系對(duì)的個(gè)數(shù)
* boolean containsKey(Key) 判斷鍵是否存在,存在返回真,否則返回假
* boolean containsValue(Value) 同理判斷值是否存在,存在返回真,不存在返回假
*/
public static void method3(){
Map<String,Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 123);
map.put("c", 124);
System.out.println(map.size());
System.out.println(map.containsKey("A"));
//自動(dòng)裝箱,會(huì)調(diào)用equals判斷,所以為真
System.out.println(map.containsValue(new Integer(124)));
map.clear();
System.out.println(map.isEmpty());
}
/*
* V remove(K)
* 移除指定的鍵值對(duì),傳遞鍵,返回值
* 沒(méi)有移除成功返回null
*/
public static void method4(){
Map<String,Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 123);
map.put("c", 124);
System.out.println(map.remove("a"));
System.out.println(map);
}
/*
* Collection<V> values()將Map中的所有值,存儲(chǔ)到Collection集合
*/
public static void method5(){
Map<String,Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 123);
map.put("c", 124);
Collection<Integer> c = new ArrayList<>();
c = map.values();
System.out.println(map);
System.out.println(c);
}
}
二、Map集合的取出方法
遍歷Map集合有兩種方法,一種是利用set集合遍歷,另外一種是利用內(nèi)部類原理進(jìn)行遍歷。
Demo
package map;
import java.util.*;
public class MapDemo1 {
/*
* 練習(xí)map的兩種數(shù)據(jù)取出方式
*/
public static void main(String[] args) {
method1();
}
/*
* 利用set取出
*/
public static void method(){
Map<String, Integer> map = new HashMap<>();
map.put("a",1);
map.put("b",2);
map.put("c",3);
map.put("d",4);
Set<String> set = map.keySet();
Iterator<String> it = set.iterator();
while(it.hasNext()){
System.out.println(map.get(it.next()));
}
}
/*
* 利用集合的鍵值對(duì)關(guān)系對(duì)象獲取 entrySet,這是一個(gè)內(nèi)部類
*/
public static void method1(){
Map<String, Integer> map = new HashMap<>();
map.put("a",1);
map.put("b",2);
map.put("c",3);
map.put("d",4);
Set<Map.Entry<String, Integer>> o = new HashSet<>();
o = map.entrySet();
Iterator<Map.Entry<String, Integer>> it = o.iterator();
while(it.hasNext()){
//利用多態(tài),next返回實(shí)現(xiàn)類對(duì)象給接口,可以不用管具體的實(shí)現(xiàn)類原理
Map.Entry<String, Integer> entry = it.next();
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
}
三、增強(qiáng)for循環(huán)間接遍歷map
實(shí)現(xiàn)的原理其實(shí)和上面的set遍歷相似,因?yàn)閙ap小的小弟們沒(méi)有實(shí)現(xiàn)接口Iterable,所有不能使用增強(qiáng)for循環(huán),因此map也就沒(méi)有辦法使用增強(qiáng)for循環(huán)來(lái)遍歷了。所以,借助Key的set集合來(lái)使用for循環(huán),從而間接遍歷可以獲取到map集合中的數(shù)據(jù),同樣,這種方式也可以和map的取出方法一樣,有兩種寫法。
demo
package map;
/*
* for循環(huán)遍歷間接遍歷map
*/
import java.util.*;
public class MapDemo2 {
public static void main(String[] args) {
Map<String, Integer> m = new HashMap<>();
m.put("a", 1);
m.put("b", 2);
m.put("c", 3);
//1.利用普通的set方法for循環(huán)遍歷,平時(shí)中推薦使用
/*for(String s : m.keySet()){
System.out.println(s+"----" + m.get(s));
}*/
//2.同樣,使用內(nèi)部類來(lái)進(jìn)行實(shí)現(xiàn)
for(Map.Entry<String, Integer> entry : m.entrySet()){
System.out.println(entry.getKey() + "..."+ entry.getValue());
}
}
}
四、HashMap類
基于哈希表的映射鍵值對(duì),線程不安全,可以存儲(chǔ)null值和null鍵,沒(méi)有順序。要求鍵是唯一的,對(duì)象必須重寫hashCode方法和equals方法。
demo
package map;
/*
* HashMap集合的存取自定義對(duì)象
* Person是之前自定義的對(duì)象
*/
import SetDemo.Person;
import java.util.*;
public class HashMapDemo {
public static void main(String[] args) {
HashMap<Person,String> hm = new HashMap<>();
hm.put(new Person("張三",22), "加拿大");
hm.put(new Person("李四",42), "澳大利亞");
hm.put(new Person("王五",71), "美國(guó)");
hm.put(new Person("趙六",54), "英國(guó)");
method1(hm);
}
/*
* 實(shí)現(xiàn)map集合的第一種獲取方法
*/
public static void method(HashMap<Person,String> hm){
Set<Person> set = hm.keySet();
Iterator<Person> it = set.iterator();
while(it.hasNext()){
Person p = it.next();
String s = hm.get(p);
System.out.println(p + ".." + s);
}
}
/*
* 第二種方法獲取,entry
*/
public static void method1(HashMap<Person,String> hm){
Set<Map.Entry<Person, String>> set = hm.entrySet();
Iterator<Map.Entry<Person, String>> it = set.iterator();
while(it.hasNext()){
Map.Entry<Person, String> e = it.next();
System.out.println(e.getKey() + ".." + e.getValue());
}
}
}
五、LinkedMap類
LinkedMap是HashMap的子類,基于鏈表的哈希表實(shí)現(xiàn),有序的Map集合,線程不安全的,開(kāi)始版本于JDK1.4。
package map;
import java.util.*;
/*
* LinkedHashMap的存儲(chǔ)和遍歷
* 按put的順序排序
*/
public class LinkedHashMapDemo {
public static void main(String[] args) {
LinkedHashMap<String, String> lhm = new LinkedHashMap<>();
lhm.put("a","65" );
lhm.put("c","67" );
lhm.put("b","668" );
lhm.put("A","786" );
lhm.put("d","786786" );
for(String key : lhm.keySet()){
System.out.println(lhm.get(key));
}
}
}
六、HashTable
底層數(shù)據(jù)結(jié)構(gòu)也是哈希表,但是不允許存儲(chǔ)null,線程安全,運(yùn)行速度慢。開(kāi)始版本JDK1.0,沒(méi)有集合框架以前,存儲(chǔ)鍵值對(duì),只能依靠Hashtable,被HashMap取代,郁郁而終了。但是他有一個(gè)子類Properties,依然活躍在開(kāi)發(fā)舞臺(tái),這個(gè)集合可以和IO流配合使用。
七、TreeMap集合
對(duì)存儲(chǔ)的鍵進(jìn)行排序,需要對(duì)象的自然順序,或者比較器,用法和實(shí)現(xiàn)代碼,直接參考HashSet集合,線程不安全集合,運(yùn)行速度快,底層實(shí)現(xiàn)紅黑樹(shù),自然平衡算法二叉樹(shù)。
package cn.itcast.map;
/*
* TreeMap存儲(chǔ)自定義對(duì)象
* 提供兩個(gè)排序方式,自然順序,一個(gè)是比較器
*/
import java.util.*;
import SetDemo.PersonComparator;
public class TreeMapDemo {
public static void main(String[] args) {
//傳遞一個(gè)自然比較對(duì)象進(jìn)去
TreeMap<Person,String> tm = new TreeMap<Person, String>(new PersonComparator());
tm.put(new Person("lisi",18), "加拿大");
tm.put(new Person("zhangsa",17), "澳大利亞");
tm.put(new Person("zhangsa",17), "澳大利亞");
tm.put(new Person("wangwu",20), "新加坡");
tm.put(new Person("zhaoliu",19), "新西蘭");
tm.put(new Person("zhaoliu",19), "新西蘭");
tm.put(new Person("lisa",22), "迪拜");
// keySet(tm);
entrySet(tm);
}
/*
* 增強(qiáng)for循環(huán),遍歷entrySet方式
*/
public static void entrySet(TreeMap<Person,String> hm){
for(Map.Entry<Person, String> entry : hm.entrySet()){
Person p = entry.getKey();
String value = entry.getValue();
System.out.println(p+"..."+value);
}
}
/*
* 增強(qiáng)for循環(huán),遍歷keySet方式
*/
public static void keySet(TreeMap<Person,String> tm){
for(Person p : tm.keySet()){
String value = tm.get(p);
System.out.println(p+"..."+value);
}
}
}
八、map集合的小練習(xí)
題目:統(tǒng)計(jì)一個(gè)字符串內(nèi)每個(gè)字母出現(xiàn)的順序,存儲(chǔ)在集合之中
package map;
/*
* 練習(xí):
* 統(tǒng)計(jì)字符串里面每個(gè)字符出現(xiàn)的次數(shù),存儲(chǔ)到map里面
* eg:a=1,b=3....
*
*/
import java.util.*;
public class HashMapTest {
public static void main(String[] args) {
String s = "xcvgbjhmklfdxhkuxck.jbjgcj,ckyfxckh";
System.out.println(method(s));
}
public static Map<Character,Integer> method(String s){
char[] c = s.toCharArray();
Map<Character,Integer> tongji = new HashMap<Character, Integer>();
for(int i = 0 ; i < c.length ; i++){
char temp = c[i];
if(#containsKey(temp)){
#put(temp,#get(temp)+1);
}
else{
#put(temp, 1);
}
}
return tongji;
}
}
九、Properties類
Hashtable的子類,也是一個(gè)線程安全的鍵值對(duì)的集合。這個(gè)類不同于其他集合,可以和IO流結(jié)合使用,實(shí)現(xiàn)數(shù)據(jù)的永久性存儲(chǔ)。查看API發(fā)現(xiàn)這個(gè)集合沒(méi)定義泛型,泛型已經(jīng)被設(shè)定好了,鍵值對(duì)的數(shù)據(jù)類型都是字符串,完全適用于Map中的一切操作。
特有方法setProperty(String key,String value)等同于就是Map中的put,getProperty(String value)傳遞鍵,返回對(duì)應(yīng)的值,等同于Map中的get方法, 以后可以用于配置文件。
demo
package map;
/*
* Properties類的練習(xí)
*/
import java.util.*;
public class PropertiesDemo {
public static void main(String[] args) {
method1();
Properties pro = System.getProperties();
System.out.println(pro);
System.out.println(pro.get("os.name"));
}
/*
* 使用Properties自己的方法操作
*/
public static void method1(){
Properties pro = new Properties();
pro.setProperty("a", "1");
pro.setProperty("b", "2");
pro.setProperty("c", "3");
pro.setProperty("d", "4");
pro.setProperty("e", "5");
System.out.println(pro.getProperty("a"));
System.out.println(pro.getProperty("bb"));
}
/*
* 像map一樣操作Properties
*/
public static void method(){
Properties pro = new Properties();
pro.put(1, 321);
pro.put(2, 345321);
pro.put(3, 321);
pro.put(5, 321);
pro.put(4, 324534531);
System.out.println(pro.get(3));
Set set = pro.keySet();
Iterator it = set.iterator();
while(it.hasNext()){
Object key = it.next();
Object value = pro.get(key);
System.out.println(key + "..." + value);
}
}
}
十、Collections工具類
Collections中的方法都是靜態(tài)的,用于操作集合類的。
- static void sort(List list)根據(jù)存儲(chǔ)在集合的對(duì)象的自然順序,對(duì)List集合排序
- static void sort(List list,Comparator com)根據(jù)比較器對(duì)List集合排序
- static Comparator reverseOrder()返回比較器,逆轉(zhuǎn)對(duì)象的自然順序
- static Comparator reverseOrder(Comparatorc)傳遞比較器,返回比較器,返回的比較器逆轉(zhuǎn)了傳遞的比較器
- static int binarySearch(List list, Objectkey)集合的折半查找,傳遞List集合必須有序,找到返回下標(biāo),找不到返回,返回插入點(diǎn)-1
- static void fill(List list,Object obj)填充集合
- static void swap(List list ,int x,int y)指定下標(biāo),對(duì)List集合中的對(duì)象換位置
- static void shuffle(List list)對(duì)List集合中的對(duì)象,隨機(jī)換位置
- 將線程不安全集合變成線程安全集合 synchronized開(kāi)頭的方法
package Collections;
/*
* 集合工具類的操作方法
*/
import java.util.*;
import SetDemo.Person;
import SetDemo.PersonComparator;
public class CollectionsDemo {
public static void main(String[] args) {
method_4();
}
/*
* static void swap(List list ,int x,int y)指定下標(biāo),對(duì)List集合中的對(duì)象換位置 static void
* shuffle(List list)對(duì)List集合中的對(duì)象,隨機(jī)換位置
*/
public static void method_4() {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
list.add(8);
System.out.println(list);
Collections.swap(list, 0, 1);
System.out.println(list);
Collections.shuffle(list);
System.out.println(list);
}
/*
* static void fill(List list,Object obj)填充集合
*/
public static void method_3() {
List<Integer> list = new ArrayList<>();
list.add(12);
list.add(13);
list.add(15);
list.add(78);
list.add(54);
Collections.fill(list, 2);
System.out.println(list);
}
/*
* static int binarySort(List list)集合的折半查找折半查找,
*/
public static void method_2() {
List<Integer> list = new ArrayList<>();
list.add(12);
list.add(13);
list.add(15);
list.add(78);
list.add(54);
list.add(84);
list.add(25);
list.add(65);
int i = Collections.binarySearch(list, 84);
System.out.println(i);
}
/*
* static Comparator reverseOrder()返回比較器,逆轉(zhuǎn)對(duì)象的自然順序 static Comparator
* reverseOrder(Comparatorc)傳遞比較器,返回比較器,返回的比較器逆轉(zhuǎn)了傳遞的比較器
*
*/
public static void method_1() {
List<Person> list = new ArrayList<>();
list.add(new Person("liu", 23));
list.add(new Person("niu", 21));
list.add(new Person("fei", 222));
list.add(new Person("zi", 2323));
Collections.sort(list, Collections.reverseOrder(new PersonComparator()));
System.out.println(list);
}
/*
* static void sort(List list) 可以給集合排序 static void sort(List list,Comparator
* com)根據(jù)比較器對(duì)List集合排序
*/
public static void method() {
List<String> list = new ArrayList<>();
list.add("slkeghk");
list.add("aftytyj");
list.add("bturehk");
list.add("crgresr");
System.out.println(list);
Collections.sort(list);
System.out.println(list);
List<Person> l = new ArrayList<>();
l.add(new Person("liu", 23));
l.add(new Person("niu", 21));
l.add(new Person("fei", 222));
l.add(new Person("zi", 2323));
System.out.println(l);
Collections.sort(l);
System.out.println(l);
}
}
十一、集合數(shù)組的互轉(zhuǎn)
數(shù)組轉(zhuǎn)成集合 Arrays類方法isList , 傳遞數(shù)組,返回集合。
數(shù)組轉(zhuǎn)成集合后,改變集合長(zhǎng)度的操作不能做,如果做了,出現(xiàn)不支持的操作異常
集合轉(zhuǎn)數(shù)組Collection中方法toArray() 將集合轉(zhuǎn)成數(shù)組,傳遞數(shù)組,返回?cái)?shù)組
寫法:list.toArray(new Object[list.size()]);