1、请使用Map集合存储自定义数据类型Car做键,对应的价格做值。并使用keySet和entrySet两种方式遍历Map集合。
package com.xxx;
public class Car {
private String name;
private int price;
public Car() {
}
public Car(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
package com.xxx;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Homework1 {
public static void main(String[] args) {
Car car = new Car("车", 100);
Car car1 = new Car("车1", 200);
Car car2 = new Car("车2", 300);
HashMap<Car, Integer> hashMap = new HashMap<>();
hashMap.put(car,car.getPrice());
hashMap.put(car1,car1.getPrice());
hashMap.put(car2,car2.getPrice());
// TreeSet<Car> keySet = new TreeSet<>();
// keySet.add(car);
// keySet.add(car1);
// keySet.add(car2);
//
// for (int i = 0; i < keySet.size(); i++) {
//
// }
//获取所有键
Set<Car> carSet = hashMap.keySet();
for (Car c : carSet) {
System.out.println(c + ":\t" + hashMap.get(c));
}
Iterator<Map.Entry<Car, Integer>> iterator = hashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Car, Integer> next = iterator.next();
System.out.println(next);
}
}
}
2、需求:
统计一个文件夹中每种文件的个数并打印
打印格式如下:
txt:3个
doc:4个
jpg:6个
package com.xxx;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Homework2 {
public static void main(String[] args) throws IOException {
//新建一些文件
new File("d:\\a\\b\\1.txt").createNewFile();
new File("d:\\a\\b\\2.doc").createNewFile();
new File("d:\\a\\b\\3.jpg").createNewFile();
new File("d:\\a\\b\\4.jpg").createNewFile();
new File("d:\\a\\b\\5.md").createNewFile();
//获取一个文件夹所有文件
String path = "d:\\a\\b";
File file = new File(path);
//给所有的后缀,文件类型设置一个hashMap
//String是文件后缀名,Integer是文件数量
HashMap<String, Integer> map = new HashMap<>();
//遍历文件路径名数组
//返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。
for (File listFile : file.listFiles()) {
String fileName = listFile.getName();
if (fileName.contains(".")) {//如果文件名称包含"."
//substring返回一个新的字符串,它是此字符串的一个子字符串。
//lastIndexOf返回指定子字符串在此字符串中最右边出现处的索引。
String fileSuffix = fileName.substring(fileName.lastIndexOf(".") + 1);
if (!map.containsKey(fileSuffix)) {//如果文件后缀名第一次出现,给它次数设为1
map.put(fileSuffix,1);
} else {
map.put(fileSuffix, map.get(fileSuffix) + 1);
}
}
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
}
3、斗地主
package com.xxx;
import java.util.*;
public class Pai {
/**
* 斗地主
* @param args
*/
public static void main(String[] args) {
//1.生成牌
//数组->表示花色
//数组->表示点数
String[] colors = {"♦","♣","♥","♠"};
String[] nums = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
//一个key对应一张牌,共54张
HashMap<Integer, String> hashMap = new HashMap<>();
//存放牌的索引的集合
List<Integer> list = new ArrayList<>();
//定义一个变量,记录牌的索引
int index = 0;
for (String num : nums) {
for (String color : colors) {
String pai = color + num;
hashMap.put(index, pai);
list.add(index);
index++;
// System.out.println(pai);//打印除大小王之外所有的牌
}
}
// System.out.println(list);
//大小王单独生成
hashMap.put(53, "小王");
list.add(53);
hashMap.put(54, "大王");
list.add(54);
// System.out.println(list);
//2.洗牌
//把索引的顺序打乱
Collections.shuffle(list);
// System.out.println(list);
//3.发牌
//给3个人和底牌,每人发17张,底牌3张
//set集合会排序,把牌整理好
TreeSet<Integer> dushen = new TreeSet<>();
TreeSet<Integer> duxia = new TreeSet<>();
TreeSet<Integer> duwang = new TreeSet<>();
TreeSet<Integer> dipai = new TreeSet<>();
//依次发牌,就是依次发索引
for (int i = 0; i < list.size(); i++) {//索引0~53,51,52,53发给底牌
//先判断是否是底牌,不然底牌发不到
if (i >= 51) {//最后三个索引发给底牌
dipai.add(list.get(i));
} else if (i % 3 == 0) {
dushen.add(list.get(i));
} else if (i % 3 == 1) {
duxia.add(list.get(i));
} else if (i % 3 == 2) {
duwang.add(list.get(i));
}
}
//打印3个人和底牌的牌的索引
// System.out.println(dushen);
// System.out.println(duxia);
// System.out.println(duwang);
// System.out.println(dipai);
//4.调用看牌的方法
lookPai("赌神", dushen, hashMap);
lookPai("赌侠", duxia, hashMap);
lookPai("赌王", duwang, hashMap);
lookPai("底牌", dipai, hashMap);
}
//4.看牌
//看name的牌,牌的索引,索引对应的牌的点数
public static void lookPai(String name, TreeSet<Integer> treeSet, Map<Integer, String> map) {
String s = name + ":";
//遍历set集合
for (Integer i : treeSet) {
s += map.get(i) + "\t";
}
System.out.println(s);
}
}
标签:练习题,Map,hashMap,list,name,07File,new,public,String
From: https://www.cnblogs.com/wyzel/p/16819842.html