Java的HashMap和ArrayList的综合应用(一对一、多)

实现hashmap的一对一和一对多

1、wife类

package com.hpu.homework;

public class Wife {
	private String wifeName;
	private int wifeAge;
	private double wifeSpend;
	public String getWifeName() {
		return wifeName;
	}
	public int getWifeAge() {
		return wifeAge;
	}
	public void setWifeAge(int wifeAge) {
		this.wifeAge = wifeAge;
	}
	public double getWifeSpend() {
		return wifeSpend;
	}
	public void setWifeSpend(double wifeSpend) {
		this.wifeSpend = wifeSpend;
	}
	public Wife(){
		super();
	}
	public Wife(String wifeName, int wifeAge, double wifeSpend) {
		// TODO Auto-generated constructor stub
		super();
		this.wifeName = wifeName;
		this.wifeAge = wifeAge;
		this.wifeSpend = wifeSpend;
	}
	public void costMoney(){
		System.out.println("双十一快到了,"+wifeName+"买了一只iphonex花了"+wifeSpend+"($ _ $)");
	}
}

2、husband类

package com.hpu.homework;

public class Husband {
	private String husbandName;
	private int husbandAge;
	private double husbandSalary;
	public String getHusbandName() {
		return husbandName;
	}
	public void setHusbandName(String husbandName) {
		this.husbandName = husbandName;
	}
	public double getHusbandSalary() {
		return husbandSalary;
	}
	public void setHusbandSalary(double husbandSalary) {
		this.husbandSalary = husbandSalary;
	}
	public Husband(String husbandName,int husbandAge,double husbandSalary){
		super();
		this.husbandName=husbandName;
		this.husbandAge=husbandAge;
		this.husbandSalary=husbandSalary;
	}
	public Husband(){
		super();
	}
	public void makeMoney(){
		System.out.println(husbandName+"今天上了一天班,赚了"+husbandSalary+"($ _ $)");
	}
}

3、一对一

package com.hpu.homework;

import java.util.HashMap;
import java.util.Scanner;

/**
 * 一对一
 * @author Administrator
 *
 */
public class TestHusbandWife {
	public static void main(String[] args) {
		HashMap map=new HashMap<>();
		//默认添加三对夫妻
		map.put(new Husband("张爱岗", 35, 8000.5), new Wife("小红", 32, 4000.5));
		map.put(new Husband("李诚实", 23, 4000), new Wife("小兰", 22, 1000.5));
		map.put(new Husband("王建党", 50, 15000.56), new Wife("小花", 35,10000.5));
		System.out.println("谁谁谁起来了,搬砖了");
		Scanner input = new Scanner(System.in);
		//获取丈夫姓名
		String inputHusbandName=input.next();
		//遍历map
		for(Husband h:map.keySet()){
			if(h.getHusbandName().equals(inputHusbandName)){
				h.makeMoney();//赚钱啊
				Wife w=map.get(h);//通过丈夫找到妻子
				w.costMoney();
			}
		}
	}
}

4、一对多

package com.hpu.homework;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

import org.omg.Messaging.SyncScopeHelper;

/*
 * 一对多
 */
public class TestHusbandWife2 {
	public static void main(String[] args) {
		HashMap> map=new HashMap<>();
		ArrayList list1=new ArrayList<>();
		list1.add(new Wife("小红",23,1234));
		list1.add(new Wife("小蓝",22,2430));
		list1.add(new Wife("小枚",25,1000));
		map.put(new Husband("王哈哈",34,1000000), list1);
	    //用户输入地主姓名
		Scanner input=new Scanner(System.in);
		System.out.println("请输入姓名");
		String diName=input.next();
		for(Husband h:map.keySet()){
			if(h.getHusbandName().equals(diName)){
				//根据姓名找出对应妻子
				ArrayList wifeList=map.get(h);
				h.makeMoney();
				//对妻子集合进行遍历
				for(Wife w:wifeList){
					w.costMoney();
				}
			}
		}
	}
}

 

你可能感兴趣的:(Java)