4、 MAP集合:双列
在java.util包下面,需要导包
是一个接口,interface Map <K,V>:K:键的类型;V:值的类型。
将键映射到值的对象;不能包含重复的键;每个键可以映射到最多一个值。
接口不能直接用,需要一个实现类。
创建Map集合的对象:
多态的方式
具体的实现类HashMap
public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); map.put("001","zzzz"); map.put("002","aaaa"); map.put("003","pppp"); map.put("003","ppppa"); System.out.println(map); }
{001=zzzz, 002=aaaa, 003=ppppa}
4.1 Map集合的基本功能
put(K key,V value):添加元素。
remove(Object key):根据键删除对应键值对的元素。
void clear():一处所有的键值对的怨怒是
containsKey(object key):判断集合是否包含指定的键。
containsValue(object value):判断集合是否包含指定的值
isEmpty():判断集合是否为空。
int size():集合的长度,也就是集合中键值对的个数。
public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); map.put("df","kjf"); map.put("zz","tt"); map.put("002","56"); map.remove("002"); // map.clear(); System.out.println(map.isEmpty()); System.out.println(map.size()); System.out.println(map);
false 2 {zz=tt, df=kjf}
4.2 Map集合的获取功能
get(Object key):根据键获取值
Set《K》 keyset():获取所有键的集合
Collection《V》 values():获取所有值的集合
Set《Map.Entry<K,V>> entrySet():获取所有键值对对象的集合。
public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); map.put("df","kjf"); map.put("zz","tt"); map.put("002","56"); System.out.println(map.get("zz"));//tt Set<String> key = map.keySet(); for (String i:key){ System.out.println(i); } Collection<String> values = map.values(); for (String y :values){ System.out.println(y); }
tt zz df 002 tt kjf 56
4.3 Map集合的遍历
第一种:获得所有键,通过键找值。
Map<String,String> map = new HashMap<String,String>(); map.put("df","kjf"); map.put("zz","tt"); map.put("002","56"); Set<String> s = map.keySet(); for (String i :s){ String y = map.get(i); System.out.println(y+","+i); }
tt,zz kjf,df 56,002
第二种:
public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); map.put("df","kjf"); map.put("zz","tt"); map.put("002","56"); Set<Map.Entry<String, String>> skv = map.entrySet(); for (Map.Entry<String, String> me: skv){ String key = me.getKey(); String value = me.getValue(); System.out.println(key+","+value); }
zz,tt df,kjf 002,56
4.4Hashmap集合存储学生并遍历
public static void main(String[] args) { HashMap<String,Student> h = new HashMap<String,Student>(); Student s1 = new Student("aa",11); Student s2 = new Student("pp",99); Student s3 = new Student("hh",55); h.put("001",s1); h.put("002",s2); h.put("003",s3); //方式一 Set<String> kk = h.keySet(); for (String i :kk){ Student o = h.get(i); System.out.println(i+","+o.getName()+","+o.getAge()); } //方式二 Set<Map.Entry<String, Student>> ee = h.entrySet(); for (Map.Entry<String, Student> p:ee){ String key = p.getKey(); Student value = p.getValue(); System.out.println(key+","+value.getName()+","+value.getAge()); } }
001,aa,11 002,pp,99 003,hh,55 001,aa,11 002,pp,99 003,hh,55
保证键的唯一性:如果学生对象的成员变量值相同,我们就认为是同一个对象。
HashMap<Student,String> p = new HashMap<Student,String>(); Student s1 = new Student("aa",11); Student s2 = new Student("pp",99); Student s3 = new Student("hh",55); Student s4 = new Student("hh",55); p.put(s1,"25"); p.put(s2,"2"); p.put(s3,"003"); p.put(s4,"003"); Set<Student> o = p.keySet(); for (Student s : o){ System.out.print(s.getName()+","+s.getAge()+","); String ss = p.get(s); System.out.println(ss); } hh,55,003 aa,11,25 pp,99,2 hh,55,003
上面一样的还能输出,所以不唯一,要重写
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return age == student.age && Objects.equals(name, student.name); } @Override public int hashCode() { return Objects.hash(name, age); } pp,99,2 hh,55,003 aa,11,25
4.5案例,用Arraylist集合存储hashmap元素并遍历。
需求:创建一个ArrayList集合,存储三个元素,每一个元素都是HashMap,每一个HashMap的键和值都是String,并遍历。
public static void main(String[] args) { ArrayList<HashMap<String,String>> array = new ArrayList<HashMap<String,String>>(); HashMap<String,String> hm1 = new HashMap<String,String>(); hm1.put("孙策","大桥"); hm1.put("周瑜","小乔"); array.add(hm1); HashMap<String,String> hm2 = new HashMap<String,String>(); hm2.put("郭靖","黄蓉"); hm2.put("杨过","小龙女"); array.add(hm2); HashMap<String,String> hm3 = new HashMap<String,String>(); hm3.put("令狐冲","任盈盈"); hm3.put("零平之","岳灵珊"); array.add(hm3); for (HashMap<String,String> hh : array){ Set<String> strings = hh.keySet(); for (String o :strings){ String s = hh.get(o); System.out.println(o+","+s); } }
孙策,大桥 周瑜,小乔 杨过,小龙女 郭靖,黄蓉 令狐冲,任盈盈 零平之,岳灵珊
4.6 案例,HashMap集合存储ArrayList
需求:创建一个HashMap集合,存储三个键值对元素,每一个键值的元素都市String,值是ArrayList,每一个ArrayList的元素都是String,并遍历。
public static void main(String[] args) { HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>(); ArrayList<String> arr1 = new ArrayList<String>(); arr1.add("诸葛亮"); arr1.add("赵云"); hm.put("三国演义",arr1); ArrayList<String> arr2 = new ArrayList<String>(); arr2.add("唐曾"); arr2.add("孙悟空"); hm.put("西游记",arr2); ArrayList<String> arr3 = new ArrayList<String>(); arr3.add("武松"); arr3.add("老虎"); hm.put("水浒传",arr3); Set<String> s = hm.keySet(); for (String i : s){ ArrayList<String> v = hm.get(i); for (String p : v){ System.out.print(p+","); } System.out.println(i); } }
武松,老虎,水浒传 诸葛亮,赵云,三国演义 唐曾,孙悟空,西游记
4.7 统计每一个字符串中出现的次数
需求:键盘录入一个字符串,要求统计字符串中每个字符穿出现的次数。
public static void main(String[] args) { Scanner sc = new Scanner(System.in); String input = sc.nextLine(); HashMap<Character,Integer> hm = new HashMap<Character,Integer>(); for (int i=0 ;i<input.length();i++){ char key = input.charAt(i); Integer value = hm.get(key); if (value==null){ hm.put(key,1); }else{ value++; hm.put(key,value); } } StringBuilder sb = new StringBuilder(); Set<Character> keyset = hm.keySet(); for (Character c : keyset){ Integer value = hm.get(c); sb.append(c).append("(").append(value).append(")"); } String result = sb.toString(); System.out.println(result); }
aaatttuuuiioopnjgg p(1)a(3)t(3)u(3)g(2)i(2)j(1)n(1)o(2)
用treemap可以进行对键进行排序。
5、Collections
5.1概述
是具体的类,是static方法,可以直接用类名访问
Collections类中常用的方法:
public static void main(String[] args) { List<Integer> l = new ArrayList<Integer>(); l.add(1); l.add(5); l.add(99); l.add(3); // Collections.sort(l);//[1, 3, 5, 99] // Collections.reverse(l);//[3, 99, 5, 1] // Collections.shuffle(l);//[3, 1, 5, 99] System.out.println(l); }
5.2ArrayLiat集合存储学生对象并排序
public static void main(String[] args) { Student s1 = new Student("aa",12); Student s2 = new Student("yy",56); Student s3 = new Student("dd",5); Student s4 = new Student("oo",5); ArrayList<Student> arr = new ArrayList<Student>(); arr.add(s1); arr.add(s2); arr.add(s3); arr.add(s4); Collections.sort(arr, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { int num = o1.getAge()- o2.getAge(); // System.out.println(o1.getName().compareTo(o2.getName())); int num2 = num==0?o1.getName().compareTo(o2.getName()):num; return num2; } }); for (Student s : arr){ System.out.println(s.getAge()+","+s.getName()); } }
5,dd 5,oo 12,aa 56,yy
5.3模拟斗地主
实现洗牌,发牌,看牌
public class demo1 { public static void main(String[] args) { ArrayList<String> arr = new ArrayList<String>(); String[] colors = {"♦", "♣", "♠", "♥"}; String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; for (String color : colors) { for (String number : numbers) { arr.add(color + number); } } arr.add("大王"); arr.add("小王"); Collections.shuffle(arr); // System.out.println(arr); ArrayList<String> p1 = new ArrayList<String>(); ArrayList<String> p2 = new ArrayList<String>(); ArrayList<String> p3 = new ArrayList<String>(); ArrayList<String> dipai = new ArrayList<String>(); for (int i = 0; i < arr.size(); i++) { String poker = arr.get(i); if (i >= arr.size() - 3) { dipai.add(poker); } else if (i % 3 == 0) { p2.add(poker); } else if (i % 3 == 1) { p1.add(poker); } else if (i % 3 == 2) { p3.add(poker); } } lookpoker("1", p1); lookpoker("2", p2); lookpoker("3", p3); lookpoker("底牌",dipai); } public static void lookpoker(String name, ArrayList<String> arr) { System.out.println(name + "的牌是:"); for (String poker : arr) { System.out.print(poker + " "); } System.out.println(); } }
1的牌是: ♥5 ♣7 ♣5 ♠7 ♠10 ♠5 ♣K ♥9 ♣2 ♥6 ♦4 ♦5 ♠A ♦7 ♥K ♠Q ♥J 2的牌是: ♠2 ♦3 ♥10 ♦A ♦J 大王 ♦K ♦10 ♠J ♠4 ♦9 ♣6 ♦Q ♥4 ♠6 ♥A ♦2 3的牌是: ♣J ♣Q ♥7 ♣10 ♠9 ♣9 ♣4 ♥8 ♥Q ♣A ♥2 ♥3 ♦6 ♣3 ♠8 ♠K ♦8 底牌的牌是: ♣8 ♠3 小王 Process finished with exit code 0
升级版
public static void main(String[] args) { ArrayList<String> arr = new ArrayList<String>(); String[] colors = {"♦", "♣", "♠", "♥"}; String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; for (String color : colors) { for (String number : numbers) { arr.add(color + number); } } arr.add("小王"); arr.add("大王"); HashMap<Integer, String> hm = new HashMap<Integer, String>(); ArrayList<Integer> arr2 = new ArrayList<Integer>(); int index = 0; for (String number : numbers) { for (String color : colors) { hm.put(index, color + number); arr2.add(index); index++; } } hm.put(index, "小王"); arr2.add(index); index++; hm.put(index, "大王"); arr2.add(index); Collections.shuffle(arr2); // System.out.println(arr); TreeSet<Integer> p1 = new TreeSet<Integer>(); TreeSet<Integer> p2 = new TreeSet<Integer>(); TreeSet<Integer> p3 = new TreeSet<Integer>(); TreeSet<Integer> dp = new TreeSet<Integer>(); for (int i = 0; i < arr2.size(); i++) { int x = arr2.get(i); if (i >= arr.size() - 3) { dp.add(x); } else if (i % 3 == 0) { p2.add(x); } else if (i % 3 == 1) { p1.add(x); } else if (i % 3 == 2) { p3.add(x); } } lookpoker("1", p1,hm); lookpoker("2", p2,hm); lookpoker("3", p3,hm); lookpoker("底牌", dp,hm); } public static void lookpoker(String name, TreeSet<Integer> index,HashMap<Integer,String> hm) { System.out.println(name + "的牌是:"); for (Integer key : index) { String value = hm.get(key); System.out.print(value + " "); } System.out.println(); }
1的牌是: ♦2 ♠3 ♦4 ♠4 ♥4 ♦5 ♠6 ♥6 ♠7 ♠8 ♥9 ♠10 ♣Q ♠Q ♠K ♦A ♣A 2的牌是: ♠2 ♥2 ♣3 ♣5 ♠5 ♦6 ♦7 ♥7 ♦10 ♠J ♥J ♦Q ♦K ♥K ♠A ♥A 小王 3的牌是: ♣2 ♦3 ♥3 ♣4 ♥5 ♣6 ♣7 ♦8 ♣8 ♥8 ♠9 ♣10 ♥10 ♣J ♥Q ♣K 大王 底牌的牌是: ♦9 ♣9 ♦J