首页 > 其他 > 详细

2020.02.27 集合练习题

时间:2020-02-27 22:09:26      阅读:86      评论:0      收藏:0      [点我收藏+]
package com.guoyun.view;

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

/**
* ClassName:
* Function: ADD FUNCTION
* Reason: ADD REASON
*
* @author
* @Date
* @since Ver 1.1
*/
public class Test10 {
public static void main(String[] args) {
Map<Car2,Integer> map=new HashMap<>();
Car2 c1=new Car2("奥拓",100);
Car2 c2 = new Car2("宝马", 200);
Car2 c3 = new Car2("奔驰", 300);
map.put(c1,10000);
map.put(c2,500000);
map.put(c3,2000000);
Set<Car2> car2s = map.keySet();
for (Iterator<Car2> iterator = car2s.iterator(); iterator.hasNext(); ) {
Car2 next = iterator.next();
System.out.println(next.getName());
if(next.getName().equals("宝马")){
System.out.println(map.get(next));
}
}
map.replace(c1,(int)(map.get(c1)*0.8));
map.replace(c2,(int)(map.get(c2)*0.8));
map.replace(c3,(int)(map.get(c3)*0.8));
for (Iterator<Car2> iterator = car2s.iterator(); iterator.hasNext(); ) {
Car2 next = iterator.next();
if (next.getName().equals("宝马")) {
System.out.println(map.get(next));
}
}
}
}
class Car2{
private String name;
private int speed;

public String getName() {
return name;
}

public int getSpeed() {
return speed;
}

@Override
public int hashCode() {
return name.hashCode();
}

@Override
public boolean equals(Object obj) {
if(this==obj){
return true;
}else if(obj instanceof Car2){
Car2 car2=(Car2) obj;
if(name.equals(car2.name)){
return true;
}
}
return false;
}

public Car2(String name, int speed) {
this.name = name;
this.speed = speed;
}
}

2020.02.27 集合练习题

原文:https://www.cnblogs.com/aojie/p/12374684.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!