Map集合的简单介绍

Map集合和Collection集合的区别:
     Collection:是一个单列集合,只能存储一种引用类型
	最具体的子实现类:
				Set ------>HashSet 和TreeSet的add方法都和HashMap,TreeMap的put有关系	

      Map集合:是一个双列集合,能够存储两种引用类型(键和值:是一种键值对存在),理解为"夫妻对"
		HashMap<K,V>,TreeMap<K,V>

Map集合

添加功能:
V put(K key, V value) :给Map集合添加键值对

  •  删除:
     	V remove(Object key):删除指定的键,返回对应的值
    
  •  判断功能
     boolean containsKey(Object key):是否包含指定的键
     boolean containsValue(Object value):是否包含指定的值
     boolean isEmpty():判断是否为空,如果为空,返回true
    
  •  获取功能:
     int size():获取Map集合的键值对的个数(元素)  
    
  •   常用的遍历:keySet()
      V get(Object key):通过键获取值
      Set<K> keySet():获取Map集合中所有的键的集合 
      Set<Map.Entry<K,V>> entrySet():获取所有的键值对对象
      			Map.Entry<K,V>:接口:代表键值对对象
      			K getKey():获取键
      			V getValue():获取值
    

HashMap

public class Student {
	private String name;
	private int age;
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", 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;
	}
	
}
import java.util.HashMap;
import java.util.Set;

public class HashMapDemo {
	
	public static void main(String[] args) {
		
		//创建HashMap集合对象
		HashMap<String, Student> hm = new HashMap<String,Student>() ;
		
		//添加元素
		Student s1 = new Student("刘诗诗", 25) ;
		Student s2 = new Student("杨幂", 24) ;
		Student s3 = new Student("佟丽娅", 23) ;
		Student s4 = new Student("唐嫣", 22) ;hm.put("001", s1) ;
		hm.put("002", s2) ;
		hm.put("003", s3) ;
		hm.put("004", s4) ;
		
		//遍历
		Set<String> set = hm.keySet() ;
		for(String key:set) {
			Student value = hm.get(key) ;
			System.out.println(key+"="+value.getName()+"---"+value.getAge());
		}
	}
}

TreeMap

选择排序器

public class Student {
	private String name;
	private int age;
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(String name, int age) {
		super();
		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;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	
}
import java.util.Comparator;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

public class TreeMapDemo {
	public static void main(String[] args) {
		
		TreeMap<Student,String> tm = new TreeMap<Student, String>(new Comparator<Student>(){

			@Override
			public int compare(Student s1, Student s2) {
				int num = s1.getAge()-s2.getAge();
				int num2 = (num == 0)?s1.getName().compareTo(s2.getName()):num;
				return num2;
			}
			
		});
		Student s1 = new Student("liushishi",21);
		Student s2 = new Student("wuqilong",21);
		Student s3 = new Student("tangyan",23);
		Student s4 = new Student("tongliya",22);
		Student s5 = new Student("gaoyuanyuan",24);
		Student s6 = new Student("liushishi",23);
		tm.put(s1, "001");
		tm.put(s2, "002");
		tm.put(s3, "003");
		tm.put(s4, "004");
		tm.put(s5, "005");
		tm.put(s6, "006");
		//遍历
		Set<Student> set = tm.keySet() ;
		for(Student key :set) {
			//通过键获取值
			String value = tm.get(key) ;
			System.out.println(key.getName()+"---"+key.getAge()+"---"
					+value);
		}
		
	}
}

自然排序

public class Student implements Comparable<Student> { //自然排序的接口
	
	private String name ;
	private int age ;
	public Student() {
		super();
	}
	
	public Student(String name, int age) {
		super();
		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;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}

	//按照学生的年龄从大到小排序
	@Override
	public int compareTo(Student s) {
		
		int num = s.age - this.age ;
		
		//年龄一样,比较姓名内容是否相同
		int num2 = (num==0)? (this.name.compareTo(s.name)) :num ; 
		
		return num2;
	}
}
import java.util.Comparator;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapDemo {
	
	public static void main(String[] args) {
		//创建TreeMap集合对象
		TreeMap<Student, String> tm = new TreeMap<Student,String>() ;
		
		//创建学生对象
		Student s1 = new Student("liushishi",21);
		Student s2 = new Student("wuqilong",21);
		Student s3 = new Student("tangyan",23);
		Student s4 = new Student("tongliya",22);
		Student s5 = new Student("gaoyuanyuan",24);
		Student s6 = new Student("liushishi",23);
		
		//添加元素
		tm.put(s1,"001") ;
		tm.put(s2,"002") ;
		tm.put(s3,"003") ;
		tm.put(s4,"004") ;
		tm.put(s5,"005") ;
		tm.put(s6,"005") ;
		
		//获取所有的键
		Set<Student> set = tm.keySet() ; 
		for(Student key :set) {
			//通过键获取值
			
			
			String value = tm.get(key) ;
			System.out.println(key.getName()+"---"+key.getAge()+"---"
					+value);
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值