集合框架(1)

集合和数组的区别:
1.数组是定长的,集合不限定长度
2.任意类型都有自己对应的数组数组中只能存放同种类型的数据,集合中只能存放引用类型的数据

Collection collection = new ArrayList<>();
//添加数据
collection.add("张三");
collection.add(new Date());
//基本数据类型自动进行装箱操作,转换成对应的包装类类型,添加的是Integer类型
collection.add(20);
collection.add("a");
/*---------------------------------------*/
//判断集合是否为空
Sysout.out.println(collection.isEmpty());

//集合存放元素的数量
System.out.println(collection.size);

//判断集合是否包含某个元素

System.out.println(collection.contain(a));

//移除删除某个元素

System.out.println(collection.remove(a));

//清空集合所有数据
collection.claer();
List list = new ArrayList();
//添加数据
list.add("哼哈二将");

//在指定位置
list.add(1,"cc");

//根据元素获取索引
int a = list.indexOf("ccc");
//根据索引获取元素
Object object = list.get(1);

//判断集合是否为空
System.out.println(list.isEmpty);

//集合存放元素的数量
System.out.println(list.size);

//判断集合是否包含某个元素
System.out.println(list.contain("a"));

//移除某个元素
System.out.println(list.remove("哼哈二将”));
System.out.println(list.remove("0”));

//清空所有数据
list.clear();
List<Object> list  = new listArray();
    list.add("aa");
    list.add("cc");
    list.add(20);
    //遍历
    for(Object a :list){
    System.out.println(a);
}
    for(i = 0; i<list.size;i++){
    Object object = list.get(i);
    System.out.println(object);
}
    Iterator iterator = list.iterator();
    while(iterator.hasNext){
    Object object = iterator.next();
    System.out.println(object);
}

    //重写toString
    public class Student {
    public String name;
    public int age;
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }
}
    //
    Set<String> set = new HashSet<>();
    Set.add("aa");
    Set.add("bb");
    Set.add("cc");
    Set.add("dd");

    for(String string :set){
    System.out.println(string);
}
    //迭代
    Iterator<String> iterator = new iterator();
    //hasNext()检查游标右侧的内容
    for(iterator.hasNext()){
    //next()记录检查的内容
    String string = iterator.next();
    System.out.println(string);
    //remove()删除游标左侧的内容
}

Map类

//Map   存放的是key---value  键值对数据
//K:key键值
//V:value
//无序可重复  ,key值是不能重复的 

HashMap<String, String> map = new HashMap<>();
        //存取数据
        map.put("name1", "张三");
        map.put("name2", "李四");
        map.put("name3", "王五");
        map.put("name4", "赵六");
        map.put("name5", "张三");

        //获取数据
        String name2 = map.get("name2");
        //删除
        map.remove("name4");

        //判断是否包含此key
        System.out.println(map.containsKey("name4"));
        //判断容器是否为空
        System.out.println(map.isEmpty());

        //清空集合里面的所有数据
         map.clear();


        System.out.println(name2);
        System.out.println(map);

        //获取所有vlaue值

        Collection<String> collection = map.values();
        //遍历
        for(String string :collection){
            System.out.println(string);

        }
                //遍历
                Set<String>  keys =map.keySet();
                for(String key :keys){
                    //System.out.println(key);
                    String value = map.get(key);
                    System.out.println(key+":"+value);
                }
                //获取到map中的一组一组数据
                Set<Entry<String,String>> set =map.entrySet();
                for(Entry<String,String> entry:set){
                    //key
                    String key = entry.getKey();
                    //value
                    String value = entry.getValue();

                    System.out.println(key+":"+value);

                }

猜你喜欢

转载自blog.csdn.net/L_K123/article/details/81139595
今日推荐