Java -- 容器使用 Set, List, Map, Queue, Collections

1. ArrayList

	 	ArrayList<String> c = new ArrayList<String>();
	 	c.add("hello");
	 	c.add("world");
	 	String a = new String("aya");
	 	c.add(a);
	
	 	for( Object obj : c  )
	 	{
	 		System.out.println( (String)obj );
	 	}
	 	
	 	c.set(2, "111");
	 	
	 	Iterator<String> it = c.iterator();
	 	while(it.hasNext())
	 	{
	 		System.out.println( (String)it.next() );	 		
	 	}

ArrayList 的迭代器还有 ListIterator 可以有正反两种查询,先hasNext知道最后 hasPrevious才能有值

	 	ListIterator<String> it2 = c.listIterator();	 	
	 	while(it2.hasNext())
	 	{
	 		System.out.println( "hasNext " + (String)it2.next() );
	 	}	 	
	 	while(it2.hasPrevious())
	 	{
	 		System.out.println( "hasPrevious " + (String)it2.previous() );
	 	}


2. HashSet 集合是无序的。

HashSet集合判断两个元素相等的标准是两个对象通过equals 方法比较相等,并且两个对象的hashCode()方法返回值也相等。

向HashSet中添加可变对象时,必须小心,如果修改HashSet集合中的对象有可能导致该对象与集合中其他对象相等,从而导致Hashset无法准确访问该对象。

3. TreeSet 集合有序

由于TreeSet有序, 所有有访问第一个,前一个,后一个,最后一个并可以截取子集。

排序的顺序并不是根据插入的顺序,而是根据元素实际值来排序。

	 	TreeSet<Integer> nums = new TreeSet<Integer>();
	 	nums.add(4);
	 	nums.add(3);
	 	nums.add(2);
	 	nums.add(1);
	 	
	 	System.out.println(nums);  //整个集合
	 	System.out.println(nums.first()); 
	 	System.out.println(nums.last());
	 	System.out.println(nums.headSet(3)); //小于3,但不包括3
	 	System.out.println(nums.tailSet(3)); //大于等于3
	 	System.out.println(nums.subSet(1, 3));  大于等于1,小于3

结果为

[1, 2, 3, 4]
1
4
[1, 2]
[3, 4]
[1, 2]

 TreeSet自然排序:根据元素大小,升序排列。 对于TreeSet判断两个元素相等的标准是,两个对象通过equals方法比较返回true,并且通过compareTo(Object obj)比较 返回0。

和HashSet一样集合中为可变对象时,修改对象值后可能容易出错,为了健壮性,HashSet和TreeSet推荐放入不可变对象。

 TreeSet定序排列: 使用comparator接口帮助。

4. EnumSet 是一个专为枚举类设计的集合类,其中所有的值都是指定枚举类型的枚举值。没有暴露构造器,必须用静态方法创建。

enum Season
{
	SPRING,SUMMER,FALL,WINTER;
}

public class Main {		
	public static void main(String[] args) {
		
		EnumSet es1 = EnumSet.allOf(Season.class); //创建一个EnumSet集合,集合元素就是Season枚举类的全部枚举值
		System.out.println(es1);		
		EnumSet es2 = EnumSet.noneOf(Season.class); //空集合,但指定其中的元素是Season类的枚举值
		System.out.println(es2);
		es2.add(Season.SPRING);
		es2.add(Season.SUMMER);
		System.out.println(es2);
		EnumSet es3 = EnumSet.of(Season.SPRING, Season.SUMMER); // 以指定枚举值创建EnumSet
		System.out.println(es3);
		EnumSet es4 = EnumSet.range(Season.SPRING, Season.FALL); // 范围值
		System.out.println(es4);
		EnumSet es5 = EnumSet.complementOf(es4); //Season中 除去es4里剩下的部分 为es5
		System.out.println(es5);		
	}	
}

输出结果:

[SPRING, SUMMER, FALL, WINTER]
[]
[SPRING, SUMMER]
[SPRING, SUMMER]
[SPRING, SUMMER, FALL]
[WINTER]

