Map集合

Map是一种键-值(Key-value)集合,Map集合中的每一个元素都包含一个键对象和一个值对象。其中,键对象不允许重复,并且值对象还可以是Map类型的,就像数组中的元素还可以是数组一样。

Map主要有两个实现类:HashMap类和TreeMap类。其中,HashMap类按哈希算法来存取键对象,而TreeMap类可以对键对象进行排序。

1.Map.Entry

    Map中包括了一个内部类Entry,该类封装了一个key-value记录

2.常用方法

3.HashTable和HashMap的使用方法基本一致

4.HashMap和HashTable的区别

(1)Hashtable不允许使用null作为key和value,如果试图把nul放进HashTable中,将会引发NullPointerException异常;但HashMap可以使用null作为key或value
(2)Hashtable是一个线程安全的Map实现类,但HashMap是线程不安全的实现类,所以HashMap要比Hashtable的性能高一点;但是如果有多个线程访问同一个Map对象时,使用Hashtable实现类会更好

小练习1

输出对应员工的工资

运行结果

package Employee;

public class Employee {

	private String name;//员工姓名
	private double salary;//工资
	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Employee(String name, double salary) {
		super();
		this.name = name;
		this.salary = salary;
	}
	
	@Override
	public String toString() {
		return "Employee [name=" + name + ", salary=" + salary + "]";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	
}

package Employee;

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

public class test {

	public static void main(String[] args) {
		ArrayList<HashMap<String,Employee>> emp=new ArrayList<HashMap<String,Employee>>();
		HashMap<String ,Employee> n1=new HashMap<String ,Employee>();
		HashMap<String ,Employee> n2=new HashMap<String ,Employee>();
		HashMap<String ,Employee> n3=new HashMap<String ,Employee>();
		n1.put("张三", new Employee("张三",800));
		n2.put("李四", new Employee("李四",1500));
		n3.put("王五", new Employee("王五",3000));
		emp.add(n1);
		emp.add(n2);
		emp.add(n3);
		//遍历ArrayLsit
		for(HashMap<String,Employee> e:emp) {
			//遍历Map
			Set<Entry<String, Employee>> ss = e.entrySet();
			for(Entry<String,Employee> s:ss) {
				String k=s.getKey();
				Employee v=s.getValue();
				System.out.println(k+v);
			}
		}
	}

}

练习2

需求:
使用HashMap存储学生信息。
1、定义一个学生类Student,包括学生姓名,年龄,性别,成绩基本信息。
2、创建两个学生对象。
3、保存到HashMap集合中,键为学生编号,值为学生对象。
4、两种方式遍历输出集合中的元素信息。

运行结果为

package Student;

public class Student {

	private String name;//姓名
	private int age;//年龄
	private String sex;//性别
	private double score;//成绩
	public Student() {
		
	}
	public Student(String name, int age, String sex, double score) {
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.score = score;
	}
	
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", sex=" + sex + ", score=" + score + "]";
	}
	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;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	
}

package Student;

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

public class test {

	public static void main(String[] args) {
		Student s1=new Student("张三",18,"男",88);
		Student s2=new Student("李四",18,"男",88);
		Student s3=new Student("王五",18,"男",88);
		Student s4=new Student("李丽",20,"女",90);
		//HashMap保存的元素是唯一且无顺序的
		//linkedHashMap保存的元素是唯一的,有序的
		//HashMap在多线程开发环境中不安全
		//HashTable多线程安全的(原因是具备线程同步锁)
		Map<String ,Student> stu=new HashMap<String ,Student>();
		stu.put("张三", s1);
		stu.put("李四", s2);
		stu.put("王五", s3);
		stu.put("李丽", s4);
		
		Set<Entry<String, Student>> ens = stu.entrySet();
		//增强型for循环遍历
		for(Entry<String ,Student> en:ens) {
			String k=en.getKey();
			Student v=en.getValue();
			System.out.println("学生姓名为:"+k+",学生信息为:"+v);
		}
		//迭代器遍历
		Iterator<Entry<String ,Student>> enst=ens.iterator();
		while(enst.hasNext()) {
			System.out.println(enst.next());
		}
	}

}

练习3

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值