美文网首页
(必须会)经常用的List和Map在代码中常用的

(必须会)经常用的List和Map在代码中常用的

作者: 蛋炒饭_By | 来源:发表于2018-01-12 11:31 被阅读13次

    Map

    package com.company;
    
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * Created by ttc on 2018/1/10.
     */
    public class MapReview {
        public static void main(String[] args) {
            //创建map对象
            //key-value
            Map<String,String> words = new HashMap<>();
    
            //增删改查
            //增
    
            words.put("dog","狗");//2个参数,第一个是key,第二个是value
            words.put("cat","猫");
    
            //遍历map
            for(String key : words.keySet())
            {
                System.out.println(key + "----" + words.get(key));
            }
    
            //删
            String str = words.remove("dog");
            System.out.println(str + "被删除了");
    
            //遍历map
            for(String key : words.keySet())
            {
                System.out.println(key + "----" + words.get(key));
            }
    
            //改
            words.put("cat","小猫");
            //遍历map
            for(String key : words.keySet())
            {
                System.out.println(key + "----" + words.get(key));
            }
    
            //判断map中是否包含某个key
            if(words.containsKey("cat"))
            {
                System.out.println("包含猫");
            }
            else
            {
                System.out.println("不包含猫");
            }
    
            //遍历map的第2种方法
            for(Map.Entry entry : words.entrySet())
            {
                System.out.println(entry.getKey() + "----" + entry.getValue());
            }
    
        }
    }
    

    List独立

    package collection;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    /**
     * 集合里放字符串
     */
    public class ListDemo
    {
        public static void main(String[] args)
        {
            //List是个接口,定义实现这个接口的类ArrayList
            List<String> listDog=new ArrayList();
            Scanner scanner=new Scanner(System.in);
            while (true)
            {
                String dogName=scanner.next();
                if (dogName.equals("no"))
                {
                    break;
                }
                if(!listDog.contains("ergouzi"))
                {
                    listDog.add(dogName);
                }
                //listDog.add(dogName);
                //listDog.contain()   ;    listDog.remove(1), listDog.remove("abc")
            }
            listDog.remove(1);
    //        for(int i=0;i<listDog.size();i++)
    //        {
    //            System.out.println(listDog.get(i));
    //        }
            //也可以用增强for循环
                for(String dogName1:listDog)
                {
                    System.out.println(dogName1);
                }
    
        }
    }
    

    --------------------------------------------------------

    ArrayList

    package array;
    import java.util.ArrayList;
    import java.util.List;
    
    public class ListReview {
        public static void main(String[] args) {
    
            List<String> stringList = new ArrayList<>();
            stringList.add("zs");
            stringList.add("ls");
            stringList.remove("ls");
            if (stringList.contains("zs")) {
                System.out.println("yes");
            } else {
                System.out.println("no");
            }
            stringList.set(0, "ww");
            stringList.add("ww");
            for (String string : stringList) {
                System.out.println(string);
            }
        }
    }
    

    MapReview

    public class MapReview {
        public static void main(String[] args) {
            Map<String ,String>words =new HashMap<>();
            words.put("dog","狗 ");
            words.put("cat","猫");
          String str =  words.remove("dog" );
            System.out.println(str+"666");
    
            words.put("cat","小猫");
    
            for (String key:words.keySet())
            {
                System.out.println(key+"----"+words.get(key));
            }
    
            if (words.containsKey("cat"))
            {
                System.out.println("包含");
            }
            else {
                System.out.println("不包含");
            }
    
            for (Map.Entry entry:words.entrySet())
            {
                System.out.println(entry.getKey()+"----"+entry.getValue());
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:(必须会)经常用的List和Map在代码中常用的

          本文链接:https://www.haomeiwen.com/subject/xazxoxtx.html