介绍一下集合框架(集合框架体系Collection和Map常用API【汇总】)
导读:集合框架体系Collection和Map常用API【汇总】 Collection公共的方法 Collection是单列结合的顶层接口,它的方法是所有单列集合都可以继承使用的。...
集合框架体系Collection和Map常用API【汇总】
Collection公共的方法
Collection是单列结合的顶层接口,它的方法是所有单列集合都可以继承使用的。 //把给定元素添加到集合中 public boolean add(E e) //把给定元素从集合中删除 public boolean remove(E e) //清空集合中的所有元素 public void clear() //判断集合中是否包含给定对象 public boolean contains(Object obj) //判断集合是否为空 public boolean isEmpty() //返回集合中的长度 public int size()注意:
coll.remove(E e):Collection定义的是所有子类共有的方法,Set没有索引,所以remove方法的参数是元素。
coll.contains(Object object):Collection中contains方法底层是用object.equals()来判断元素是否相等的,所以比较的是地址值。当自定义对象类型的集合使用此方法时,需要重写equals方法。
List特有的方法
List继承了Collection接口的方法 List集合因为有索引,所以定义了很多索引操作方法 //增:根据索引插入指定元素 public void add(int index,E e) //删:删除指定索引处的元素,并返回被删除元素 public E remove(int index) //改:修改指定索引处的元素,并返回被修改的元素 public E set(int index,E e) //返回指定索引处的元素 public E get(int index)注意:
list.remove(int index)是List接口重载Collection中remove的方法,功能为删除指定索引处的元素。当list中的元素为Integer类型时,要注意以下细节: public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.remove(1);// int index 删除索引为1的元素 list.remove(Integer.valueOf(1));// Object object 删除元素为1的元素 System.out.println(list); }Set特有的方法
Set继承了Collection接口的方法
Set的常用方法与Collection基本一致
Map公共的方法
//添加键值对元素,并返回添加之前的值(若已存在键,则覆盖键值对元素) public V put(K key,V value) //根据键删除键值对元素,并返回被删除的值 public V remove(Object key) //清空所有键值对元素 public void clear() //判断集合是否包含指定的键 public boolean containsKey(Object key) //判断集合是否包含指定的值 public boolean containsValue(Object value) //判断集合是否为空 public boolean isEmpty() //返回集合的长度 public int size()Collections工具类
java.util.Collections:是集合的工具类
常用方法(省略泛型):
//批量添加元素 public static boolean addAll(Collection coll, T... elements) //随机打乱元素 public static void shuffle(List list) //根据指定规则排序(可传比较器) public static void sort(List list) //二分查找指定元素 public static int binarySearch(List list, T key) //拷贝集合中的元素 public static void copy(List dest, List src) //以指定元素填充集合 public static int fill(List list, T object) //返回集合中的最大/最小值 public static T max/min(Collection coll) //交换集合中指定位置的元素 public static void swap(List list, int i, int j) //反转集合 public static void reverse(List list)Collection与Map的遍历方式
Collection public static void main(String[] args) { Collection<String> coll = new ArrayList<>(); coll.add("aaa"); coll.add("bbb"); coll.add("ccc"); // 迭代器 Iterator<String> it = coll.iterator(); while (it.hasNext()) { String s = it.next(); System.out.println(s); } // 增强for for (String s : coll) { System.out.println(s); } // Lambda表达式 coll.forEach(s -> System.out.println(s)); //以下为List特有的遍历方式 List<String> list = new ArrayList<>(); list.add("aaa"); list.add("bbb"); list.add("ccc"); // 普通for循环 for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } // 列表迭代器遍历 ListIterator<String> itList = list.listIterator(); while (itList.hasNext()) { String s = itList.next(); System.out.println(s); } } Map public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("k1", "v1"); map.put("k2", "v2"); map.put("k3", "v3"); //键找值 Set<String> set = map.keySet(); for (String key : set) { System.out.println(map.get(key)); } //键值对 Set<Map.Entry<String, String>> entries = map.entrySet(); for (Map.Entry<String, String> entry : entries) { System.out.println("key:" + entry.getKey()); System.out.println("value:" + entry.getValue()); } //lambda表达式 map.forEach((key, value) -> { System.out.println("key:" + key); System.out.println("value:" + value); }); }随着学习与应用的深入,后期可能会有更新
本文作者:CodingOrange
本文链接:https://www.cnblogs.com/CodingOrange/p/17075280.html
转载请注明出处!创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!