5. LinkedList 不仅可以当做双向队列使用,还可以当做“栈”使用,其包含有 pop出栈 和 push入栈 方法

	 	LinkedList<String> books = new LinkedList<String>();
	 	books.offer("test1");  //加入队列尾部
	 	books.push("test2");   //入栈
	 	books.offerFirst("test3"); //放入头部
		for(int i=0; i<books.size(); i++)
		{
			System.out.println( books.get(i) );
		}
		
		System.out.println( books.peekFirst() ); //访问不删除
		System.out.println( "log1: " + books );
		System.out.println( books.pollFirst() );  //访问并删除
		System.out.println( "log2: " + books );		

输出结果:

test3
test2
test1
test3
log1: [test3, test2, test1]
test3
log2: [test2, test1]

6. Map

HashMap 和 HashTable 判断Value相等的条件只需要equals方法返回true即可,判断Key相等的条件是equals()和hashCode()返回值都相等。

LinkedHashMap以链表来维护内部顺序。性能低于HashMap,消耗低于TreeMap。

TreeMap key的排序也是根据红黑树排列,有两种排序方式,自然排序和定制排序。

WeekHashMap 只保留key对应对象的弱引用,可能被垃圾回收。

enum Season
{
	SPRING,SUMMER,FALL,WINTER;
}

public class Main {		
	public static void main(String[] args) {
		
		HashMap<Integer, String> map1 = new HashMap<Integer, String>();		
		map1.put(1, "hello");
		map1.put(2, "world");
		System.out.println(map1);
		System.out.println(map1.containsKey(3));
		System.out.println(map1.containsValue("world"));
		map1.put(2, "aya");	
		System.out.println(map1);				
	
		for(Map.Entry<Integer, String> m : map1.entrySet())
		{
			System.out.println(m);
			System.out.println(m.getKey());
		}		
		EnumMap map2 = new EnumMap(Season.class);
		map2.put(Season.SPRING, "spring");
		map2.put(Season.SUMMER, "summer");
		System.out.println(map2);		
	}		
}


7. Collections 操作list map set 的工具类

		ArrayList<Integer> list = new ArrayList<Integer>();
		list.add(1);
		list.add(2);
		list.add(3);
		System.out.println(list);
		Collections.reverse(list);  //反转排序
 		System.out.println("reverse " + list);
		Collections.shuffle(list);  //乱序 洗牌
		System.out.println("shuffle " + list);
		Collections.sort(list);     //自然排序
		System.out.println("sort " + list);
		Collections.swap(list, 0, 1); //交换位置 0 - 1交换
		System.out.println("swap " + list);
		Collections.rotate(list, 2);  //向右推移2位
		System.out.println("rotate " + list);
		
		System.out.println("max " + Collections.max(list)); //最大值
		System.out.println("min " + Collections.min(list)); //最小值
		Collections.replaceAll(list, 3, 1);    //替换,将所有的 3 替换为 1
		System.out.println("replaceAll " + list); 
		System.out.println("fre " + Collections.frequency(list, 1)); // 1 出现的频率
		System.out.println("binarySearch " + Collections.binarySearch(list, 2)); //二分法查找, 2的引锁值

输出结果:

[1, 2, 3]
reverse [3, 2, 1]
shuffle [3, 2, 1]
sort [1, 2, 3]
swap [2, 1, 3]
rotate [1, 3, 2]
max 3
min 1
replaceAll [1, 1, 2]
fre 2
binarySearch 2

8. 控制线程安全

之上的list map set 都是不能做到线程安全的,

可以如下建立: List<String> list2 = Collections.synchronizedList(new ArrayList<String>()); 

同样 map  set 也可以这样建立,做到线程安全。

9. 创建不可修改集合

利用Collections.unmodifiableXXX即可, set  map list 均可, list2将不可修改, list2.add() 将报异常。

	    ArrayList<Integer> list = new ArrayList<Integer>();
		list.add(1);
		list.add(2);
		list.add(3);

		List<Integer> list2 = Collections.unmodifiableList(list);
		list2.add(1);



 

原文地址:https://www.cnblogs.com/xj626852095/p/3648079.html