Map集合
Map是双列集合,存储的是【键值对】.键不能重复。
常用功能
对集合的操作无非就是增加、删除、修改、查询
V put(K key, V value)
往Map集合中添加键值对.返回被覆盖键的值,如果键相同.把值修改了
V remove(Object key)
根据键删除键值对.返回被删除的值
void clear()
清空集合中的元素
int size()
获取集合中【键值对】的个数
V get(Object key)
根据键获取值
Set<K> keySet()
获取所有键的集合
Collection<V> values()
获取所有值的集合
Set<Map.Entry<K,V>> entrySet()
获取所有的【键值对】
boolean containsKey(Object key)
判断键是否存在
boolean containsValue(Object value)
判断值是否存在
boolean isEmpty()
判断集合中是否有元素
Map集合练习题
/*
需求:获取字符串中每一个字符出现的次数
分析:
1. 创建一个HashMap集合把字符当做键,字符的次数当做值存入Map集合
2. 遍历字符串的每一个字符,准备往Map集合中存
a: 如果map中不包含要存入的字符,值默认是1
b: 如果map中包含要存入的字符,先获取值+1,再重复覆入Map集合
3. 最后遍历Map集合
*/
public class Test{
public static void main(String[] args){
//1. 创建一个HashMap集合把字符当做键,字符的次数当做值存入Map集合
HashMap<Character,Integer> map=new HashMap<Character,Integer>();
String str="hellojavagoodgoodstudydaydayup";
//2. 遍历字符串的每一个字符,准备往Map集合中存
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
//判断是否包含字符
if(!map.containsKey(ch)){
map.put(ch,1);
}else{
Integer value=map.get(ch);
map.put(ch,value+1);//覆盖存入
}
}
//3. 最后遍历Map集合
Set<Characher> set=map.keySet();
for(Character key:set){
Integer value= map.get(key);
System.out.println(key+"="+value);
}
}
}
Map集合的遍历
keySet()遍历
keySet()
方法可以获取所有的键,再通过get(key)
获取到键对应的值
Map<String,Integer> map =new HashMap<String,Integer>();
map.put("李四", 6000);
map.put("张三", 7889);
map.put("张三", 7000);
map.put("王五", 9000);
//获取所有的键
Set<String> keys=map.keySet();
//遍历键的集合
for(String key:keys){
//通过键.获取值
String value=map.get(key);
System.out.println(key+"="+value);
}
entrySet()遍历
entrySet()
方法可以获取到所有的【键值对】组成的Set集合,键值对用Map.Entry<K,V>
表示,其中K表示键的类型,V表示值得类型
//在Map集合中存储自定义对象.Student作为键
HashMap<Student,String> map=new HashMap<Student,String>();
map.put(new Student("张三",18), "武汉");
map.put(new Student("王五",20), "北京");
map.put(new Student("李四",19), "南京");
map.put(new Student("张三",18), "上海");
//使用entrySet方法遍历map集合.Student作为键
Set<Entry<Student, String>> entrys = map.entrySet();
for (Entry<Student, String> entry : entrys) {
Student key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"...."+value);
}
可变参数
可变参数用在方法的参数位置,当写一个方法时不确定参数有多少个时,可以使用可变参数。
public void 方法名(参数类型...变量名){
//可变参数本质上是一个数组,直接遍历使用即可。
...
}
//int...arr 本质上就是一个数组。
public int sum(int...arr){
int sum=0;
for(int i=0;i<arr.length;i++){
sum+=arr[i];
}
return sum;
}
public static void main(String[] args){
//求2个数的和。调用sum方法,3和4作为arr数组的元素
System.out.println(sum(3,4)); //7
//求3个数的和。调用sum方法,3,4,5作为arr数组的元素
System.out.println(sum(3,4,5)); //12
}
JDK9的新特性
当我们想要使用集合去存储【几个固定的值的时候】,JDK9提供了几个简化的操作。
List<元素类型> list=List.of(元素1,元素2,元素3...);
Set<元素类型> set=Set.of(元素1,元素2,元素3...);
Map<键的类型,值的类型> map=Map.of(键1,值1,键2,值2...);
public class Demo2 {
public static void main(String[] args) {
List<Integer> list = List.of(1, 2, 3, 4, 5);
Set<String> set = Set.of("hello", "world", "java");
Map<String, Integer> map = Map.of("张三", 20, "李四", 30);
}
}
斗地主发牌案例
采用一种更加面向对象的方式思考问题。我们可以每一张扑克牌看多一个对象,每一个对象包含花色和点数两个属性。
Poker类
/*
Poker类封装花色和的点数
*/
public class Poker {
private String color;
private int number;
public Poker() {
}
public Poker(String color, int number) {
this.color = color;
this.number = number;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
/*
3 4 5...10 J Q K A 2 小王 大王
3 4 5...10 11 12 13 14 15 16 17
*/
@Override
public String toString() {
if(number==11){
return color+"J";
}else if(number==12){
return color+"Q";
}else if(number==13){
return color+"K";
}else if(number==14){
return color+"A";
}else if(number==15){
return color+"2";
}else if(number==16){
return "小王";
}else if(number==17){
return "大王";
}
return color+number;
}
}
测试类
1. 创建一个ArrayList<Poker>集合box,shu存储54g个Poker对象
2. 使用Collections对shuffle方法对box进行打乱
3. 把54个Poker对象,发给3个玩家和底牌
4. 对每一个玩家集合,进行排序
5. 打印每一个玩家集合
public class Demo3{
public static void main(String[] args) {
//准备牌,每一张牌有花色和点数
//准备13个点数
/*
3 4 5...10 J Q K A 2 小王 大王
3 4 5...10 11 12 13 14 15 16 17
*/
List<Integer> numbers= List.of(3,4,5,6,7,8,9,10,11,12,13,14,15);
//准备4个花色
List<String> colors = List.of("♠", "♥", "♣", "♦");
//创建一个ArrayList<Poker>存储每一个Poker对象
ArrayList<Poker> box=new ArrayList<>();
//遍历花色和点数,封装成对象
for (Integer number : numbers) {
for (String color : colors) {
Poker poker = new Poker(color, number);
box.add(poker);
}
}
//小王和大王单独添加
box.add(new Poker("",16));
box.add(new Poker("",17));
//洗牌
Collections.shuffle(box);
//发牌
ArrayList<Poker> player1=new ArrayList<>();
ArrayList<Poker> player2=new ArrayList<>();
ArrayList<Poker> player3=new ArrayList<>();
ArrayList<Poker> dipai=new ArrayList<>();
//遍历序号,让序号%3==0,==1,==2发给不同的玩家
for (int i = 0; i < box.size(); i++) {
Poker poker = box.get(i);
if(i>=51){
dipai.add(poker);
}else{
if(i%3==0){
player1.add(poker);
}else if(i%3==1){
player2.add(poker);
}else if(i%3==2){
player3.add(poker);
}
}
}
Comparator comparator= new Comparator<Poker>() {
@Override
public int compare(Poker o1, Poker o2) {
return o2.getNumber()-o1.getNumber();
}
};
//排序
Collections.sort(player1, comparator);
Collections.sort(player2, comparator);
Collections.sort(player3, comparator);
Collections.sort(dipai, comparator);
//看牌
System.out.println("刘德华:"+player1);
System.out.println("周星驰:"+player2);
System.out.println("周润发:"+player3);
System.out.println("底牌:"+dipai);
}
}