家居企业网站建设如何,西安手机网站制作,做网站的相关规定,wordpress重新发布文章目录一、Map1. 概述2. 基本功能3. 遍历4. 遍历学生对象5. 集合嵌套6. 统计字符出现次数二、Collections1. 常用方法2. 学生对象排序三、模拟斗地主一、Map
1. 概述
Interface MapK, V#xff1a;K 是键的类型#xff0c;V 是值的类型。 将键映射到值的对象K, VK 是键的类型V 是值的类型。 将键映射到值的对象不能包含重复的键每个键可以映射到最多一个值。 举例学生的学号和姓名学号是键姓名是值。 tyut001 刘德华 tyut002 张学友 tyut003 成吉思汗 创建 Map 集合的对象 采用多态的方式具体的实现类是 HashMap。 //Test.javapackage com.zxe;import java.util.HashMap;
import java.util.Map;public class Test {public static void main(String[] args) {MapString, String map new HashMap();map.put(tyut001, 刘德华);map.put(tyut002, 张学友);map.put(tyut003, 成吉思汗);map.put(tyut003, 程咬金);System.out.println(map);}
} 利用 HashMap 保证了元素的唯一性键是唯一的当出现重复的键时后者会把前者的值替换掉
2. 基本功能 //打印输出键值对中所有的值package com.zxe;import java.util.Collection;
import java.util.HashMap;
import java.util.Map;public class Test {public static void main(String[] args) {MapString, String map new HashMap();map.put(tyut001, 刘德华);map.put(tyut002, 张学友);map.put(tyut003, 成吉思汗);CollectionString values map.values();for (String s : values) {System.out.println(s);}}
} 3. 遍历
1方式一
遍历思路 ① 先获取所有键的集合用 keySet() 方法实现 ② 然后遍历键的集合获取到每一个键用增强 for 实现 ③ 根据键全找值用 get(Object key) 方法实现。
package com.zxe;import java.util.HashMap;
import java.util.Map;
import java.util.Set;public class Test {public static void main(String[] args) {MapString, String map new HashMap();map.put(tyut001, 刘德华);map.put(tyut002, 张学友);map.put(tyut003, 成吉思汗);SetString key map.keySet();for (String k : key) {String value map.get(k);System.out.println(k , value);}}
} 2方式二
遍历思路 ① 直接获取所有键值对对象的集合用 entrySet() 方法实现集合为 Set 型每一个键值对对象用 Map.EntryString, String 接收 ② 遍历键值对对象的集合得到每一个键值对对象用增强 for 实现得到每一个 Map.Entry ③ 根据键值对对象获取键和值用 getKey() 得到键用 getValue() 得到值。
package com.zxe;import java.util.HashMap;
import java.util.Map;
import java.util.Set;public class Test {public static void main(String[] args) {MapString, String map new HashMap();map.put(tyut001, 刘德华);map.put(tyut002, 张学友);map.put(tyut003, 成吉思汗);SetMap.EntryString, String entries map.entrySet();for (Map.EntryString, String entry : entries) {String key entry.getKey();String value entry.getValue();System.out.println(key , value);}}
}
4. 遍历学生对象
需求创建一个 HashMap 集合键是学号String值是学生对象Student存储三个键值对元素并遍历。
思路 ① 定义学生类 ② 创建 HashMap 集合对象 ③ 创建学生对象 ④ 把学生添加到集合 ⑤ 遍历集合。
//Student.javapackage com.zxe;public class Student {private String name;private int age;private String sex;Overridepublic String toString() {return Student{ name name \ , age age , sex sex \ };}public Student() {}public Student(String name, int age, String sex) {this.name name;this.age age;this.sex sex;}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;}public String getSex() {return sex;}public void setSex(String sex) {this.sex sex;}
}
//Test.javapackage com.zxe;import java.util.HashMap;
import java.util.Map;
import java.util.Set;public class Test {public static void main(String[] args) {Student s1 new Student(刘德华, 60, 男);Student s2 new Student(张学娥, 21, 女);Student s3 new Student(李华, 14, 男);String sno1 001;String sno2 002;String sno3 003;HashMapString, Student allstudent new HashMap();allstudent.put(sno1, s1);allstudent.put(sno2, s2);allstudent.put(sno3, s3);SetMap.EntryString, Student entries allstudent.entrySet();for (Map.EntryString, Student entry : entries) {String key entry.getKey();Student value entry.getValue();System.out.println(key , value);}}
} 思考一下如果我们的键是用户定义的学生对象值是居住地址这时候如何保证学生对象成员变量的值不重复的呢 在学生类中重写两个方法hashCode() 和 equals()这两个方法在之前的内容中有讲过Alt Insert 快捷键自动生成。 以学生对象 s1 和 s2 为例HashMap 保证的唯一性是指该集合中不允许添加两个或更多的 s1但可以添加一个 s1 和一个 s2即使 s1 和 s2 中的内容一模一样但它并不在意二者内容如何而重写 hashCode() 和 equals() 方法保证的唯一性是指它不允许两个对象中出现完全相同的内容。所以要想保证真正意义上的唯一性应同时使用 HashMap 和 hashCode()、equals() 方法
5. 集合嵌套
//Test.javapackage com.zxe;import java.util.*;public class Test {public static void main(String[] args) {HashMapInteger, String hm1 new HashMap();hm1.put(1, 孙策);hm1.put(2, 大桥);HashMapInteger, String hm2 new HashMap();hm2.put(3, 吕布);hm2.put(4, 貂蝉);ArrayListHashMapInteger, String a new ArrayList();a.add(hm1);a.add(hm2);System.out.println(a);}
} 直接输出或者也可以用遍历的方式输出
//方法一
for (HashMapInteger, String hm : a) {SetMap.EntryInteger, String entries hm.entrySet();for (Map.EntryInteger, String entry : entries) {System.out.println(entry.getKey() , entry.getValue());}}//方法二
for (HashMapInteger, String hm : a) {SetInteger keys hm.keySet();for (Integer key : keys) {String value hm.get(key);System.out.println(key , value);}} 6. 统计字符出现次数
需求键盘录入一个字符串要求统计字符串中每个字符串出现的次数。
思路 ① 键盘录入一个字符串 ② 创建 HashMap 集合键是 Character值是 Integer ③ 遍历字符串得到每一个字符 ④ 拿得到的每一个字符作为键到 HashMap 集合中去找对应的值看其返回值返回值为 null 说明该字符在 HashMap 集合中不存在就把该字符作为键1 作为值存储如果返回值不是 null 说明该字符在 HashMap 集合中存在把该键所对应的值加 1然后重新存储该键值对 ⑤ 遍历 HashMap 集合得到键和值按照要求进行拼接。
//Test.javapackage com.zxe;import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;public class Test {public static void main(String[] args) {Scanner sc new Scanner(System.in);System.out.println(请输入一个字符串);String str sc.nextLine();HashMapCharacter, Integer hm new HashMap();for (int i 0; i str.length(); i) {Character c str.charAt(i);if (hm.containsKey(c)) {Integer newValue hm.get(c) 1;hm.put(c, newValue);} else {hm.put(c, 1);}}SetMap.EntryCharacter, Integer entries hm.entrySet();for (Map.EntryCharacter, Integer entry : entries) {Character letter entry.getKey();Integer num entry.getValue();System.out.println(letter 出现了 num 次);}}
} 二、Collections
1. 常用方法
Collections 是针对集合操作的工具类。 常用方法 ① sort()将指定的列表按升序排序 ② reverse()反转指定列表中元素的顺序 ③ shuffle()使用默认的随机源随机排列指定的列表。 //Test.javapackage com.zxe;import java.util.ArrayList;
import java.util.Collections;
import java.util.List;public class Test {public static void main(String[] args) {ListInteger list new ArrayList();list.add(1);list.add(6);list.add(7);list.add(2);list.add(4);list.add(9);Collections.reverse(list);System.out.println(反转 list);Collections.sort(list);System.out.println(升序排序 list);Collections.shuffle(list);System.out.println(随机排序 list);}
} 这是升序排序的方法如果想要降序排序完全可以先对集合进行升序排序然后再反转就得到了降序集合。 2. 学生对象排序
需求ArrayList 存储学生对象使用 Collections 对 ArrayList 进行排序要求按照年龄从大到小排序年龄相同时按照姓名的字母顺序排序。
//Test.javapackage com.zxe;import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;public class Test {public static void main(String[] args) {Student s1 new Student(zhangliang, 17);Student s2 new Student(lvbu, 23);Student s3 new Student(jiangziya, 31);Student s4 new Student(kuangtie, 23);Student s5 new Student(sunce, 27);ArrayListStudent stus new ArrayList();stus.add(s1);stus.add(s2);stus.add(s3);stus.add(s4);stus.add(s5);Collections.sort(stus, new ComparatorStudent() {Overridepublic int compare(Student o1, Student o2) {int num o1.getAge() - o2.getAge();int num2 num 0 ? o1.getName().compareTo(o2.getName()) : num;return num2;}});for (Student s : stus) {System.out.println(s.getName() , s.getAge());}}
} 这里排序的时候 sort() 方法是需要传入两个参数的第一个就是要排序的集合第二个需要指定一个比较器之前我们学过的两种排序方法自然排序和比较器排序自然排序需要在学生类里面实现一个接口这边我们通过比较器的方式来实现排序。 三、模拟斗地主
需求通过程序实现斗地主过程中的洗牌、发牌和看牌要求对牌进行排序。
思路 ① 创建 HashMap键是编号值是牌 ② 创建 ArrayList存储编号 ③ 创建花色数组和点数数组 ④ 从 0 开始往 HashMap 里面存储编号并存储对应的牌同时往 ArrayList 里面存储编号 ⑤ 洗牌洗的是编号用 Collections 的 shuffle() 方法实现 ⑥ 发牌发的也是编号为了保证编号是排序的创建 TreeSet 集合接收 ⑦ 定义方法看牌遍历 TreeSet 集合获取编号到 HashMap 集合去到对应的牌 ⑧ 调用看牌方法。
//Test.javapackage com.zxe;import java.util.*;public class Test {public static void main(String[] args) {HashMapInteger, String pokers new HashMap();ArrayListInteger indexs new ArrayList();String[] colors {♥, ♠, ♦, ♣};String[] numbers {A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K};String[] kings {大王, 小王};Integer index 0;for (String number : numbers) {for (String color : colors) {pokers.put(index, color number);indexs.add(index);index;}}for (String king : kings) {pokers.put(index, king);indexs.add(index);index;}Collections.shuffle(indexs);TreeSetInteger indexsOfPlayer1 new TreeSet();TreeSetInteger indexsOfPlayer2 new TreeSet();TreeSetInteger indexsOfPlayer3 new TreeSet();TreeSetInteger indexsRetains new TreeSet();for (int i 0; i indexs.size(); i) {Integer in indexs.get(i);if (i indexs.size() - 3) {indexsRetains.add(in);} else {switch (i % 3) {case 0:indexsOfPlayer1.add(in);break;case 1:indexsOfPlayer2.add(in);break;default:indexsOfPlayer3.add(in);break;}}}showPokers(刘德华, indexsOfPlayer1, pokers);showPokers(张学友, indexsOfPlayer2, pokers);showPokers(程咬金, indexsOfPlayer3, pokers);System.out.println(\n底牌是);for (Integer i : indexsRetains) {String retain pokers.get(i);System.out.print(retain );}}public static void showPokers(String name, TreeSetInteger indexsOfPlayer, HashMapInteger, String pokers) {System.out.println(\n name 的牌);for (Integer i : indexsOfPlayer) {String poker pokers.get(i);System.out.print(poker );}}
}