精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
作者 | 正文 | ||||||||||||
发表时间:2011-04-15
最后修改:2011-04-22
闲来无事,项目做多了感觉基础弱了好多,业余时间仔仔细细阅读了一遍thinkinginjava4(集合基础篇章),跑完每个Sample,写完每个Ex,总结下其中一些心得,收获不少,后续还会补上其它章节心得~~~~
1.如果不需要使用每个元素的索引,你可以使用foreach语法来选择List中的每个元素
2.
这种方式并非总能奏效,因为某些累具有额外的功能。如果你需要使用这些方法,不能将他们向上转型为更通用的接口。
3.填充容器(Collection)和数组(Array) //填充容器 public Collection<String> fill(Collection<String> c, int n){ for(int i=0; i<n; i++){ c.add(this.next()); } return c; } //填充数字 public void fillAll(String[] ary){ for (int i = 0; i < ary.length; i++) { ary[i] = this.next(); } }
4.添加一组元素
Arrays.asList(...) //变参 Collections.addAll() Arrays.<Snow>asList(...);
List list = new ArrayList(Arrays.asList(elements)) ==ist.addAll(Arrays.asList(elements)) == Collections.addAll(list, "Peaches 'n Plutonium", "Rocky Racoon")
5.Arrays.asList(array);其返回类型为java.util.Arrays.ArrayList ------Arrays的内部类
String [] array = new String[]{"a","b","c","d","e"}; List<String> list = new ArrayList<String>(); List<String> subList = null; list = Arrays.asList(array); print("1: " + list); subList = list.subList(1, 3); print("2: " + subList); List<String> copy = new ArrayList<String>(list); copy.removeAll(subList); print("3: " + copy);
6.List类型的可以调用迭代器ListIterator 利用ListIterator逆序输出
List<Integer> list1 = new ArrayList<Integer>(Arrays.asList(3,4,5,6,7)); ListIterator<Integer> listIter = list1.listIterator(list1.size());//起始索引位 List<Integer> list2 = new ArrayList<Integer>(); while(listIter.hasPrevious()){ list2.add(listIter.previous()); } for (Integer integer : list2) { System.out.println(integer); }
7.String.CASE_INSENSITIVE_ORDER //Comparator<String>对象 对字符串排序 默认字符串排序方式是敏感的,此常量返回一个不敏感大小写的Comparator类型 Set<String> words = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);//参数为Comparator类型 words.addAll(new TextFile("src/com/mindviewinc/chapter11/sample/collection/SetOperations.java", "\\W+")); System.out.println(words);
8.Set应用 计算单词中的元音字母个数
public void vowelCounter(Set<String> st) { Set<Character> vowels = new TreeSet<Character>(); Collections.addAll(vowels, 'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'); int allVowels = 0; for(String s : st) { int count = 0; for(Character v : s.toCharArray()) { //toCharArray字符串转化为char[] if(vowels.contains(v)) { count++; allVowels++; } } System.out.print(s + ": " + count + ", "); } System.out.println(); System.out.print("Total vowels: " + allVowels); }
9.Map 统计数量 map.put(c, (map.get(c)!=null?map.get(c):1) + 1);
10.List排序 Collections.sort(words, String.CASE_INSENSITIVE_ORDER); 如果该list已经排序迭代的时候,再插入其他集合时候可以用LinkedHashMap,保留其插入顺序
11.Set 排序
Set<String> keySet = map.keySet(); Set<String> set = new TreeSet<String>(keySet);//用TreeSet排序,TreeSet自动维护队列顺序,不用手动进行排序 Or
List<String> ss2List = new LinkedList<String>(m.keySet()); Collections.sort(ss2List);// 转化为List进行排序
12.Queue
13.PriorityQueue 按优先级出队列
打印队列中内容
while(pq.peek() != null) System.out.print(pq.poll() + " ");
PriorityQueue pq = new PriorityQueue(); pq.offer(new User()); pq.offer(new User()); //com.mindviewinc.chapter11.exercise.User cannot be cast to java.lang.Comparable 14.Collection和Iterator 当你要实现一个不是Collection的外部类时,由于让它去实现Collection接口可能非常困难或麻烦,因此使用Iterator就会变得非常吸引人。(The use of Iterator becomes compelling when you implement a foreign class, one that is not a Collection, in which it would be difficult or annoying to make it implement the Collection interface.)
15.foreach和Iterator Iterable接口被foreach用来在序列中移动,不是所有的集合类才会实现Interable接口。 例(解释14/15):
class PetSequence { protected Pet[] pets = Pets.createArray(8); } public class Ex32 extends PetSequence implements Iterable<Pet>{ public Iterator<Pet> iterator() { //实现一个不是Collection的外部类时,使用Iterator就会变得非常吸引人 return new Iterator<Pet>() { private int index = 0; public boolean hasNext() { return index < pets.length; } public Pet next() { return pets[index++]; } public void remove() { // Not implemented throw new UnsupportedOperationException(); } }; } public Iterable<Pet> reversed() { //Iterable不是Iterator return new Iterable<Pet>() { public Iterator<Pet> iterator() { return new Iterator<Pet>() { int index = pets.length - 1; public Pet next() { return pets[index--]; } public boolean hasNext() { return index >= 0; } public void remove() { throw new UnsupportedOperationException(); } }; } }; } public static void main(String[] args) { Ex32 ex = new Ex32(); for (Pet pet : ex.reversed()) {//利用foreach逆序 System.out.println(pet); } for (Pet pet : ex) { System.out.println(pet); } } }
16. List<Integer> list = new ArrayList<Integer>(Arrays.asList(ia));//1.深拷贝,重构一个ArrayList List<Integer> list = Arrays.asList(ia);//2.返回一个不可变的固定大小的ArrayList 如果将两个list排序,2会打乱数组ia的顺序,1则不会
String[] strs = {"1","2","3","4","5","6","7","8","9","10"}; //List<String> list = Arrays.asList(strs); List<String> list = new ArrayList<String>(Arrays.asList(strs));//深拷贝 Collections.shuffle(list); PPrint.pprint(list); PPrint.pprint(strs);
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|||||||||||||
返回顶楼 | |||||||||||||
浏览 1910 次