目录
集合
在java基础中,接触的集合可大致分为以下几个类别

Collection集合
集合概述
前言:之所以称Collection为单列集合是因为对比Map集合,每一个元素是单独存在的,不存在配对关系
添加元素
 Collection<String> c=new ArrayList<String>();
        //boolean add(E e):添加元素
        System.out.println(c.add("hello"));
        System.out.println(c.add("world")); 
移除元素
  Collection<String> c=new ArrayList<String>();
        c.add("hello");
        c.add("world");
        c.add("java");
        //boolean remove(Object o):移除指定元素
        System.out.println(c.remove("world")); 
判断集合存在指定元素
  Collection<String> c=new ArrayList<String>();
        c.add("hello");
        c.add("world");
        c.add("java");
     
        //boolean contains(Object o):判断集合中是否存在指定元素
        System.out.println(c.contains("world")); 
清空集合元素
Collection<String> c=new ArrayList<String>();        
        c.add("hello");
        //void clear():清空集合中的元素
        c.clear();
 
判断是否存在元素
  Collection<String> c=new ArrayList<String>();
        c.add("hello");
        //boolean isEmpty():判断集合是否为空
        System.out.println(c.isEmpty()); 
集合长度
        //int size():集合长度,集合中元素的个数
        System.out.println(c.size()); 
遍历集合
public class Collection遍历元素 {
    public static void main(String[] args) {
        Collection<String> c=new ArrayList<String>();
        c.add("hello");
        c.add("world");
        c.add("java");
        //迭代器
        Iterator<String> it = c.iterator();
        //it.hasNext表示判断集合中是否有元素
        //it.next()表示输出元素
        while(it.hasNext()){
            String s = it.next();
            System.out.println(s);
        }
    }
} 
集合方法
顺序排列
Collection.sort(list);
反转顺序
Collection.reverse(list);
随机排列
Collections.shuffle(list);
Map集合
前言:Map集合被称为双列集合,因为每一个元素中由包含了两个部分,键以及键值,这个原理像高中时的映射。比如元素A中包括了 键a 和值b。其中a作为b的键,操作时在控制台输入a的索引即可访问到b的值
集合概述
根据键删除值,返回键(控制台删除赵敏,返回值“赵敏”一样会输出在控制台)
Map<String,String> map=new HashMap<String, String>();
      map.put("张无忌","赵敏");
      map.put("郭靖","杨蓉");
      map.put("杨过","小龙女");
       //V remove(Object key):根据键删除指定元素,返回删除值
      System.out.println(map.remove("张无忌")); 
清空集合
map.clear();
判断集合是否存在指定值(返回布尔值)
Map<String,String> map=new HashMap<String, String>();
        map.put("张无忌","赵敏");
        map.put("郭靖","杨蓉");
        map.put("杨过","小龙女");
        //boolean containKey(Object key):判断集合是否包含指定的键
        System.out.println(map.containsKey("郭靖")); 
集合是否为空
boolean isEmpty():判断集合是否为空
遍历集合
获取值
Map<String,String> map=new HashMap<String, String>();
        map.put("张无忌","赵敏");
        map.put("郭靖","杨蓉");
        map.put("杨过","小龙女");
        //V get(Object key):根据键获取值
        System.out.println(map.get("张无忌")); 
获取键
 Map<String,String> map=new HashMap<String, String>();
        map.put("张无忌","赵敏");
        map.put("郭靖","杨蓉");
        map.put("杨过","小龙女");
        //Set<K> keySet():获取所有键的集合
          Set<String> keySet=map.keySet();
          for(String key:keySet){
              System.out.println(key);
          } 
获取键和值
Map<String,String> map=new HashMap<String, String>();
        map.put("张无忌","赵敏");
        map.put("郭靖","杨蓉");
        map.put("杨过","小龙女");
     
        //Collection<V> values():获取所有值的集合
        Collection<String> values=map.values();
        for(String value:values){
            System.out.println(value);
        } 










