



- package day01;
-
-
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.List;
-
- public class Test03 {
-
- public static void main(String[] args) {
- LinkedList<String> queue = new LinkedList<>();
- queue.add("第1个");
- queue.add("第2个");
- queue.add("第3个");
- queue.add("第4个");
- System.out.println("输出队列,先进先出");
- System.out.println(queue.removeFirst());
- System.out.println(queue.removeFirst());
- System.out.println(queue.removeFirst());
- System.out.println(queue.removeFirst());
- System.out.println("-------------------------------");
- System.out.println("栈输出,后进后出");
- LinkedList<String> stack = new LinkedList<>();
- // stack.add("第1个");
- // stack.add("第2个");
- // stack.add("第3个");
- // stack.add("第4个");
- // System.out.println(stack.removeLast());
- // System.out.println(stack.removeLast());
- // System.out.println(stack);
- stack.push("第1个");
- stack.push("第2个");
- System.out.println(stack.pop());
- System.out.println(stack);
-
- }
- }

















- package day01;
-
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.TreeMap;
-
- public class Mapday1 {
-
- public static void main(String[] args) {
-
- /*
- HashMap 无序 不重复,会覆盖前面 无索引
- */
- Map<String, Integer> map = new HashMap<String, Integer>();
- map.put("wwx", 1);
- map.put("wwx", 4);
- map.put("wwx2", 2);
- map.put("wwx3", 3);
- System.out.println(map);
- /*
- LinkedHashMap 有序 不重复,会覆盖前面 无索引
- */
- LinkedHashMap<String, String> lkMap = new LinkedHashMap<>();
- lkMap.put("wwx0", "000");
- lkMap.put("wwx1", "111");
- lkMap.put("wwx1", "1112");
- lkMap.put("wwx2", "222");
- System.out.println(lkMap);
- /*
- TreeMap<> 有序 不重复 无索引
- */
- TreeMap<String, String> strMap = new TreeMap<>();
- strMap.put("wwx0", "000");
- strMap.put("wwx1", "111");
- strMap.put("wwx1", "1112");
- strMap.put("wwx2", "222");
- System.out.println(strMap);
- }
- }

- package day01;
-
- import java.util.*;
-
- public class Mapday1 {
-
- public static void main(String[] args) {
-
- /*
- HashMap 无序 不重复,会覆盖前面 无索引
- */
- Map<String, Integer> map = new HashMap<>();
- map.put("wwx1", 100);
- map.put("wwx2", 2);
- map.put("wwx3", 3);
- System.out.println(map);
- //返回集合大小
- System.out.println(map.size());
- //清空键值对表
- //map.clear();
- //会判断键值对表 是否为空,为空ture,否则反之
- // System.out.println(map.isEmpty());
- int v1 = map.get("wwx1");
- System.out.println(v1);
- //获取全部键集合 Set集合接收 set不可重复,所以不会重复接收
- Set<String> strings = map.keySet();
- System.out.println(strings);
- //获取Map集合全部值 Collection集合接收
- Collection<Integer> values = map.values();
- System.out.println(values);
- /*
- putAll()方法
-
- */
- System.out.println("-------------------------");
- Map<String ,Integer> map1=new HashMap<>();
- map1.put("put1",111);
- map1.put("put2",222);
- System.out.println(map1);
- Map<String ,Integer> map2=new HashMap<>();
- map2.put("put3",3);
- map2.put("put4",4);
- map1.putAll(map2);
- System.out.println(map2);
- System.out.println(map1);
- System.out.println(map2);
-
- }
- }

- package day01;
-
- import java.util.*;
-
- public class Mapday1 {
-
- public static void main(String[] args) {
-
- /*
- HashMap 无序 不重复,会覆盖前面 无索引
- */
- System.out.println("--------------------");
- Map<String, Integer> map = new HashMap<>();
- map.put("wwx1", 1);
- map.put("wwx2", 2);
- map.put("wwx3", 3);
- Set<String> keySets = map.keySet();
- System.out.println(keySets);
- for (String keySet : keySets) {
- Integer integer = map.get(keySet);
- System.out.println(keySet + "-->>" + integer);
- }
- System.out.println("--------------------");
-
-
- }
- }

- package day01;
-
- import java.util.*;
-
- public class Mapday1 {
-
- public static void main(String[] args) {
- //把80个学生选择的景点数据拿到程序中来
- ArrayList<String> data = new ArrayList<>();
- //假设ABCD为4个景点
- String[] select = {"A", "B", "C", "D"};
-
- Random r = new Random();
-
- for (int i = 0; i < 80; i++) {
- //模拟一个学生选择一个景点,存到集合中去
- int index = r.nextInt(4);//0123,包左不包右
- data.add(select[index]);
- }
- System.out.println(data);
-
- HashMap<String, Integer> result = new HashMap<>();
-
- //遍历80个景点数据
- for (String s : data) {
- //问问Map集合中是否存在该景点
- if (result.containsKey(s)) {
- //说明这个景点之前统计过,其值+1,存入到Map集合中去
- result.put(s, result.get(s) + 1);
- } else {
- //说明这个景点是第一次统计
- result.put(s, 1);
- }
- }
- System.out.println(result);
- }
- }






此时TreeMap,无法识别类型无法排序
1.让类实现Comparable接口重写比较规则:

快捷键alt+insert,选择equals() and hashCode(),一直next



- package day02;
-
- import java.util.*;
-
- public class MapTest {
- public static void main(String[] args) {
- //创建一个嵌套集合list集合的map集合,值为list集合,
- Map<String, List<String>> map = new HashMap<>();
- //创建一个ArrayList集合来当作map集合的值
- ArrayList<String> cities = new ArrayList<>();
- //调用Collections.addAll(list集合,”数据“,”数据“,···)批量添加数据
- Collections.addAll(cities, "厦门", "福州", "泉州", "漳州", "龙岩");
- //map.put(键,值)方法,给map集合赋值
- map.put("福建省", cities);
- System.out.println(map);
-
- //map.get(键),该方法可获得该键的值
- List<String> fjCities = map.get("福建省");
- //增强for遍历
- for (String fjCity : fjCities) {
- System.out.println(fjCity);
- }
- System.out.println("-------------------------");
- //lambda遍历
- map.forEach((p,c)->{
- System.out.println(p+"->"+c);
- });
- }
- }
-
