Java-11.18

 Map集合的数据结构只跟键有关

  运行 可以插入null值和null 键

  哈希表数据结构---->保证元素的唯一性,靠元素重写 equals方法和HashCode 方法

  String  Integer 默认重写了equals方法和HashCode 方法的

package org.lemon.Map;
//HashMap集合键是String,值是Student
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

/**
 * @author zhaojiangbo
 * Map集合的数据结构只跟键有关
 */
public class HashMapDemo {
    public static void main(String[] args) {
	//创建hashmap集合	
    	HashMap<String, Student> hm = new HashMap<String, Student>();
	//添加	
    	    hm.put("s1", new Student("我",12));
		hm.put("s2", new Student("依",15));
		hm.put("s3", new Student("然",17));
		hm.put("s4", new Student("爱",14));
		hm.put("s5", new Student("你",11));
		hm.put("s5", new Student("你",11));
		
	//遍历 方法1
		Set<String> keySet = hm.keySet();
		for(String s : keySet) {
			System.out.println(s +"---"+ hm.get(s).getName()+"---"+hm.get(s).getAge());
		}
        System.out.println("------------------");
	//遍历 方式2
		Set<Entry<String,Student>> entrySet = hm.entrySet();
		for(Entry<String,Student> en :entrySet) {
			String key = en.getKey();
			Student value = en.getValue();
			System.out.println(key +"---"+value.getName()+"---"+value.getAge() );
		}
	}

}


package org.lemon.Map;


public class Student {
  private String name;
  private int age;
  public Student() {
		super();
		
	}

