这是我从网上看到的题目,自己拿来写了写,代码都是自己打的,只注重了功能,格式或者是代码运行效率并没有重点考虑,如果觉得不太好的,请见谅。
3.请使用Map集合的方法完成添加元素,根据键删除,以及根据键获取值操作。
代码如下:
public class Test03 { public static void main(String[] args) { HashMap<String,String> hashMap = new HashMap<>(); hashMap.put("1","我们"); hashMap.put("2","他们"); hashMap.put("3","你们"); // 删除 hashMap.remove("2"); // 获取 System.out.println(hashMap.get("3")); } }
代码如下:
public class Test04 { public static void main(String[] args) { HashMap<String,String> hashMap = new HashMap<>(); hashMap.put("1","我们"); hashMap.put("2","他们"); hashMap.put("3","你们1"); hashMap.put("4","你们2"); hashMap.put("5","你们3"); hashMap.put("6","你们4"); Collection<String> values = hashMap.values(); for (String s:values ) { System.out.println(s); } System.out.println("增强for"); Iterator<String> iterator = values.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } } }
代码如下:
public class Car { private String name; private double price; public Car() { } public Car(String name, double price) { this.name = name; this.price = price; } 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; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Car car = (Car) o; return Double.compare(car.price, price) == 0 && Objects.equals(name, car.name); } @Override public int hashCode() { return Objects.hash(name, price); } } public class Test05 { public static void main(String[] args) { HashMap<Car, Integer> hashMap = new HashMap<Car, Integer>(); hashMap.put(new Car("宝马",123.0),2); hashMap.put(new Car("奔驰",222.0),3); hashMap.put(new Car("红旗",333.0),4); Set<Car> cars = hashMap.keySet(); for (Car s:cars ) { System.out.println(s.getName()+" "+s.getPrice()); } System.out.println("---------"); Set<Map.Entry<Car, Integer>> entries = hashMap.entrySet(); for (Map.Entry<Car,Integer> entry:entries ) { Car car = entry.getKey(); Integer i = entry.getValue(); System.out.println(car.getName()+" "+car.getPrice()+" "+i); } } }
代码如下:
public class Test06 { public static void main(String[] args) { Map<Integer,String> map = new HashMap<Integer, String>(); map.put(1, "张三丰"); map.put(2, "周芷若"); map.put(3, "汪峰"); map.put(4, "灭绝师太"); Set<Integer> integers = map.keySet(); for (Integer i:integers ) { System.out.println(i+" "+ map.get(i)); } map.put(5,"李晓红"); String remove = map.remove(1); System.out.println("已经移除 " + remove); boolean flag = map.replace(2, map.get(2), "周林"); System.out.println("将map集合中编号为2的姓名信息修改为周林,是否完成:"+flag); System.out.println(map); } }
7.有2个数组,第一个数组内容为:[黑龙江省,浙江省,江西省,广东省,福建省],第二个数组为:[哈尔滨,杭州,南昌,广州,福州],将第一个数组元素作为key,第二个数组元素作为value存储到Map集合中。如{黑龙江省=哈尔滨, 浙江省=杭州, …}。
代码如下;
public class Test07 { public static void main(String[] args) { String[] onearray = new String[]{"黑龙江省","浙江省","江西省","广东省","福建省"}; String[] twoarray = new String[]{"哈尔滨","杭州","南昌","广州","福州"}; HashMap<String,String> hashMap = new HashMap<>(); for (int i = 0;i < onearray.length;i++){ hashMap.put(onearray[i],twoarray[i]); } System.out.println(hashMap); } }