(Java)集合框架(三)Map集合

【Map接口】

(1)Collection中的集合是单列集合,Map中的集合是双列集合

(2)Map集合中,元素是成对出现的。每个元素由键--值两部分组成,通过键可以找到所对应的值。

(3)Map集合中不能包含重复的键,值可以重复。

(4)Map中常用的集合为HashMap、LinkedHashMap。

 

【常用集合】

(1)HashMap<K, V> :存储数据采用-哈希表结构,元素的存取顺序不能保证一致。由于要保证键的唯一,需要重写键的hashCode()方法,equals()方法。线程不安全集合,运行速度快。允许存储null键null值。

(1.1)Hashtable<K, V>:存储数据采用-哈希表结构。线程安全集合,运行速度慢。不允许存储null键null值

(2)LinkedHashMap<K, V>:HashMap的子类,存储数据采用-哈希表结构+链表结构。通过链表结构可以保证存取顺序的一致。由于要保证键的唯一,需要重写键的hashCode()方法,equals()方法。

(3)注意:Map中的集合都有两个泛型<K, V>,在使用时要为两个泛型变量赋予数据类型。

【Map接口中常用方法】

import java.util.HashMap;
import java.util.Map;

/*
 * Map接口中常用的方法
 * 1.V  put(K,V)   将键值对存储在结合中     
 * 2.V  get(K)     通过键获取值
 * 3.V  remove(K)  删除集合中的键值对,并返回对应的值
 */
public class MapDemo1 {
	public static void main(String[] args) {
		function_3();
	}
	
	//1.将键值对存储在结合中     V  put(K,V)  K作为键对象    V作为值的对象
	//      返回值一般情况下是null,  当存储重复键时,返回被覆盖之前的值
	public static void function_1(){
		// 创建集合对象,HashMap    储存对象 键是字符串    值是整数
		Map<String,Integer> map = new HashMap<String, Integer>();
		map.put("a", 18);
		Integer i = map.put("a",12);
		System.out.println(i);                      // 18
		
		map.put("b", 19);
		map.put("c", 20);
		System.out.println(map);                   // {b=19, c=20, a=18}
		
	}
	
	//2.V get(K)  通过键获取值
	//    如果中没有这个键,就返回null
	public static void function_2(){
		Map<Integer,String> map = new HashMap<Integer,String>();
		map.put(1, "a");
		map.put(2, "b");
		map.put(3, "c");
		map.put(4, "d");
		
		String value = map.get(1);
		System.out.println(value);                 // a
		
	}
	
	//3.V remove(K)  删除集合中的键值对,并返回对应的值
	public static void function_3(){
		Map<Integer,String> map = new HashMap<Integer,String>();
		map.put(1, "a");
		map.put(2, "b");
		map.put(3, "c");
		map.put(4, "d");
		System.out.println(map);                 // {1=a, 2=b, 3=c, 4=d}
		
		String value = map.remove(1);
		System.out.println(value);              // a
		System.out.println(map);                // {2=b, 3=c, 4=d}
	}

}

【Map集合遍历方式】

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/* 
 * Map集合遍历方式:
 * 1. 调用Map集合中的keySet()方法,所有的键存储到Set集合中
 * 2. 遍历集合Set(A.迭代器;B.增强for),获取Set中的所有元素(Map中的键)
 * 3. 调用Map中的get()方法,通过键获得值
 */
public class MapDemo2 {
	public static void main(String[] args) {
		function_1();
		System.out.println("=============================");
		function_2();
	}
	
	// A.Map集合遍历----迭代器方法
	public static void function_1(){
		Map<String,Integer> map = new HashMap<String, Integer>();
		map.put("a", 1);
		map.put("b", 2);
		map.put("c", 3);
		map.put("d", 4);
		// 1. 调用Map集合中的keySet()方法,所有的键存储到Set集合中
		Set<String> set = map.keySet();		
		// 2. 创建迭代器
		Iterator<String> it = set.iterator();
		// 3. 迭代器实现
		while(it.hasNext()){
			String k = it.next();
			Integer v = map.get(k);
			System.out.println(k+"="+v);  // d=4  b=2  c=3 a=1
		}		
		
	}
	// B.Map集合遍历----增强for方法
	public static void function_2(){
		Map<String,Integer> map = new HashMap<String, Integer>();
		map.put("a", 1);
		map.put("b", 2);
		map.put("c", 3);
		map.put("d", 4);
		// 增强for实现
		for(String s:map.keySet()){
			Integer i = map.get(s);
			System.out.println(s+"="+i);  // d=4  b=2  c=3 a=1
		}
		
	}

}

【Entry键值对对象,遍历方式】

