Java学习DAY11

第十章 集合2

1.Set

在这里插入图片描述

public static void main(String[] args)  {
        Set<String> c =new HashSet<String>();
        c.add("hello");
        c.add("world");
        c.add("java") ;

        for(String s:c){
            System.out.println(s);
        }
    }
结果:
world
java
hello

Process finished with exit code 0
2.HashSet

在这里插入图片描述
案例:
在这里插入图片描述

public class Test{
    public static void main(String[] args) throws ParseException {
        HashSet<Student> c =new HashSet<Student>();

        Student h1=new Student("Lily",18);
        Student h2=new Student("Bob",19);
        Student h3=new Student("Jack",20);
        c.add(h1);
        c.add(h2);
        c.add(h3);

        for(Student s:c){
            System.out.println(s.getName()+"----"+s.getAge());
        }

        Iterator<Student> it =c.iterator();
        while(it.hasNext()){
            Student s= it.next();
            System.out.println(s.getName()+"------"+s.getAge());
        }
    }
}
3.Map集合

在这里插入图片描述
成员方法及获取功能:

public class Test{
    public static void main(String[] args) throws ParseException {
        Map<String,String> c =new HashMap<String,String>();

        c.put("001","lily");
        c.put("002","bob");
        c.put("003","jack");//添加元素
        System.out.println(c);

        Set<String> set= c.keySet();//获取所有键的集合
        for(String key:set){
            System.out.println(key);
        }

        Collection<String> s=c.values();//获取所有值的集合
        for(String value:s){
            System.out.println(value);
        }

        c.remove("001");//删除元素
        System.out.println(c);

        System.out.println(c.get("002"));//根据键获取值

        System.out.println(c.containsKey("001"));//是否包含某键,这里输出false
        System.out.println(c.containsValue("lily"));//是否包含某值,这里输出false

        System.out.println(c.size());//输出键值对个数
        c.clear();//清空
        System.out.println(c.isEmpty());//判空

    }
}

遍历方法:(1)根据键找值
在这里插入图片描述

public static void main(String[] args) throws ParseException {
        Map<String,String> c = new HashMap<>();

        c.put("001","lily");
        c.put("002","bob");
        c.put("003","jack");//添加元素
        System.out.println(c);

        Set<String> set= c.keySet();//获取所有键的集合
        for(String key:set){
            String value=c.get(key);
            System.out.println(key+"---"+value);
        }


    }

在这里插入图片描述

Set<Map.Entry<String,String>> set= c.entrySet();//获取所有键值对象的集合
        for(Map.Entry<String,String> me:set){
            String key= me.getKey();
            String value= me.getValue();;
            System.out.println(key+"---"+value);
        }
4.HashMap

在这里插入图片描述

public class Test{
    public static void main(String[] args) throws ParseException {
        HashMap<String,Student> c = new HashMap<String, Student>();
        Student h1=new Student("lily",18);
        Student h2=new Student("bob",19);
        Student h3=new Student("jack",20);

        c.put("001",h1);
        c.put("002",h2);
        c.put("003",h3);//添加元素
        //System.out.println(c);

        Set<String> set1= c.keySet();//获取所有键的集合
        for(String key:set1){
            Student value= c.get(key);;
            System.out.println(key+"---"+value.getName()+"---"+value.getAge());
        }

        Set<Map.Entry<String,Student>> set= c.entrySet();//获取所有键值对象的集合
        for(Map.Entry<String,Student> me:set){
            String key= me.getKey();
            Student value= me.getValue();;
            System.out.println(key+"---"+value.getName()+"-----"+value.getAge());
        }


    }
}

在这里插入图片描述

public class Test{
    public static void main(String[] args) throws ParseException {
        HashMap<Student,String> c = new HashMap<Student, String>();
        Student h1=new Student("lily",18);
        Student h2=new Student("bob",19);
        Student h3=new Student("jack",20);
        Student h4=new Student("jack",20);

        c.put(h1,西安);
        c.put(h2,上海);
        c.put(h3,北京);
        c.put(h4,香港);//添加元素
        //System.out.println(c);

        Set<Student> set1= c.keySet();//获取所有键的集合
        for(Student key:set1){
            String value= c.get(key);;
            System.out.println(key.getName()+"---"+key.getAge()+"-----"+value);
        }
    }
}
5.集合的嵌套练习

在这里插入图片描述

public class Test{
    public static void main(String[] args) throws ParseException {
        ArrayList<HashMap<String,String>> c = new ArrayList<HashMap<String,String>>();

        HashMap<String,String> h1=new HashMap<String,String>();
        h1.put("lily","18");
        h1.put("betty","19");
        c.add(h1);
        HashMap<String,String> h2=new HashMap<String,String>();
        h2.put("jack","20");
        h2.put("jason","19");
        c.add(h2);

        HashMap<String,String> h3=new HashMap<String,String>();
        h3.put("remi","20");
        h3.put("lucy","19");
        c.add(h3);

        for(HashMap<String,String> hm:c){
            Set<String> set= hm.keySet();
            for(String key:set){
                String value =hm.get(key);
                System.out.println(key+"-----"+value);
            }
            System.out.println("----------------------------");

        }
    }
}

在这里插入图片描述

public class Test{
    public static void main(String[] args) throws ParseException {
        HashMap<String,ArrayList<String>> c = new HashMap<String,ArrayList<String>>();

        ArrayList<String> h1=new ArrayList<String>();
        h1.add("lily");
        h1.add("betty");
        c.put("001",h1);

        ArrayList<String> h2=new ArrayList<String>();
        h2.add("jack");
        h2.add("jason");
        c.put("002",h2);
        
        ArrayList<String> h3=new ArrayList<String>();
        h3.add("remi");
        h3.add("lucy");
        c.put("003",h3);
        

        Set<String> set1= c.keySet();//获取所有键的集合
        for(String key:set1){
            System.out.println(key);
            ArrayList<String> value= c.get(key);
            for(String s:value){
                System.out.println(s);

            }
            System.out.println("------------------");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值