J2SE学习笔记(五)

19、集合

Set:无序、值不可重复,否则将被覆盖

List:有序,值可重复,不会被覆盖

Map:具有映射关系的集合

Queue:队列

J2SE学习笔记(五)_第1张图片


(a)、Collection接口:

boolean add(E e)  

boolean addAll(Collection<? extends E> c) 

void clear() 

boolean contains(Object o) 

boolean containsAll(Collection<?> c) 

boolean remove(Object o) 

boolean removeAll(Collection<?> c) 

boolean retainAll(Collection<?> c) 

int size()  

Object[] toArray()  

/**
 * ArrayList
 */
ArrayList<Object> arrList=new ArrayList<>();
arrList.add("abc");
arrList.add(123);
arrList.add("abc");
System.out.println("集合元素数量:"+arrList.size());//集合元素数量:3
arrList.remove("abc");
System.out.println(arrList);//[123, abc]
Collection<Object> c=new ArrayList<>();
c.add("abc");
c.add("world");
arrList.addAll(c);
arrList.remove(Integer.valueOf(123));
System.out.println(arrList);//[abc, abc, world]

└──Set接口:(继承自Conllection一个不包含重复元素的 collection),Set 不包含满足 e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null 元素。

    HashSet:此类实现 Set 接口,由哈希表(实际上是一个 HashMap 实例)支持。它不保证 set 的迭代顺序;特别是它不保证该顺序恒久不变。此类允许使用 null 元素。

/*
 * HashSet
 */
HashSet<Object> hs=new HashSet<>();
hs.add(123);
hs.add(456);
hs.add(789);
hs.add("4abc");
hs.add(456);
System.out.println(hs.size());//4
System.out.println(hs);//[789, 4abc, 456, 123]

    LinkedHashSet:按照add顺序排序的set

/*
 * LinkedHashSet
 */
LinkedHashSet<Object> lhs=new LinkedHashSet<>();
lhs.add("hello");
lhs.add(333);
lhs.add("world");
lhs.add(888);
lhs.add(111);
System.out.println(lhs);//[hello, 333, world, 888, 111]

    TreeSet:使用元素的自然顺序对元素进行排序,或者根据创建 set 时提供的 Comparator 进行排序(Override compare方法)

TreeSet<String> ts=new TreeSet<>();
ts.add("554");
ts.add("123");
ts.add("999");
ts.add("abc");
ts.add("2dn");
System.out.println(ts);//[123, 2dn, 554, 999, abc]
		
TreeSet<Integer> ts2=new TreeSet<Integer>(new Comparator<Integer>() {
	@Override
	public int compare(Integer o1, Integer o2) {
		if(o1<o2){
			return 1;
		}else if(o1>o2){
			return -1;
		}else{
			return 0;
		}
	}
});
ts2.add(9392);
ts2.add(392);
ts2.add(2);
ts2.add(92);
ts2.add(929);
System.out.println(ts2);//[9392, 929, 392, 92, 2]
		
TreeSet<Integer> ts3=new TreeSet<Integer>();
ts3.addAll(ts2);
System.out.println(ts3);//[2, 92, 392, 929, 9392]

    EnumSet:与枚举类型一起使用的专用 Set 实现。枚举 set 中所有键都必须来自单个枚举类型,该枚举类型在创建 set 时显式或隐式地指定。

enum Season{
	SPRING,SUMMER,FALL,WINTER
}

EnumSet<Season> esEnumSet=EnumSet.allOf(Season.class);
esEnumSet.remove(Season.FALL);
System.out.println(esEnumSet);//[SPRING, SUMMER, WINTER]

└──List接口:(有序的 collection)

    ArrayList:List 接口的大小可变数组的实现。非线程安全。

    Vector:Vector 类可以实现可增长的对象数组。线程安全。

    Stack:public class Stack<E>extends Vector<E>,模仿栈的后进先出:peek(),pop(),push(E item)

Stack<Object> sk=new Stack<>();
sk.push("aa");
sk.push("bb");
sk.push("cc");
System.out.println(sk);//[aa, bb, cc]
sk.pop();
System.out.println(sk);//[aa, bb]


└──Queue接口:(队列)

    priorityQueue:按照大小排序后,头进底出;可以自然排序或者定制排序,禁止null元素

PriorityQueue<String> pq=new PriorityQueue<>();
pq.offer(666+"");
pq.offer("abc");
pq.offer(999+"");
pq.offer("ccc");
System.out.println(pq);//[666, abc, 999, ccc]
pq.poll();
pq.poll();
System.out.println(pq);//[abc, ccc]

    ArrayDeque:双端队列,禁止null元素

    LinkedList:双端队列,同时支持索引查询元素,允许null元素

(b)、Map接口:public interface Map<K,V>  将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。

HashMap:非线程安全,允许使用null做为key

HashMap<String,Object> hm = new HashMap<>();
hm.put("abc", 123);
hm.put(null, "hello");
System.out.println(hm);//{null=hello, abc=123}

Hashtable:线程安全,不允许使用null作为key,否则会抛出空指针异常

Hashtable<String,Object> ht=new Hashtable<>();
ht.put("abc",123);
ht.put("scde", "hello");
ht.put("ff", 999);
for(String key:ht.keySet()){
	System.out.println(key+":"+ht.get(key));
}
//ff:999
//abc:123
//scde:hello

Properties:<String,String>,表示了一个持久的属性集。Properties 可保存在流中或从流中加载。

LinkedHashMap:有序迭代的哈希表,其顺序与插入顺序相同

LinkedHashMap<String, String> lhm=new LinkedHashMap<>();
lhm.put("mkd", "abc");
lhm.put("1ay","9d9");
lhm.put("u99",null);
lhm.put("6dd", null);
System.out.println(lhm);//{mkd=abc, 1ay=9d9, u99=null, 6dd=null}


java.util.Iterator 迭代器

boolean hasNext()  

E next() 

void remove() //在集合迭代的过程中,改变集合内容可能会引发异常

/*
 * Iterator
 */
Iterator<Object> it=hs.iterator();
while(it.hasNext()){
	System.out.print(it.next()+",");
}

java.util.ListIterator 系列表迭代器

        允许程序员按任一方向遍历列表、迭代期间修改列表,并获得迭代器在列表中的当前位置nextIndex()

/*
 * ListIterator
 */
ListIterator<Object> listIt=arrList.listIterator();
while(listIt.hasNext()){
	System.out.println(listIt.nextIndex()+":"+listIt.next());
}

Collections工具类

synchronizedXXX()方法:可以将指定集合包装成线程同步的集合


线程安全集合类与非线程安全集合类 

LinkedList、ArrayList、HashSet是非线程安全的,Vector是线程安全的;

HashMap是非线程安全的,HashTable是线程安全的;

StringBuilder是非线程安全的,StringBuffer是线程安全的。


集合适用场景

对于查找和删除较为频繁,且元素数量较多的应用,Set或Map是更好的选择;

ArrayList适用于通过为位置来读取元素的场景;

LinkedList 适用于要头尾操作或插入指定位置的场景;

Vector 适用于要线程安全的ArrayList的场景;

Stack 适用于线程安全的LIFO场景;

HashSet 适用于对排序没有要求的非重复元素的存放;

TreeSet 适用于要排序的非重复元素的存放;

HashMap 适用于大部分key-value的存取场景;

TreeMap 适用于需排序存放的key-value场景。

你可能感兴趣的:(J2SE,java集合,Java学习笔记)