过来集合啦,Java的小伙伴们

目录

1.集合框架

2.集合框架的优点及作用

3 Collection 接口

 3.2Collection接口引用顺序表

 3.3Map 接口


1.集合框架

Java 集合框架 Java Collection Framework ,又被称为容器 container ,是定义在 java.util 包下的一组 接口 interfaces 和其实现类 classes
其主要表现为将多个元素 element 置于一个单元中,用于对这些元素进行快速、便捷的存储 store 、检索 retrieve 、管理 manipulate ,即平时我们俗称的增删查改 CRUD

2.集合框架的优点及作用

使用成熟的集合框架,有助与我们便捷、快速的写出高效、稳定的代码

学习背后的数据结构知识,有助于我们理解各个集合的优点及使用场景

Collection 接口

3.1方法

Collection接口是用来存储管理一组对象objects , 这些对象一般被称为元素 elements

常用方法

 3.2Collection接口引用顺序表

在没有指定是什么类 类型时,顺序表中的元素可以随意添加

import java.util.*;

public class Interfaces {

    public static void main(String[] args) {
        //接口可以引用的类
        Collection collection = new ArrayList();
        //没有指定类型可以随意添加元素
        collection.add("fly");
        collection.add("11");
        collection.add("时间");
        System.out.println(collection);



    }

}

  

 <E>是泛型,可以指定是某个特定的类 类型,不能是基本类型

下面我们来看 String 类和  Integer 类

import java.util.*;

public class Interfaces {

    public static void main(String[] args) {
       //String 类只能添加字符或者字符串,不能添加数字或者其它,否则会报错
        Collection<String> collection = new ArrayList<String>();
        collection.add("fly");
        collection.add("就等我");
        System.out.println(collection);

       //Integer 类只能添加数字,不能添加字符或者字符串等其它,否则会报错
        Collection<Integer> collection1 = new ArrayList<Integer>();
        collection1.add(12);
        collection1.add(23);
        System.out.println(collection1);

    }

}

  

size()方法和isEmpty()的使用
import java.util.*;

public class Interfaces {

    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<String>();
        collection.add("fly");
        collection.add("hello");
        int n = collection.size();//集合中元素个数
        System.out.println(collection);
        System.out.println("是否是空集:"+collection.isEmpty());//判断集合是否没有元素,
        System.out.println("元素个数:"+n);

        //转换成数组输出
        Object[] objects = collection.toArray();
        System.out.println("字符串输出: "+Arrays.toString(objects));
    }

}

   

 3.3Map 接口

Map<k,v>接口类型,k 、v 这两个类类型可以自己指定
import java.util.*;

public class Interfaces {

    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        map.put("路过","张嘉佳");
        map.put("时间","2018");
        System.out.println(map);
    }

}

  

 v get(object k),指定k查找对应的v

import java.util.*;

public class Interfaces {

    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        map.put("路过","张嘉佳");
        map.put("时间","2018");

        String ret = map.get("路过");
        String ret2 = map.get("时间");
        System.out.println(ret);
        System.out.println(ret2);
    }

}

 

//v getOrDefault(object k, v defaultValue);指定k查找对应的v,没有找到用默认值代替
import java.util.*;

public class Interfaces {

    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        map.put("路过","张嘉佳");
        map.put("时间","2018");

        String ret3 = map.getOrDefault("作者","fly");//没有找到,用fly代替
        String ret4 = map.getOrDefault("时间","fly");//找到则输出对应时间
        System.out.println(ret3);
        System.out.println(ret4);
    }

}

猜你喜欢

转载自blog.csdn.net/m0_60494863/article/details/121663825