map集合体系
可变参数
package com.peihj.MethodDemo;
import java.lang.reflect.Array;
import java.util.Arrays;
public class methoddemo {
public static void main(String[] args) {
sum();
sum(10,20,30);
sum(new int[]{10,20,30,40}); // 可以传输一个数组
}
public static void sum(int...nums){
// 可变参数内部实质上是一个数组
System.out.println("元素个数:"+nums.length);
System.out.println("元素内容:"+ Arrays.toString(nums));
}
}
可变参数必须放在形参列表的最后面
package com.peihj.MethodDemo;
import java.lang.reflect.Array;
import java.util.Arrays;
public class methoddemo {
public static void main(String[] args) {
sum(10);
sum(10,20,30);
sum(20,new int[]{10,20,30,40}); // 可以传输一个数组
}
public static void sum(int age,int...nums){
// 可变参数内部实质上是一个数组
System.out.println("第一个age:"+age);
System.out.println("元素个数:"+nums.length);
System.out.println("元素内容:"+ Arrays.toString(nums));
}
}
集合工具类collections
package com.peihj.MethodDemo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class collectiondemo {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
Collections.addAll(names,"乔巴","路飞","索隆");
System.out.println(names); // [乔巴, 路飞, 索隆]
Collections.shuffle(names);
System.out.println(names); // [索隆, 路飞, 乔巴]
List<Integer> list = new ArrayList<>();
Collections.addAll(list,10,20,5,6,7,15,0);
System.out.println(list); // [10, 20, 5, 6, 7, 15, 0]
Collections.sort(list);
System.out.println(list); // [0, 5, 6, 7, 10, 15, 20]
}
}
代码演示2:对象排序
package com.peihj.MethodDemo;
import java.util.Objects;
public class apple implements Comparable<apple> {
private String name;
private String color;
private double weight;
private int price;
public apple() {
}
public apple(String name, String color, double weight, int price) {
this.name = name;
this.color = color;
this.weight = weight;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
apple apple = (apple) o;
return Double.compare(apple.weight, weight) == 0 && price == apple.price && Objects.equals(name, apple.name) && Objects.equals(color, apple.color);
}
@Override
public int hashCode() {
return Objects.hash(name, color, weight, price);
}
@Override
public String toString() {
return "apple{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
", weight=" + weight +
", price=" + price +
'}';
}
@Override
// 自定义比较器规则
public int compareTo(apple o) {
// return Double.compare(this.weight,o.weight);
return this.price - o.price;
}
}
package com.peihj.MethodDemo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class test {
public static void main(String[] args) {
List<apple> list = new ArrayList<>();
list.add(new apple("红富士","红色",25.6,9));
list.add(new apple("青苹果","青色",35.6,10));
list.add(new apple("绿苹果","绿色",20.5,8));
list.add(new apple("黄苹果","黄色",15.6,11));
Collections.sort(list); // 因为apple已经设置了比较函数,所以可以,不然报错
System.out.println(list);
Collections.sort(list, new Comparator<apple>() {
@Override
public int compare(apple o1, apple o2) {
return Double.compare(o1.getWeight(),o2.getWeight());
}
});
System.out.println(list);
}
}
案例:斗地主游戏
package com.peihj.MethodDemo;
public class Card {
private String size;
private String color;
private int index; // 代表牌的大小
public Card() {
}
public Card(String size, String color,int index) {
this.size = size;
this.color = color;
this.index = index;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return size + color ;
}
}
package com.peihj.MethodDemo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/*
*
* //定义13个点数的数组
String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
//定义4个花色组合
String[] colors = {"♠","♣","♥","♦"};
*/
public class GameDemo {
// 1、定义一个静态的集合存储54张牌对象
public static List<Card> allcard = new ArrayList<>();
static {
String[] size = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
String[] colors = {"♠","♣","♥","♦"};
int index = 0;
for (String s : size) {
index++;
for (String color : colors) {
// 将其封装成为一个牌对象
Card c = new Card(s,color,index);
allcard.add(c);
}
}
// 将大小王存储
Card c1 = new Card("","🃏",++index);
Card c2 = new Card("","🃏",++index);
Collections.addAll(allcard,c1,c2);
}
public static void main(String[] args) {
System.out.println("洗牌前:"+allcard);
Collections.shuffle(allcard); // 洗牌后
System.out.println("洗牌后:"+allcard);
// 发牌,定义三个人物
List<Card> peo1 = new ArrayList<>();
List<Card> peo2 = new ArrayList<>();
List<Card> peo3 = new ArrayList<>();
// 保留三张底牌
for (int i = 0; i < allcard.size()-3 ; i++) {
Card c = allcard.get(i);
if (i%3==0){
peo1.add(c);
}else if(i % 3==1){
peo2.add(c);
}else if (i%3 == 2){
peo3.add(c);
}
}
// 拿到最后三张底牌
List<Card> lastthreecards = allcard.subList(allcard.size()-3,allcard.size());
// 根据玩家底牌给玩家的牌进行排序
sortcards(peo1);
sortcards(peo2);
sortcards(peo3);
sortcards(lastthreecards);
// 输出玩家的底牌
System.out.println("1号:"+peo1);
System.out.println("2号:"+peo2);
System.out.println("3号:"+peo3);
System.out.println("底牌:"+lastthreecards);
}
public static void sortcards(List<Card> list){
Collections.sort(list, new Comparator<Card>() {
@Override
public int compare(Card o1, Card o2) {
return o2.getIndex() - o1.getIndex();
}
});
}
}
map集合体系
map集合的概述与使用
小结
map集合体系的特点
小结
map集合常用API
package com.peihj.map;
import java.util.HashMap;
import java.util.Map;
public class mapdemo {
public static void main(String[] args) {
Map<String,Integer> maps = new HashMap<>();
maps.put("衣服",100);
maps.put("鞋子",200);
maps.put("裤子",300);
maps.put("毛衣",400);
System.out.println(maps); // {毛衣=400, 鞋子=200, 衣服=100, 裤子=300}
// remove
maps.remove("毛衣");
System.out.println(maps); // {鞋子=200, 衣服=100, 裤子=300}
// clear
// maps.clear();
// System.out.println(maps); //{}
// System.out.println(maps.isEmpty()); // true
Integer k = maps.get("衣服");
System.out.println(k);
// 判断集合是否包含指定的键
System.out.println(maps.containsKey("衣服")); //true
System.out.println(maps.containsKey("衣服1")); // false
// 判断集合是否包含指定的值
System.out.println(maps.containsValue(100)); // true
System.out.println(maps.containsValue(120)); // false
System.out.println(maps.size()); // 3
// 合并其他map集合,扩展
Map<String,Integer> map1 = new HashMap<>();
map1.put("java",1);
map1.put("python",2);
map1.put("c++",3);
map1.put("php",4);
map1.putAll(maps);
System.out.println(map1); // {python=2, c++=3, java=1, 鞋子=200, php=4, 衣服=100, 裤子=300}
}
}
map的遍历1–键找值
package com.peihj.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class mapdemo2 {
public static void main(String[] args) {
Map<String,Integer> maps = new HashMap<>();
maps.put("衣服",100);
maps.put("鞋子",200);
maps.put("裤子",300);
maps.put("毛衣",400);
System.out.println(maps);
// 键找值:第一步找到集合的全部的键
Set<String> Keys = maps.keySet();
for (String key : Keys) {
int value = maps.get(key);
System.out.println(value);
System.out.println(key+"======>"+value);
}
}
}
map的遍历-键值对流程
package com.peihj.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class mapdemo2 {
public static void main(String[] args) {
Map<String,Integer> maps = new HashMap<>();
maps.put("衣服",100);
maps.put("鞋子",200);
maps.put("裤子",300);
maps.put("毛衣",400);
System.out.println(maps);
Set<Map.Entry<String,Integer>> entries = maps.entrySet();
// 开始遍历
for (Map.Entry<String, Integer> entry : entries) {
String key = entry.getKey();
int value = entry.getValue();
System.out.println(key+"======>"+value);
}
}
}
lambda表达式
package com.peihj.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
public class mapdemo2 {
public static void main(String[] args) {
Map<String,Integer> maps = new HashMap<>();
maps.put("衣服",100);
maps.put("鞋子",200);
maps.put("裤子",300);
maps.put("毛衣",400);
System.out.println(maps);
maps.forEach(new BiConsumer<String, Integer>() {
@Override
public void accept(String s, Integer integer) {
System.out.println(s+"====>"+integer);
}
});
}
}
案例:统计投票人数
package com.peihj.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class maptest1 {
public static void main(String[] args) {
String[] select = {"A","B","C","D"};
StringBuilder sb = new StringBuilder();
Random r = new Random();
for (int i = 0; i < 80; i++) {
sb.append(select[r.nextInt(select.length)]);
}
System.out.println(sb);
// 定义一个map集合统计最终的结果,A=30,B=20,C=20,D=10,其中键是景点,值是选择的数量
Map<Character,Integer> infos = new HashMap<>();
for (int i = 0; i < sb.length(); i++) {
char ch = sb.charAt(i);
if (infos.containsKey(ch)){
infos.put(ch,infos.get(ch)+1);
}else {
infos.put(ch,1);
}
}
// 4、输出集合
System.out.println(infos);
}
}
Map集合的实现类HashMap
小结
map集合体系
Linkedhashmap集合体系特点
Treemap集合体系特点
集合的嵌套
package com.peihj.map;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class test {
public static void main(String[] args) {
Map<Integer,String> maps = new TreeMap<>();
maps.put(13,"王麻子");
maps.put(14,"张三");
maps.put(15,"县长");
System.out.println(maps);
// TreeMap集合体系排序,可排序,不重复,无索引
Map<student,String> maps2 = new TreeMap<>(new Comparator<student>() {
@Override
public int compare(student o1, student o2) {
return o2.getAge()-o1.getAge();
}
});
maps2.put(new student("小王",25,'男'),"山东");
maps2.put(new student("小蓝",24,'男'),"山西");
maps2.put(new student("小黄",26,'男'),"河北");
System.out.println(maps2);
}
}
package com.peihj.map;
public class student implements Comparable<student>{
private String name;
private int age;
private char sex;
public student() {
}
public student(String name, int age, char 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 char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
@Override
public String toString() {
return "student{" +
"name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
'}';
}
@Override
public int compareTo(student o) {
return this.age-o.age;
}
}
package com.peihj.map;
import java.util.*;
public class test2 {
public static void main(String[] args) {
Map<String, List<String>> data = new HashMap<>();
List<String> selects = new ArrayList<>();
Collections.addAll(selects,"A","C");
data.put("胡勇",selects);
System.out.println(data); // {胡勇=[A, C]}
List<String> selects1 = new ArrayList<>();
Collections.addAll(selects1,"A","b","c");
data.put("非衣",selects1);
System.out.println(data); // {非衣=[A, b, c], 胡勇=[A, C]}
}
}
参考
黑马程序员