collections的基本方法介绍

collections的基本方法

package com.collections;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

public class CollectionsTest {
	List<Integer> list = new ArrayList<>();
	@Before
	public void init() {
		list.add(123);
		list.add(456);
		list.add(12);
		list.add(78);
		list.add(456);
		System.out.println(list);
	}
	
	// 同步控制
	@Test
	public void testSyncronized() {
		//synchronizedCollection(Collection<T> c) 返回指定线程安全的 Collection
		Collection<Integer> c = new ArrayList<>();
		Collection<Integer> cc = Collections.synchronizedCollection(c);
		cc.add(1);
		cc.add(2);
		System.out.println(cc);
		
		//synchronizedList(List<T> list) 返回指定线程安全的 List
		List<Integer> synList = Collections.synchronizedList(new ArrayList<Integer>());
		synList.add(1);
		synList.add(2);
		synList.add(3);
		System.out.println(synList);
		
		//synchronizedMap(Map<K,V> m) 返回由指定线程安全的 Map
		Map<String, String> hm = Collections.synchronizedMap(new HashMap<>());
		hm.put("a", "aaa");
		System.out.println(hm);
		//synchronizedSet(Set<T> s) 返回指定线程安全的 Set
		
	}
	
	// 查找、替换
	@Test
	public void testQueryAndReplace() {
		//Object max(Collection) 根据元素的自然顺序,返回给定集合中的最大元素
		//Integer max = Collections.max(list);
		//System.out.println(max);
		
		// Object min(Collection) 根据元素的自然顺序 返回给定 collection的最小元素
		//Integer min = Collections.min(list);
		//System.out.println(min);
		
		//int frequency(Collectio, Object) 返回指定集合中指定元素的出现次数
		//int time = Collections.frequency(list, 456);
		//System.out.println(time);
		
		//void copy(List dest,List src) 将 src 中的内容复制到 dest 中
		List<Integer> dest = Arrays.asList(new Integer[list.size()]);
		Collections.copy(dest, list);
		System.out.println("dest" + dest);
		
		//boolean replaceAll(List list,Object oldVal,Object newVal)使用新值替换 List 对象的所有旧值
		//Collections.replaceAll(list, 456, 654);
		//System.out.println(list);
	}
	
	// 排序操作
	@Test
	public void testSortMethod() {
		//reverse(List) 反转 List 中元素的顺序
		//Collections.reverse(list);
		//System.out.println(list);
		
		//shuffle(List) 对 List 集合元素进行随机排序
		//Collections.shuffle(list);
		//System.out.println(list);
		
		//sort(List) 根据元素的自然顺序对指定 List 集合元素按升序排序
		//Collections.sort(list);
		//System.out.println(list);
		
		//sort(List, Comparator) 根据指定的 Comparator 产生的顺序对List 集合元素进行排序
		/*Collections.sort(list, new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return o2.compareTo(o1);
			}
		});
		System.out.println(list);
		*/
		//swap(List, int, int) 将指定 list 集合中的 i 处元素和 j 处元素进行交换
		Collections.swap(list, 0, 3);
		System.out.println(list);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值