	public Student(String name, int age) {
		super();
		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 String toString() {
		return "Student [getName()=" + getName() + ", getAge()=" + getAge() + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
}


LinkedHashMap

     Map接口的哈希表和链接列表实现,具有可预知的迭代顺序

     底层的数据结构是链表和哈希表 元素有序 且唯一

     链表数据结构保证元素的有序性     哈希表数据结构保证元素的唯一性


package org.lemon.Map;

import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;

/**
 * @author zhaojiangbo
 * LinkedHashMap
 *  Map接口的哈希表和链接列表实现,具有可预知的迭代顺序
 */
public class LinkedHashMapDemo {
  public static void main(String[] args) {
	//创建linkedhashmap对象
	  LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>();
	//添加  
	  lhm.put(1, "透体圣光");
	  lhm.put(2, "炙热银弹");
	  lhm.put(3, "冷酷追击");
	  lhm.put(4, "圣枪洗礼");
	//遍历 方式1
	  Set<Integer> keySet = lhm.keySet();
	  for(Integer key : keySet) {
		  System.out.println(key +"---"+lhm.get(key));
	  }
	  System.out.println("--------------");
	//遍历 方式2
	  Set<Entry<Integer,String>> entrySet = lhm.entrySet();
	  for(Entry<Integer,String> en :entrySet) {
		  Integer key = en.getKey();
		  String value = en.getValue();
		  System.out.println(key +"---"+value);
	  }
}
}

TreeMap

    数据结构是红黑树  元素唯一,能对元素进行排序(自然排序和比较器排序)

    TreeMap 不允许插入null键

package org.lemon.Map;

import java.util.TreeMap;

/**
 * @author zhaojiangbo
 *TreeMap
 *数据结构是红黑树
 */
public class TreeMapDemo {
   public static void main(String[] args) {
	//
	   TreeMap<String,String> treeMap = new TreeMap<String, String>();
	//
	 //  treeMap.put(null, null);//   错误的
	 //  treeMap.put(null, "qwer");// 错误的
	   treeMap.put("qaqaqa", "df");
	   System.out.println(treeMap.size());
	   
}
}

package org.lemon.Map;

import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

/**
 * @author zhaojiangbo
 * treeMap
 * 存储键是Integer 值是 String 类型的数据
 */
public class TreeMapDemo2 {
   public static void main(String[] args) {
	//创建treemap对象
	   TreeMap<Integer,String> treeMap = new TreeMap<Integer,String>();
	//添加
	   treeMap.put(3,"闪钢斩");
	   treeMap.put(6,"风之屏障");
	   treeMap.put(2,"踏前斩");
	   treeMap.put(4,"绝息狂风斩");
	//遍历 方式1
	   Set<Integer> keySet = treeMap.keySet();
	   for(Integer i :keySet) {
		   System.out.println(i +"---"+treeMap.get(i));
	   }
	   System.out.println("--------------");
	//遍历 方式2
	   Set<Entry<Integer,String>> entrySet = treeMap.entrySet();
	   for(Entry<Integer,String> en : entrySet) {
		   Integer key = en.getKey();
		   String value = en.getValue();
		   System.out.println(key +"---"+value);
		   
	   }
}
}

键是 Student 类型的 值是String 类型的 数据

      排序的元素必须实现Comparable接口 重写CmcompareTo(T)方法

package org.lemon.Map2;

import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

/**
 * @author zhaojiangbo
 *键是 Student 类型的 值是String 类型的 数据
 */
public class TreeMapDemo3 {
   public static void main(String[] args) {
	//创建treemap对象
	   TreeMap<Student,String> treeMap = new TreeMap<Student,String>();
	//添加
	   treeMap.put(new Student("放逐之刃",23), "s1" );
	   treeMap.put(new Student("疾风剑豪",20), "s2");
	   treeMap.put(new Student("圣枪游侠",21), "s3");
	   treeMap.put(new Student("机械先驱",25), "s4");
	   treeMap.put(new Student("麦林炮手",22), "s5");
	//遍历
	   Set<Entry<Student,String>> entrySet = treeMap.entrySet();
	   for(Entry<Student,String> en : entrySet) {
		   Student key = en.getKey();
		   String value = en.getValue();
		   System.out.println(key.getName()+"---"+key.getAge()+"---"+value);
		     }
 }
}


测试题

package org.lemon.MapTest;

import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;

/**
 * @author zhaojiangbo 需求:用户随便输入一段字符串 统计字符串中每个字符出现的次数
 */
public class MapTestDemo {

	public static void main(String[] args) {
		// 创建hashmap对象
		HashMap<Character, Integer> map = new HashMap<Character, Integer>();

		Scanner sc = new Scanner(System.in);
		System.out.println("请输入一串字母");
		String str = sc.nextLine();
		// 把字符串转成一个字符数组
		char[] chs = str.toCharArray();
		// 遍历这个数组
		for (char key : chs) {
			Integer value = map.get(key);
			if (value == null) {
				map.put(key, 1);

			} else {
				value++;
				map.put(key, value);// 键相同,值覆盖
			}
		}
		// 遍历
		Set<Character> keySet = map.keySet();
		StringBuilder sb = new StringBuilder();
		for (Character ch : keySet) {
			sb.append(ch).append("(").append(map.get(ch)).append(")");

		}
		System.out.println(sb.toString());
	}
}

package org.lemon.MapTest;

import java.util.HashMap;
import java.util.Set;

/**
 * @author zhaojiangbo
 * 输出:
 * 基础班
		 高圆圆 ---20
	     黄晓波 ---22
   就业班
		 杨桃--- 21
		 果然 ---23
 */
public class MapTestDemo2 {
  public static void main(String[] args) {
	// 创建2个 小的集合
	  HashMap<String,Integer> basicMap = new HashMap<String, Integer>();
	  basicMap.put("高圆圆", 20);
	  basicMap.put("黄晓波", 22);
	  HashMap<String, Integer> workMap = new HashMap<String, Integer>();
	  workMap.put("杨桃", 21);
	  workMap.put("果然", 23);
	//创建大集合
	  HashMap<String,HashMap<String,Integer>> superMap = new HashMap<String, HashMap<String,Integer>>();
	  superMap.put("基础班", basicMap);
	  superMap.put("就业班", workMap);
	  
	//遍历大集合
	  Set<String> keySet = superMap.keySet();
	  for(String s : keySet) {
		  System.out.println(s);
	// 拿出每一个小集合	  
		  HashMap<String,Integer> hashMap = superMap.get(s);
	//再遍历小集合
		  Set<String> smallSet = hashMap.keySet();
		  for(String s2 : smallSet) {
		  System.out.println("\t"+s2 +"---"+hashMap.get(s2));
		  }
		  System.out.println();
		  }
	}
}

package org.lemon.MapTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

/**
 * @author zhaojiangbo
 *得到:  
 *  周瑜---小乔
	吕布---貂蝉
	郭靖---郭靖
    杨过---小龙女
	令狐冲---任盈盈
    林平之---岳灵珊
 */
public class MapTestDemo3 {
    public static void main(String[] args) {
	//先创建 3个 hashmap 对象 并 添加值
    	 HashMap<String,String> sgMap = new HashMap<String,String>();
    	 sgMap.put("周瑜", "小乔");
    	 sgMap.put("吕布", "貂蝉");
    	 HashMap<String,String> sdMap = new HashMap<String,String>();
    	 sdMap.put("郭靖", "黄蓉");
    	 sdMap.put("过儿", "龙儿");
    	 HashMap<String,String> xaMap = new HashMap<String,String>();
    	 xaMap.put("令狐冲", "任盈盈");
    	 xaMap.put("林平之", "岳灵珊");
    	 //再创建 arraylist 对象并添加值
    	 ArrayList<HashMap<String,String>> arrayList = new ArrayList<HashMap<String,String>>();
    	 arrayList.add(sgMap);
    	 arrayList.add(sdMap);
    	 arrayList.add(xaMap);
    	 //遍历
    	 for(HashMap<String,String> hm :arrayList) {
    		 Set<String> keySet = hm.keySet();
    		 for(String s : keySet) {
    			 System.out.println("\t"+s +"---"+hm.get(s));
    		 }
    		 System.out.println();
    	 }
	}
}

package org.lemon.MapTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

/**
 * @author zhaojiangbo
 *得到:
 *三国演义
	 吕布
	 周瑜
  
  笑傲江湖
	 令狐冲
	 林平之
  
  神雕侠侣
	 郭靖
	 杨过
 */
public class MapTestDemo4 {
 public static void main(String[] args) {
	//先创建3 个 ArrayList 集合并添加值
	 ArrayList<String> sgList = new ArrayList<String>();
	 sgList.add("吕布");
	 sgList.add("周瑜");
	 ArrayList<String> xaList = new ArrayList<String>();
	 xaList.add("令狐冲");
	 xaList.add("林平之");
	 ArrayList<String> sdList = new ArrayList<String>();
	 sdList.add("靖哥哥");
	 sdList.add("过儿");
	//再创建  HashMap集合并添加值
	 HashMap<String, ArrayList<String>> hashMap = new HashMap<String,  ArrayList<String>>();
	 hashMap.put("三国演义", sgList);
	 hashMap.put("笑傲江湖", xaList);
	 hashMap.put("神雕侠侣", sdList);
	 //遍历
	 Set<Entry<String,ArrayList<String>>> entrySet = hashMap.entrySet();
	 for(Entry<String,ArrayList<String>> en : entrySet) {
		 String key = en.getKey();
		 System.out.println(key);
		 ArrayList<String> value = en.getValue();
     //再遍历		
		 for(String s : value) {
			 System.out.println("\t"+s);
		 }
		 System.out.println();
	 }
 
}
}

集合工具类

package org.lemon.ListUtil;

import java.util.ArrayList;
import java.util.Collections;

/**
 * @author zhaojiangbo 
 * Collections成员方法
 *        public static <T> void sort(List<T>list): 排序,默认按照自然顺序 
 *        public static <T> int binarySearch(List<?> list,T key): 二分查找
 *        public static <T> T max(Collection<?> coll): 获取最大值 
 *        public static void reverse(List<?> list): 反转 
 *        public static void shuffle(List<?> list): 随机置换
 */
public class ListUtilDemo {
	public static void main(String[] args) {

		ArrayList<Integer> list = new ArrayList<Integer>();
		list.add(20);
		list.add(30);
		list.add(1);
		list.add(0);
		list.add(10);

		// 集合工具类进行排序
		Collections.sort(list);

		// public static <T> int binarySearch(List<?> list,T key): 二分查找 前提是元素必须有序

		int index = Collections.binarySearch(list, 20);
		System.out.println(index);

		Integer max = Collections.max(list);
		Integer min = Collections.min(list);
		System.out.println(max + "==" + min);

		Collections.reverse(list);

		System.out.println(list);

	}

}


斗地主游戏

package org.lemon.Poker;

import java.util.ArrayList;
import java.util.Collections;

public class PokerGameDemo {
	public static void main(String[] args) {
		// 斗地主 实现 发牌 和看牌
		// 创建一个牌盒子
		ArrayList<String> pokerBox = new ArrayList<String>();
		// 定义花色数组
		String[] colors = { "♥", "♠", "♦", "♣" };
        // 定义牌号数组
		String[] nums = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
		// 循环拼牌
		for (String color : colors) {
			for (String num : nums) {

				pokerBox.add(color + num);

			}

		}
		// 添加大小王
		pokerBox.add("☼");
		pokerBox.add("☽");
		// 洗牌
		Collections.shuffle(pokerBox);
		Collections.shuffle(pokerBox);
		Collections.shuffle(pokerBox);

		// 发牌 三个人 一副底牌
		// 创建三个人的集合
		ArrayList<String> 高圆圆 = new ArrayList<>();
		ArrayList<String> 杨桃 = new ArrayList<>();
		ArrayList<String> 黄晓波 = new ArrayList<>();
		ArrayList<String> 底牌 = new ArrayList<>();
		
		for (int i = 0; i < pokerBox.size(); i++) {
			// 留底牌
			if (i >= pokerBox.size() - 3) {

				底牌.add(pokerBox.get(i));
			} else if (i % 3 == 0) {

				高圆圆.add(pokerBox.get(i));

			} else if (i % 3 == 1) {

				杨桃.add(pokerBox.get(i));

			} else if (i % 3 == 2) {

				黄晓波.add(pokerBox.get(i));

			}

		}

		// 看牌
		lookPai("高圆圆", 高圆圆);
		lookPai("杨桃", 杨桃);
		lookPai("黄晓波", 黄晓波);
		lookPai("底牌", 底牌);

		// System.out.println(pokerBox);

	}

	private static void lookPai(String string, ArrayList<String> list) {
		System.out.println(string);
		for (String pai : list) {

			System.out.print(pai + "  ");

		}

		System.out.println();

	}

}






























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值