学习Java第二十一天

Java集合(下)

Map

  • Map中的数据是以键值对 (key-value) 的形式存储的
  • key-value 以 Entry 类型的对象实例在
  • 可以通过 key 值快速地查 value
  • 一个映射不能包含重复的键
  • 每个健最多只能映射到一个值

HashMap

  • 基于哈希表的 Map 接口的实现
  • 允许使用 null 值和null 键
  • key 值不允许重复
  • HashMap 中的 Entry 对象是无序排列的

Map
在这里插入图片描述
HashMap
在这里插入图片描述

案例一

  • 完成一个类似字典的功能
    – 将单词以及单词的注释存到HashMap中
    – 显示 HashMap 中的内容
    – 查找某个单词的注释并显示
  • 具体使用方法 put()、get()、KeySet()、entrySet()、equals()、迭代器
package com.sh.set;

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

public class DictionaryDemo {

	public static void main(String[] args) {
		Map<String,String> animal=new HashMap<String,String>();
		System.out.println("请输入三组单词对应的注释,并存放到 HashMap 中");
		Scanner console=new Scanner(System.in);
		//添加数据
		int i=0;
		while(i<3){
			System.out.println("请输入key值(单词):");
			String key=console.next();
			System.out.println("请输入value值(注释):");
			String value=console.next();
			animal.put(key, value);
			i++;
		}
		//打印输出value的值(直接使用迭代器)
		System.out.println("*****************************************");
		System.out.println("使用迭代器输出所有的value:");
		Iterator<String> it=animal.values().iterator();
		while(it.hasNext()){
			System.out.print(it.next()+"    ");
		}
		System.out.println();
		System.out.println("*****************************************");
		System.out.println("使用迭代器输出所有的key-value");
		Set <String> keyset =animal.keySet();
		Iterator<String> itor=keyset.iterator();
		while(itor.hasNext()) {
			String Key=itor.next();
			
			String value=animal.get(Key);
			System.out.println("Key:"+Key+"\t\tvalue:"+value);
		}
		System.out.println();
		System.out.println("*****************************************");
		//用get方法获取每个键对应的值
		//打印输出key和value的值
		//通过entrySet方法
		System.out.println("通过entrySet方法得到key-value:");
		Set<Entry<String, String>> entrySet=animal.entrySet();
		for(Entry<String, String> entry:entrySet){
			System.out.print(entry.getKey()+"-");;
			System.out.println(entry.getValue());
		}
		System.out.println();
		System.out.println("*****************************************");
		
		//通过单词找到注释并输出
		//使用keySet方法
		System.out.println("请输入要查找的单词:");
		String strSearch=console.next();
		//1.取得keySet
		Set<String> keySet=animal.keySet();
		//2.遍历keySet
		for(String key:keySet){
			if(strSearch.equals(key)){
				System.out.println("找到了!"+"键值对为:"+key+"-"+animal.get(key));
				break;
			}
		}
	}

}

  • 运行结果
    在这里插入图片描述

案例二(商品信息管理)

  • 使用 HashMap 对商品信息进行管理
    – 其中 key 为商品编号,value 为商品对象

  • 对 HashMap 中的商品信息进行增、删、改、查操作

  • 属性
    – 商品编号:id
    – 商品名称:name
    – 商品价格:price

  • 方法
    – 构造方法
    – 获取和设置属性值的方法
    – 其他方法

  • 因为数值输入的问题,对程序进行了优化,详情见代码

//Goods类
package com.sh.set;

public class Goods {
	private String id;//商品编号
	private String name;//商品名称
	private double price;//商品价格
	//构造方法
	public Goods(String id,String name,double price){
		this.id=id;
		this.name=name;
		this.price=price;
	}

	//getter和setter方法
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}
	public String toString(){
		return "商品编号:"+id+",商品名称:"+name+",商品价格:"+price;
	}
}
//GoodsTest类
package com.sh.set;

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

public class GoodsTest {

	public static void main(String[] args) {

		Scanner console = new Scanner(System.in);
		// 定义HashMap对象
		Map<String, Goods> goodsMap = new HashMap<String, Goods>();
		System.out.println("请输入三条商品信息:");
		int i = 0;
		while (i < 3) {
			System.out.println("请输入第" + (i + 1) + "条商品信息:");
			System.out.println("请输入商品编号:");
			String goodsId = console.next();
			// 判断商品编号id是否存在
			if (goodsMap.containsKey(goodsId)) {
				System.out.println("该商品编号已经存在!请重新输入!");
				continue;
			}
			System.out.println("请输入商品名称:");
			String goodsName = console.next();
			System.out.println("请输入商品价格:");
			double goodsPrice = 0;
			try {
				goodsPrice = console.nextDouble();
			} catch (java.util.InputMismatchException e) {
				System.out.println("商品价格的格式不正确,请输入数值型数据!");
				console.next();
				continue;
			}
			Goods goods = new Goods(goodsId, goodsName, goodsPrice);
			// 将商品信息添加到HashMap中
			goodsMap.put(goodsId, goods);
			i++;
		}
		// 遍历Map,输出商品信息
		System.out.println("商品的全部信息为:");
		Iterator<Goods> itGoods = goodsMap.values().iterator();
		while (itGoods.hasNext()) {
			System.out.println(itGoods.next());
		}
				
	}

}

  • 运行截图
    在这里插入图片描述

总结

  • List、Set、Map
  • ArrayList
  • LinkedList
  • HashSet
    – hashCode() 和 equals()
  • TreeSet
  • Iterator(迭代器)
  • HashMap
  • 44
    点赞
  • 113
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值