mlaaalm 发表于 2013-2-5 01:15:50

Collections乱七八糟

 
Collection: add(E e)         确保此 collection 包含指定的元素(可选操作)。boolean addAll(Collection<? extends E> c)         将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。void clear()         移除此 collection 中的所有元素(可选操作)。boolean contains(Object o)         如果此 collection 包含指定的元素,则返回 true。boolean containsAll(Collection<?> c)         如果此 collection 包含指定 collection 中的所有元素,则返回 true。boolean equals(Object o)         比较此 collection 与指定对象是否相等。int hashCode()         返回此 collection 的哈希码值。boolean isEmpty()         如果此 collection 不包含元素,则返回 true。Iterator<E> iterator()         返回在此 collection 的元素上进行迭代的迭代器。boolean remove(Object o)         从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。boolean removeAll(Collection<?> c)         移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。boolean retainAll(Collection<?> c)         仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。int size()         返回此 collection 中的元素数。Object[] toArray()         返回包含此 collection 中所有元素的数组。 <T> T[]toArray(T[] a)   !!!!很好所有Collection都能转成数组,反之呢? 利用List/set的构造函数做为 Set和数组转换的桥梁====================================Map:get/put/remove/clear/containsKey /containsValue/sizeList:get / indexof/lastIndexofSet 没有get!!set-list通过构造函数互相转换Collection(set,list,queue)接口toArray方法List Arrays.asList(array[]);System.arraycopy(src, srcPos, dest, destPos, length)new String("Abc".toCharArray());new String("Abc".getBytes("UTF-8"),"UTF-8");==================================排序:Collection.sort()对List only!还是list最好Arrays.sort();所有对象,和集合无关SortedMap,SortedSet本身利用构造函数传入不同的new Comparator来排序LinkedHashMap ,LinkedHashSet,实现是链表结构,参见linkedMap/Set(2种排序,插入orLRU)HashMap,HashSet本身迭代器是不保证排序的 
 
 
package samples;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import java.util.TreeSet;public class TestCollections {public static void main(String[] args) throws UnsupportedEncodingException {/* * create Map ,set,list */Map<String, String> hm = new HashMap<String, String>();hm.put("1", "1");hm.put("2", "2");hm.put("3", "3");Set<String> hs = hm.keySet();List<String> l = new ArrayList<String>(hs);populate(hm, hs, l);Collections.reverse(l);populate(hm, hs, l);/* * error List<String> l2 = Collections.unmodifiableList(l); * l2.add("4"); *//* * Collections.sort() for List only!!!! ** new Interface ()? 匿名类 */Collections.sort(l, new Comparator<String>() {public int compare(String s1, String s2) {return 1;}});Comparator<String> comparator = new Comparator<String>() {public int compare(String op1, String op2) {return 0;}};new String("Abc".toCharArray());new String("Abc".getBytes("UTF-8"), "UTF-8");/* * 构造了ts,按照自然排序 * 现在希望让ts按照另外一种方式排序,怎么做? * 下面ts2无效 * 只能遍历?? */TreeSet<String> ts= new TreeSet<String>();ts.add("1");ts.add("2");populateCollection(ts);TreeSet<String> ts2= new TreeSet<String>(comparator);ts2=ts;populateCollection(ts);populateCollection(ts2);}public static void populateCollection(Collection<String> c) {for (String s : c) {System.out.print(s + " ");}System.out.println();}public static void populateMap(Map<String, String> c) {for (Map.Entry<String, String> entry : c.entrySet()) {System.out.print(entry.getKey() + " ");System.out.print(entry.getValue() + " ");}System.out.println();}public static void populate(Map<String, String> m, Set<String> s,List<String> l) {populateMap(m);populateCollection(s);populateCollection(l);System.out.println("==============================");}} 
页: [1]
查看完整版本: Collections乱七八糟