在Map设计类时,提供了一个嵌套接口:Entry(内部接口)。Entry将键值对的对应关系封装成了对象

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/*
 * Map集合获取方式    entrySet()方法 ,键值对映射关系获取
 *   实现步骤:
 *        1. 调用集合Map方法entrySet()方法,将集合中的映射关系对象,存储到Set集合中
 *                   Set< Entry<K, V> >
 *        2. 迭代Set集合
 *        3. 获得Set集合中的元素是映射关系对象
 *        4. 通过对象方法getKey(),getValue()获取键值对
 */
public class MapDemo3 {
	public static void main(String[] args) {
		function_1();
		System.out.println("=============================");
		function_2();
	}
	// A.Map集合遍历----迭代器方法
	public static void function_1(){
		Map<Integer,String> map = new HashMap<Integer,String>();
		map.put(1, "a");
		map.put(2, "b");
		map.put(3, "c");
	    // 1. 调用集合Map方法entrySet()方法,将集合中的映射关系对象,存储到Set集合中
		Set<Map.Entry<Integer,String>> set = map.entrySet();
		// 2. 迭代Set集合
		Iterator<Entry<Integer, String>> it = set.iterator();
		
		while(it.hasNext()){
			// 3. 获得Set集合中的元素是映射关系对象
			//    it.next()获取的是Map.Entry对象
			Map.Entry<Integer, String> entry = it.next();
			// 4. 通过对象方法getKey(),getValue()获取键值对
			Integer key = entry.getKey();
			String value = entry.getValue();
			System.out.println(key+"="+value);     // 1=a  2=b   3=c
		}
	}
	
	// B.Map集合遍历----增强for方法
	public static void function_2(){
		Map<Integer,String> map = new HashMap<Integer,String>();
		map.put(1, "a");
		map.put(2, "b");
		map.put(3, "c");
		//  注意:增强for通过Set间接遍历Map
		for(Map.Entry<Integer, String> e:map.entrySet()){
			System.out.println(e.getKey()+"="+e.getValue());     // 1=a  2=b   3=c
		}
				
	}

}

【练习】

import java.util.HashMap;
import java.util.Map;

/*
 * 使用HashMap集合,存储自定义Person对象
 * 
 */
public class MapDemo4 {
	public static void main(String[] args) {
		function_2();
	}
	
	// A.使用自定义Person  作值
	public static void function_1(){
		HashMap<String,Person> map = new HashMap<String,Person>();
		map.put("北京", new Person("zhang", 22));
		map.put("上海", new Person("li", 27));
		map.put("郑州", new Person("wei", 24));
		
		// 1.使用Map方法keySet(),将所有键存储到Set中,再利用增强for遍历
		for(String key :map.keySet()){
			Person value = map.get(key);
			System.out.println(key+"  "+value);          // 上海name=li  age=27   北京name=zhang  age=22  郑州name=wei  age=24
		}
		
	    // 2.使用Map的内部嵌套Entry接口,将键值对封装成对象存储在Set中,再使用getKay()方法获取键,getValue()方法获取值	
		for(Map.Entry<String,Person> entry:map.entrySet()){
			String key = entry.getKey();
			Person value = entry.getValue();
			System.out.println(key+"  "+value);
		}
	}
	
	// B.使用自定义Person  作键
	//   注意:为了保证键的唯一性需要重写Person中的   hashCode()  与equals()方法
	public static void function_2(){
		HashMap<Person,String> map = new HashMap<Person,String>();
		map.put(new Person("zhang", 22), "巴厘岛");
		map.put(new Person("zhang", 22), "巴厘岛");
		map.put(new Person("shang", 22), "希腊");
		map.put(new Person("huang", 22), "巴塞罗那");
		//System.out.println(map);
		
		// 1.增强for方法遍历
		for(Person key:map.keySet()){
			String value = map.get(key);
			System.out.println(key+" "+value);  //  name=huang  age=22 巴塞罗那......
		}
		// 2.内部接口Entry方法遍历
		
	}

}

【方法的可变参数】

/*
 * 方法的可变参数
 *    使用前提:方法参数数据类型确定,参数的个数任意个
 *    语法:       数据类型...变量名
 *    
 *    注意事项:
 *          1.一个方法中可变参数只能有一个
 *          2.可变参数,必须写在参数列表的最后一位
 */
public class VarargumentDemo1 {
	public static void main(String[] args) {
		int sum = getSum(1,2,3);
		System.out.println(sum);
	}
	
	// 求和方法   其中a为一个数组
	public static int getSum(int...a){
		int sum = 0;
		for(int i:a){
			sum += i;
		}
		return sum;
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值