各位看官们,大家好,上一回中咱们说的是Java中类集之工具类的例子,这一回咱们继续说该例子。闲话休提,言归正转。让我们一起Talk Android吧!
看官们,我们在上一章回中介绍了工具类及其提供的方法,不过都是偏向理论性的内容,而没有具体的实践,这显然不符合我们的风格。这一回中,我们将通过代码结合文本的方式来演示如何使用工具类中的功能,这样可以帮助大家进一步加深理解工具类。好了,直接上代码吧,如果看不懂代码了,直接看代码中的注释就能明白。
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class collentionsEx {
private static List<Integer> list = new ArrayList<>();
private static List<Integer> link = new LinkedList<>();
private static Set<Integer> hashSet = new HashSet<>();
private static Set<Integer> treeSet = new TreeSet<>();
public static void main(String args[]) {
//init the collections
Collections.addAll(list,1,3,5,7,9);
Collections.addAll(link,1,3,5,7,9);
Collections.addAll(hashSet,1,3,5,7,9);
Collections.addAll(treeSet,1,3,5,7,9);
showCollection();
//append some content in list, other collection can be used too.
Collections.addAll(list, 3,4,5);
System.out.println(" \nadd some new content in list.");
showList();
//find the content in collection,collection must be sorted, set can't be used
int index = Collections.binarySearch(list,new Integer(3));
if(index>=0){
System.out.println("find 3 at: "+index);
}else{
System.out.println("can't find 3."+index);
}
//replace the content in collection, it is only used for list.
System.out.println(" \nreplace content 3 with 6 in list.");
Collections.replaceAll(list, 3,6);
showList();
// swap the content of index a and b, it is only used for list.
Collections.swap(list,1, 2);
System.out.println(" \nswap the content,which located at 1 and 2 in list.");
showList();
// sort the list, it is only used for list.
Collections.sort(list);
System.out.println(" \nsort the content in list.");
showList();
// revert the list, it is only used for list.
System.out.println(" \nreverse content in list. set and map can't be reversed");
Collections.reverse(list);
Collections.reverse(link);
showCollection();
// Collections.reverse(hashSet);
// Collections.reverse(treeSet);
}
public static void showCollection() {
//show content of collection by Iterator
Iterator<Integer> iter = list.iterator();
System.out.print("List: ");
while(iter.hasNext()) {
System.out.print(iter.next()+" ");
}
System.out.println(" ");
iter = link.iterator();
System.out.print("Link: ");
while(iter.hasNext()) {
System.out.print(iter.next()+" ");
}
System.out.println(" ");
iter = hashSet.iterator();
System.out.print("HashSet: ");
while(iter.hasNext()) {
System.out.print(iter.next()+" ");
}
System.out.println(" ");
iter = treeSet.iterator();
System.out.print("TreeSet: ");
while(iter.hasNext()) {
System.out.print(iter.next()+" ");
}
System.out.println(" ");
}
public static void showList() {
//show content of list by Iterator
Iterator<Integer> iter = list.iterator();
System.out.print("List: ");
while(iter.hasNext()) {
System.out.print(iter.next()+" ");
}
System.out.println(" ");
}
}
下面是程序的运行结果,请参考:
List: 1 3 5 7 9
Link: 1 3 5 7 9
HashSet: 1 3 5 7 9
TreeSet: 1 3 5 7 9
add some new content in list.
List: 1 3 5 7 9 3 4 5
find 3 at: 1
replace content 3 with 6 in list.
List: 1 6 5 7 9 6 4 5
swap the content,which located at 1 and 2 in list.
List: 1 5 6 7 9 6 4 5
sort the content in list.
List: 1 4 5 5 6 6 7 9
reverse content in list. set and map can't be reversed
List: 9 7 6 6 5 5 4 1
Link: 9 7 5 3 1
HashSet: 1 3 5 7 9
TreeSet: 1 3 5 7 9
各位看官,关于Java中类集之工具类的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!