java中的集合类的几个鲜为人知的实用方法

 
 java中的集合类的几个鲜为人知的实用方法

1。list方法。   将 Enumeration 类型转换成list类型

2。swap方法。方便的调换一个list中的两个元素的位置。

3。lastIndexOfSubList方法。从一个list中从后面开始查找另外一个list第一次出现的位置。

4。rotate方法。在一个list中,顺序移动每一个元素的位置到指定的位置。

5。replaceAll方法。用指定的元素替换一个list中所用匹配的元素。

6。indexOfSubList方法。从一个list中从前面开始查找另外一个list第一次出现的位置。

示例程序:

import java.util.*;

class TestCollections {

 public static void main(String[] args) {
  TestCollections t = new  TestCollections();
  t.testList();
 }
 public void testList() {
  Vector v = new Vector();
  v.add("a");
  v.add("b");
  Enumeration e = v.elements() ;
  List l = Collections.list(e);
  System.out.println(l);
 }
 
 public void testSwap() {
  List l = new ArrayList();
  l.add("t");
  l.add("a");
  l.add("n");
  l.add("k");
  l.add("s");
  System.out.println(l);
  Collections.swap(l,1,3);
  System.out.println(l);
 }
 
 public void testLastIndexOfSubList() {
  List l = new ArrayList();
  l.add("a");
  l.add("b");
  l.add("c");
  l.add("d");
  l.add("e");
  l.add("a");
  l.add("b");
  l.add("c");
  l.add("d");
  l.add("e");
  List l2 = new ArrayList();
  l2.add("b");
  l2.add("c");
  l2.add("d");
  int result = Collections.lastIndexOfSubList(l,l2);
  if(result != -1) {
   System.out.println("!!! " + result + ". Found from " + l + " with " + l2);
  } else {
   System.out.println("!!! Not found from " + l + " with " + l2);
  }
  List l3 = new ArrayList();
  l3.add("b");
  l3.add("d");
  l3.add("d");
  result = Collections.lastIndexOfSubList(l,l3);
  if(result != -1) {
   System.out.println("!!! " + result + ". Found from " + l + " with " + l3);
  } else {
   System.out.println("!!! Not found from " + l + " with " + l3);
  }
 }
 public void testRotate() {
  List l = new ArrayList();
  l.add("t");
  l.add("a");
  l.add("n");
  l.add("k");
  l.add("s");
  System.out.println(l);
  Collections.rotate(l,1);
  System.out.println(l);
  
  //
   l = new ArrayList();
  l.add("t");
  l.add("a");
  l.add("n");
  l.add("k");
  l.add("s");
  System.out.println(l);
  Collections.rotate(l,-4);
  System.out.println(l);
  
   l = new ArrayList();
  l.add("a");
  l.add("b");
  l.add("c");
  l.add("d");
  l.add("e");
  System.out.println(l);
  Collections.rotate(l.subList(1, 4), -1);
  System.out.println(l);

  
 }
 
 public void testReplaceAll() {
  List l = new ArrayList();
  l.add("t");
  l.add("a");
  l.add("n");
  l.add("a");
  l.add("s");
  System.out.println(l);
  boolean b = Collections.replaceAll(l,"a","hello");
  System.out.println(l);
  System.out.println(b);
  
  // not found
  b = Collections.replaceAll(l,"a","hello");
  System.out.println(b);
  
 }
 
 public void testIndexOfSubList() {
  List l = new ArrayList();
  l.add("a");
  l.add("b");
  l.add("c");
  l.add("d");
  l.add("e");
  List l2 = new ArrayList();
  l2.add("b");
  l2.add("c");
  l2.add("d");
  int result = Collections.indexOfSubList(l,l2);
  if(result != -1) {
   System.out.println("!!! " + result + ". Found from " + l + " with " + l2);
  } else {
   System.out.println("!!! Not found from " + l + " with " + l2);
  }
  List l3 = new ArrayList();
  l3.add("b");
  l3.add("d");
  l3.add("d");
  result = Collections.indexOfSubList(l,l3);
  if(result != -1) {
   System.out.println("!!! " + result + ". Found from " + l + " with " + l3);
  } else {
   System.out.println("!!! Not found from " + l + " with " + l3);
  }
 }
}



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=443020

 

你可能感兴趣的:(java,list,vector,function,String,import)