zoukankan      html  css  js  c++  java
  • 2019-05-28 Java学习日记 day18

    集合框架

    Map集合

      将键映射到值的对象

      一个映射不能包含重复的键

      每个键最多只能映射到一个值

    Map接口和Collection接口的不同

      map是双列的。collection的单列的

      map的键唯一,collection的子体系set是唯一的

      map集合的数据结构值针对键有效,跟值无关,collection集合的数据结构是针对元素有效

    map集合功能

    *添加功能:

      *V put(K key,V value):添加元素

        *如果键是第一个存储,就直接存储元素,返回null

        *如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值

    *删除功能

      *void clear():移除所有的键值对元素

      *V remove(object key):根据键删除键值对元素,并把值返回

    *判断功能

      *boolean containsKey(object key):判断集合是否包含指定的键

      *boolean containsValue(object value):判断集合是否包含指定的值

      *boolean isEmpty():判断集合是否为空

    *获取功能

      *Set<Map.Entry<K.V>> entrySet():

      *V get(object key):根据键获取值

      *set<K> keySet():获取集合中所有键的集合

      *Collection<V> values():获取集合中所有值的集合

    *长度功能

      *int size():返回集合中的键值对的个数  

    public class demo2_Iterator {
    
        public static void main(String[] args) {
            Map<String, Integer> map=new HashMap<>();
            map.put("张三", 23);
            map.put("李四", 24);
            map.put("六六", 26);
            
            Integer i1=map.get("张三"); //根据键获取值
            System.out.println(i1);
            
            //获取所有的键
            /*Set<String> keyset=map.keySet();  //获取所有键的集合
            Iterator<String> it1=keyset.iterator(); //获取迭代器
            while (it1.hasNext()) {                //判断集合中是个有元素
                String key=it1.next();            //获取每一个键
                Integer value =map.get(key);    //根据键卓取值
                System.out.println(key+","+value);
                
            }*/
            
            //使用增强for循环
            for(String key :map.keySet()){  //map.KeySet()是所有键的集合
                System.out.println(key+"="+map.get(key));
            }
        }
    
    }
    案例
    public class demo3_Iterator {
        /*Map集合的第二种迭代,根据键值对对象,获取获取键和值
         * 键值对对象找键和值的思路:
         *     获取所有键值对对象的集合
         *     遍历键值对对象的集合,获取到每一个键值对对象
         *     根据键值对对象找键和值
         * */
        public static void main(String[] args) {
            Map<String, Integer> map=new HashMap<>();
            map.put("张三", 23);
            map.put("李四", 24);
            map.put("六六", 26);
            
            //map.entry说明entry是map的内部接口,将键和值封装成了entry对象,并存储在set集合中
            //Set<Map.Entry<String, Integer>> en1=map.entrySet();
            
            /*//获取每一个对象
            Iterator<Map.Entry<String, Integer>> it1=en1.iterator();
            while (it1.hasNext()) {
            //获取每一个entry对象
                Map.Entry<String, Integer> en=it1.next();
                String key=en.getKey();  //根据键值对对象获取键
                Integer in1=en.getValue();    //根据键值对对象获取值
                System.out.println(key+"="+in1); 
            }*/
            for(Map.Entry<String, Integer> en:map.entrySet()){
                System.out.println(en.getKey()+"="+en.getValue());
            }
    
        }
    
    }

     

    统计字符串中每个字符出现的次数

    public class test1 {
    
        public static void main(String[] args) {
            //定义一个需要被统计字符的字符串
            String s="aabbccccccbdbshcbd";
            
            //将字符串转换为字符数组
            char[] arr=s.toCharArray();
            
            //定义双列集合,存储字符串中字符以及字符出现的次数
            HashMap<Character, Integer> hm1=new HashMap<>();
            
            //遍历字符数组获取内一个字符,并将字符存储在双列集合中
            for(char c:arr){
                //存储过郑重要做判断,如果集合不包含这个键,就将该字符当作键,值为1存储,如果集合中包含这个键,就将值加1存储
                /*if(!hm1.containsKey(c)){  //如果不包含这个键
                    hm1.put(c, 1);
                    
                }else {
                    hm1.put(c, hm1.get(c)+1);
                }*/
                hm1.put(c, !hm1.containsKey(c) ? 1: hm1.get(c)+1);
                       //如果不包含 就存1 ,包含就值+1存进去
            }
    
            //打印双列集合获取字符出现的次数
            for (Character key : hm1.keySet()) {     //hm1.keyset()代表所有键的集合
                System.out.println(key+ "="+hm1.get(key));    //hm1.get(key)根据键获取值
            }
        }
    
    }

    HashSet嵌套HashSet

    public class test2 {
    
        public static void main(String[] args) {
            HashMap<Student, String> hm1 =new HashMap<>();
            hm1.put(new Student("张三",23), "北京");
            hm1.put(new Student("李四",24), "上海");
            hm1.put(new Student("王五",23), "广州");
            hm1.put(new Student("六六",23), "深圳");
            
            
            HashMap<Student, String> hm2 =new HashMap<>();
            hm2.put(new Student("美丽",18), "马来西亚");
            hm2.put(new Student("漂亮",17), "印度尼西亚");
            hm2.put(new Student("beautiful",19), "广州");
            hm2.put(new Student("靓靓",20), "深圳");
            
            HashMap<HashMap<Student, String>,String> hm =new HashMap<>();
            hm.put(hm1, "第一期学生");
            hm.put(hm2, "第二期学生");
            
            //遍历双列集合
            for (HashMap<Student, String> h : hm.keySet()) {
                String value =hm.get(h);  //get(h)根据键对象获取值对象
                System.out.println(value);
                //遍历键的双列集合对象
                for(Student key : h.keySet()){  //h.keyset()获取集合总所有的学生键对象
                    String value2=h.get(key);
                
                    System.out.println(key+","+value2+","+value);
                    
                }
                
            }
        }
    
    }
    案例

     

    HashMap和Hashtable

    public class demo8_Hashtable {
    
            /*hashMap和Hashtable的区别
             * 共同点:
             *     底层都是哈希算法,都是双列集合
             * 区别:
             *     HashMap是线程不安全的,效率高,JDK1.2版本
             *     Hashtable是线程安全的,效率低,JDK1.0版本
             *  
             *  HashMap可以存储null键和null值
             *  Hashtable不可以存储null键和null值
             * */
        public static void main(String[] args) {
            HashMap<String, Integer> hm =new HashMap<>();
            hm.put(null, 23);
            hm.put("李四", null);
            System.out.println(hm);
            
            Hashtable<String, Integer> ht =new Hashtable<>();
            ht.put(null, 23);
            System.out.println(ht);
    
        }
    
    }

    Collection

    针对集合操作 的工具类

    Collecitons中的常见方法
    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)

    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Collections;
    
    public class demo1_Collection {
    
        public static void main(String[] args) {
            //demo1();
            //demo2();
            //demo3();
            //demo4();
            ArrayList<String> list = new ArrayList<>();
            list.add("a");
            list.add("b");
            list.add("c");
            list.add("d");
            list.add("f");
            list.add("g");
             Collections.shuffle(list);
             System.out.println(list);  //随机置换,可以洗牌
        }
    
        public static void demo4() {
            ArrayList<String> list = new ArrayList<>();
            list.add("a");
            list.add("b");
            list.add("c");
            list.add("d");
            list.add("f");
            list.add("g");
            Collections.reverse(list);
            System.out.println(list);  //反转字符串
        }
    
        public static void demo3() {
            ArrayList<String> list = new ArrayList<>();
            list.add("a");
            list.add("b");
            list.add("c");
            list.add("d");
            list.add("f");
            list.add("g");
            System.out.println(Collections.max(list)); //根据排序结果获取最大值
        }
    
        public static void demo2() {
            ArrayList<String> list = new ArrayList<>();
            list.add("a");
            list.add("b");
            list.add("c");
            list.add("d");
            list.add("f");
            list.add("g");
            System.out.println(Collections.binarySearch(list, "c")); //查找字符串位置
            System.out.println(Collections.binarySearch(list, "e"));
        }
    
        public static void demo1() {
            ArrayList<String> list = new ArrayList<>();
            list.add("c");
            list.add("d");
            list.add("d");
            list.add("b");
            list.add("a");
            list.add("d");
            
            System.out.println(list);
            Collections.sort(list);
            System.out.println(list);
        }
    
    }
    案例

    斗地主洗牌

    public class test3 {
    
        public static void main(String[] args) {
            String [] num= {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
            String [] color={"红桃","黑桃","方片","梅花"};
            ArrayList<String> poker = new ArrayList<>();
            
            //并接花色和数字
            for(String s1 :color){
                for(String s2 : num){
                    poker.add(s1.concat(s2));  //cancat连接两个字符串
                }
            }
            poker.add("小王");
            poker.add("大王");
            //洗牌
            Collections.shuffle(poker);
            //发牌
            ArrayList<String> tan = new ArrayList<>();
            ArrayList<String> jung = new ArrayList<>();
            ArrayList<String> jun = new ArrayList<>();
            ArrayList<String> dipai = new ArrayList<>();
            
            for (int i = 0; i < poker.size(); i++) {
                if ( i >=poker.size()-3) {
                    dipai.add(poker.get(i));  //将三张底牌存储在底牌集合中
                } else if(i % 3==0){
                    tan.add(poker.get(i));
                }else if(i % 3 ==1){
                    jung.add(poker.get(i));
                }else{
                    jun.add(poker.get(i));
                }
            }
            //看牌
            System.out.println(tan);
            System.out.println(jung);
            System.out.println(jun);
            System.out.println(dipai);
        }
    
    }
    import java.awt.List;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.TreeSet;
    
    import javax.swing.event.TreeSelectionEvent;
    
    public class test4 {
        public static void main (String args []){
            String [] num= {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
            String [] color={"红桃","黑桃","方片","梅花"};
            HashMap<Integer, String> hm =new HashMap<>(); //存储索引和扑克牌
            ArrayList<Integer> list = new ArrayList<>(); //存储索引
            int index =0;
            
            //拼接扑克牌并索引和扑克牌存储在hm中
            for(String s1: num){  //获取数字
                for(String s2 : color){ //获取颜色
                    hm.put(index, s2.concat(s1));
                    list.add(index);   //将索引0到51添加到list集合
                    index++;
                }
            }
            //将小王添加到双列集合中
            hm.put(index, "小王");
            list.add(index);  //将52索引添加到集合中
            index++;
            hm.put(index, "大王");
            list.add(index);    //将53索引添加到集合中
            
            //洗牌
            Collections.shuffle(list);
            
            //发牌
            TreeSet<Integer> tan = new TreeSet<>();
            TreeSet<Integer> jung = new TreeSet<>();
            TreeSet<Integer> jun = new TreeSet<>();
            TreeSet<Integer> bootom = new TreeSet<>();
            
            for (int i = 0; i < list.size(); i++) {
                if (i>=list.size() - 3) {
                    bootom.add(list.get(i));   //将三张底牌存储在底牌集合中
                }else if(i %3 ==0){
                    tan.add(list.get(i));
                }else if (i % 3 == 1) {
                    jung.add(list.get(i));
                }else {
                    jun.add(list.get(i));
                }
            }
            //看牌
            lookpoker(hm, tan, "谭某");
            lookpoker(hm, jung, "某人");
            lookpoker(hm, jun, "俊");
            lookpoker(hm, bootom, "底牌");
            
        }
        public static void lookpoker(HashMap<Integer, String> hm,TreeSet<Integer> ts,String name){
                System.out.print(name+"的牌是: ");
                for (Integer i :ts) {   //i代表双列集合中的每一个键
                    System.out.print(hm.get(i)+" ");
                }
                System.out.println();
            }
    }
    斗地主洗牌规则

     

    泛型固定下边界

    import java.util.ArrayList;
    import java.util.Comparator;
    import java.util.TreeSet;
    
    import tan.jung.test.BaseStudent;
    import tan.jung.test.Student;
            /* 泛型固定下边界
             *  ? super E
             * 泛型固定上边界
             *     ? extends E
             * */
    public class demo2_Genric {
    
        public static void main(String[] args) {
            //demo();
            TreeSet<Student> ts1 =new TreeSet<>(new CompareByAge());
            ts1.add(new Student("张三",23));
            ts1.add(new Student("里斯",18));
            ts1.add(new Student("王五",25));
            ts1.add(new Student("赵六",13));
            ts1.add(new Student("好奇",33));
            System.out.println(ts1);
            
            TreeSet<BaseStudent> ts2 =new TreeSet<>(new CompareByAge());
            ts2.add(new BaseStudent("张三",23));
            ts2.add(new BaseStudent("里斯",18));
            ts2.add(new BaseStudent("王五",25));
            ts2.add(new BaseStudent("赵六",13));
            ts2.add(new BaseStudent("好奇",33));
            System.out.println(ts2);
        }
    
        public static void demo() {
            ArrayList<Student> list1 =new ArrayList<>();
            list1.add(new Student("张三",23));
            list1.add(new Student("李四",24));
            
            ArrayList<BaseStudent> list2 =new ArrayList<>();
            list2.add(new BaseStudent("四四",23));
            list2.add(new BaseStudent("1四",24));
            
            list1.addAll(list2);
        }
    
    }
    class CompareByAge implements Comparator<Student>{
    
        @Override
        public int compare(Student s1, Student s2) {
        int num =s1.getAge() -s2.getAge();
            return num == 0 ? s1.getName().compareTo(s2.getName()) :num;
        }
        
    }
    案例
  • 相关阅读:
    优先队列
    Problem W UVA 662 二十三 Fast Food
    UVA 607 二十二 Scheduling Lectures
    UVA 590 二十一 Always on the run
    UVA 442 二十 Matrix Chain Multiplication
    UVA 437 十九 The Tower of Babylon
    UVA 10254 十八 The Priest Mathematician
    UVA 10453 十七 Make Palindrome
    UVA 10163 十六 Storage Keepers
    UVA 1252 十五 Twenty Questions
  • 原文地址:https://www.cnblogs.com/JungTan0113/p/10941492.html
Copyright © 2011-2022 走看看