java.util.collections引入布料,Collections工具类-java.util.Collections

Collections包含有各种有关集合操作的静态多态方法。此类不能实例化,就像一个工具类,服务于Java的Collection框架。

addAll(集合,值1,值2,值3...)

将一些元素添加到一个List中,此时我们一般有两种选择:Collections.addAll()或者是ArrayList.addAll()。在需添加元素比较少的情况下,并在List的size在万级以上时,一般建议Collections.addAll(),但当List的size较小时,两种方法没有什么区别,甚至ArrayList.addAll()更好。

@SafeVarargs

public static boolean addAll(Collection super T> c, T... elements) {

boolean result = false;

for (T element : elements)

result |= c.add(element);

return result;

}

shuffle(List);

使用默认随机源对列表进行置换,所有置换发生的可能性都是大致相等的

public static void shuffle(List> list) {

Random rnd = r;

if (rnd == null)

r = rnd = new Random(); // harmless race.

shuffle(list, rnd);

}

sort(List);

sort(List,比较器);

Collections中

@SuppressWarnings({"unchecked", "rawtypes"})

public static void sort(List list, Comparator super T> c) {

list.sort(c);

}

Lists中

@SuppressWarnings({"unchecked", "rawtypes"})

default void sort(Comparator super E> c) {

Object[] a = this.toArray();

Arrays.sort(a, (Comparator) c);

ListIterator i = this.listIterator();

for (Object e : a) {

i.next();

i.set((E) e);

}

}

@SuppressWarnings({"unchecked", "rawtypes"})

public static void sort(List list, Comparator super T> c) {

list.sort(c);

}

binarySearch(List,目标值);

binarySearch(List,目标值,比较器);

此方法传入一个实现了Comparable接口的对象类的列表和要查找的元素。

int binarySearch(List extends Comparable super T>> list, T key) {

if (list instanceof RandomAccess || list.size()

return Collections.indexedBinarySearch(list, key);

else

return Collections.iteratorBinarySearch(list, key);

}

private static

int indexedBinarySearch(List extends Comparable super T>> list, T key) {

int low = 0;

int high = list.size()-1;

while (low <= high) {

int mid = (low + high) >>> 1;

Comparable super T> midVal = list.get(mid);

int cmp = midVal.compareTo(key);

if (cmp < 0)

low = mid + 1;

else if (cmp > 0)

high = mid - 1;

else

return mid; // key found

}

return -(low + 1); // key not found

}

@SuppressWarnings("unchecked")

public static int binarySearch(List extends T> list, T key, Comparator super T> c) {

if (c==null)

return binarySearch((List extends Comparable super T>>) list, key);

if (list instanceof RandomAccess || list.size()

return Collections.indexedBinarySearch(list, key, c);

else

return Collections.iteratorBinarySearch(list, key, c);

}

private static int indexedBinarySearch(List extends T> l, T key, Comparator super T> c) {

int low = 0;

int high = l.size()-1;

while (low <= high) {

int mid = (low + high) >>> 1;

T midVal = l.get(mid);

int cmp = c.compare(midVal, key);

if (cmp < 0)

low = mid + 1;

else if (cmp > 0)

high = mid - 1;

else

return mid; // key found

}

return -(low + 1); // key not found

}

private static int iteratorBinarySearch(List extends T> l, T key, Comparator super T> c) {

int low = 0;

int high = l.size()-1;

ListIterator extends T> i = l.listIterator();

while (low <= high) {

int mid = (low + high) >>> 1;

T midVal = get(i, mid);

int cmp = c.compare(midVal, key);

if (cmp < 0)

low = mid + 1;

else if (cmp > 0)

high = mid - 1;

else

return mid; // key found

}

return -(low + 1); // key not found

}

Collections.swap(List,int,int);

交换集合两个元素的位置

@SuppressWarnings({"rawtypes", "unchecked"})

public static void swap(List> list, int i, int j) {

// instead of using a raw type here, it's possible to capture

// the wildcard but it will require a call to a supplementary

// private method

final List l = list;

l.set(i, l.set(j, l.get(i)));

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值