Java之Collection和Collections

java.util接口 Collection<E>

所有通用的 Collection 实现类(通常通过它的一个子接口间接实现 Collection)应该提供两个“标准”构造方法:

  1. 一个是 void(无参数)构造方法,用于创建空 collection;
  2. 另一个是带有 Collection 类型单参数的构造方法,用于创建一个具有与其参数相同元素新的 collection。

常用的:

 booleanadd(E e)
          确保此 collection 包含指定的元素(可选操作)。
 voidclear()
          移除此 collection 中的所有元素(可选操作)。
 booleancontains(Object o)
          如果此 collection 包含指定的元素,则返回 true。
 booleanequals(Object o)
          比较此 collection 与指定对象是否相等。
 booleanisEmpty()
          如果此 collection 不包含元素,则返回 true。
 booleanremove(Object o)
          从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。
 intsize()
          返回此 collection 中的元素数。

 

java.util类 Collections

此类完全由在 collection 上进行操作或返回 collection 的静态方法组成。

 

<T> boolean
addAll(Collection<? super T> c, T... elements)
          将所有指定元素添加到指定 collection 中。
static
<T> int
binarySearch(List<? extends Comparable<? super T>> list, T key)
          使用二分搜索法搜索指定列表,以获得指定对象。
static
<T> void
copy(List<? super T> dest, List<? extends T> src)
          将所有元素从一个列表复制到另一个列表。
static booleandisjoint(Collection<?> c1, Collection<?> c2)
          如果两个指定 collection 中没有相同的元素,则返回 true。
static
<T> void
fill(List<? super T> list, T obj)
          使用指定元素替换指定列表中的所有元素。
static intfrequency(Collection<?> c, Object o)
          返回指定 collection 中等于指定对象的元素数。
static intindexOfSubList(List<?> source, List<?> target)
          返回指定源列表中第一次出现指定目标列表的起始位置;如果没有出现这样的列表,则返回 -1。
static intlastIndexOfSubList(List<?> source, List<?> target)
          返回指定源列表中最后一次出现指定目标列表的起始位置;如果没有出现这样的列表,则返回 -1。
static
<T> ArrayList<T>
list(Enumeration<T> e)
          返回一个数组列表,它按返回顺序包含指定枚举返回的元素。
static
<T extends Object & Comparable<? super T>>
T
max(Collection<? extends T> coll)
          根据元素的自然顺序,返回给定 collection 的最大元素。
static
<T extends Object & Comparable<? super T>>
T
min(Collection<? extends T> coll)
          根据元素的自然顺序 返回给定 collection 的最小元素。
static
<T> boolean
replaceAll(List<T> list, T oldVal, T newVal)
          使用另一个值替换列表中出现的所有某一指定值。
static voidreverse(List<?> list)
          反转指定列表中元素的顺序。
static voidrotate(List<?> list, int distance)
          根据指定的距离轮换指定列表中的元素。
static
<T extends Comparable<? super T>>
void
sort(List<T> list)
          根据元素的自然顺序 对指定列表按升序进行排序。
static voidswap(List<?> list, int i, int j)
          在指定列表的指定位置处交换元素。
static
<T> List<T>
synchronizedList(List<T> list)
          返回指定列表支持的同步(线程安全的)列表。

下面写一段代码,运用一些上面的方法 

import java.util.ArrayList;
import java.util.Collections;

public class CollectionsDemo {
	public static void main(String[] args) {
		ArrayList<Integer> list = new ArrayList<>();
		for (int i = 0; i < 20; i++) {// 从0到19
			list.add(i);
		}
		System.out.println(list);
		// 找有没有15,并返回位置(从0开始数)
		System.out.println(Collections.binarySearch(list, 15));
		// 如果没有找到15,就会打印-21, 21 - 1为索引
		Collections.reverse(list);// 反转
		System.out.println(list);
		Collections.fill(list, 1);
		System.out.println(list);
	}
}

end. 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值