- 使用LinkedHashSet存储以下元素: "王昭君","王昭君","西施","杨玉环","貂蝉"。使用迭代器和增强for循环遍历LinkedHashSet。
- 有2个数组,第一个数组内容为: [黑龙江省,浙江省,江西省,广东省,福建省],第二个数组为: [哈尔滨,杭州,南昌,广州,福州],将第一个数组元素作为key,第二个数组元素作为value存储到Map集合中。如{黑龙江省=哈尔滨,浙江省=杭州...}。
- 请使用Map集合存储自定义数据类型Car做键,对应的价格做值。并使用keySet和entrySet两种方式遍历Map集合。
- 向list集合添加姓名{张三,李四,王五,二丫,钱六,孙七},将二丫替换为王小丫。
- 定义人类,包含姓名和年龄属性。创建4个人存储到HashSet中,姓名和年龄相同的人看做同一人不存储。
| 1、 |
| package com.xxx; |
| |
| import java.util.Iterator; |
| import java.util.LinkedHashSet; |
| |
| public class Exe1 { |
| public static void main(String[] args) { |
| LinkedHashSet<String> strings = new LinkedHashSet<>(); |
| strings.add("王昭君"); |
| strings.add("西施"); |
| strings.add("杨玉环"); |
| strings.add("貂蝉"); |
| strings.add("王昭君"); |
| |
| Iterator<String> iterator = strings.iterator(); |
| while (iterator.hasNext()) { |
| String s = iterator.next(); |
| System.out.println(s); |
| } |
| |
| for (String s : strings) { |
| System.out.println(s); |
| } |
| } |
| } |
| |
| 2、 |
| package com.xxx; |
| |
| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.List; |
| |
| public class Exe2 { |
| public static void main(String[] args) { |
| String[] str1 = {"黑龙江", "浙江省", "江西省", "广东省", "福建省"}; |
| String[] str2 = {"哈尔滨", "杭州", "南昌", "广州", "福州"}; |
| HashMap<String, String> hashMap = new HashMap<>(); |
| |
| for (int i = 0; i < str1.length; i++) { |
| hashMap.put(str1[i], str2[i]); |
| } |
| System.out.println(hashMap); |
| } |
| } |
| |
| 3、 |
| package com.xxx; |
| |
| public class Car { |
| private String carName; |
| private int carPrice; |
| |
| public Car() { |
| } |
| |
| public Car(String carName, int carPrice) { |
| this.carName = carName; |
| this.carPrice = carPrice; |
| } |
| |
| public String getCarName() { |
| return carName; |
| } |
| |
| public void setCarName(String carName) { |
| this.carName = carName; |
| } |
| |
| public int getCarPrice() { |
| return carPrice; |
| } |
| |
| public void setCarPrice(int carPrice) { |
| this.carPrice = carPrice; |
| } |
| |
| @Override |
| public String toString() { |
| return "Car{" + |
| "carName='" + carName + '\'' + |
| ", carPrice=" + carPrice + |
| '}'; |
| } |
| } |
| package com.xxx; |
| |
| import java.util.HashMap; |
| import java.util.Map; |
| import java.util.Set; |
| |
| public class Exe3 { |
| public static void main(String[] args) { |
| Map<Car, Integer> map = new HashMap<>(); |
| |
| map.put(new Car("劳斯莱斯",500),500); |
| map.put(new Car("布加迪", 1200),1200); |
| |
| Set<Car> cars = map.keySet(); |
| for (Car c : cars) { |
| System.out.println(c.getCarName() + "-->" + c.getCarPrice()); |
| } |
| |
| Set<Map.Entry<Car, Integer>> entries = map.entrySet(); |
| for (Map.Entry<Car, Integer> entry : entries) { |
| Car key = entry.getKey(); |
| Integer value = entry.getValue(); |
| System.out.println(key.getCarName() + "-->" + value); |
| } |
| } |
| } |
| |
| 4、 |
| package com.xxx; |
| |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.List; |
| |
| public class Exe4 { |
| public static void main(String[] args) { |
| List<String> list = new ArrayList<>(); |
| |
| list.add("张三"); |
| list.add("李四"); |
| list.add("王五"); |
| list.add("二丫"); |
| list.add("钱六"); |
| list.add("孙七"); |
| |
| |
| Collections.replaceAll(list, "二丫", "王小丫"); |
| System.out.println(list); |
| } |
| } |
| |
| 5、 |
| package com.xxx; |
| |
| import java.util.Objects; |
| |
| public class Person { |
| private String name; |
| private int age; |
| |
| public Person() { |
| } |
| |
| public Person(String name, int age) { |
| this.name = name; |
| this.age = age; |
| } |
| |
| public String getName() { |
| return name; |
| } |
| |
| public void setName(String name) { |
| this.name = name; |
| } |
| |
| public int getAge() { |
| return age; |
| } |
| |
| public void setAge(int age) { |
| this.age = age; |
| } |
| |
| @Override |
| public boolean equals(Object o) { |
| if (this == o) return true; |
| if (o == null || getClass() != o.getClass()) return false; |
| Person person = (Person) o; |
| return age == person.age && |
| Objects.equals(name, person.name); |
| } |
| |
| @Override |
| public int hashCode() { |
| return Objects.hash(name, age); |
| } |
| |
| @Override |
| public String toString() { |
| return "Person{" + |
| "name='" + name + '\'' + |
| ", age=" + age + |
| '}'; |
| } |
| } |
| |
| package com.xxx; |
| |
| import java.util.HashSet; |
| |
| public class Exe5 { |
| public static void main(String[] args) { |
| HashSet<Person> people = new HashSet<>(); |
| people.add(new Person("黑崎",18)); |
| people.add(new Person("蓝染",33)); |
| people.add(new Person("格里姆乔",50)); |
| people.add(new Person("重国",999)); |
| people.add(new Person("蓝染",33)); |
| people.add(new Person("蓝染",43)); |
| |
| |
| for (Person person : people) { |
| System.out.println(person); |
| } |
| } |
| } |
| |
本文作者:Ritchie里其
本文链接:https://www.cnblogs.com/wang-zeyu/p/16793129.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步