JAVA自学教程之集合框架工具类(一)



JAVA自学教程之集合框架工具类(一)。

一、Collections:集合框架的工具类

其中的方法都是静态的

排序方法演示

 

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.List;
  5. class ComparaByLeng implements Comparator<String>{
  6.     public int compare(String o1, String o2) {
  7.         int t = o1.length() - o2.length();
  8.         return t==0?o1.compareTo(o2):t;
  9.     }
  10. }
  11. public class Main
  12. {
  13.     public static void main(String[] args) {
  14.         Colletyions_Demo();
  15.     }
  16.     public static void Colletyions_Demo(){
  17.         List<String> li = new ArrayList<String>();
  18.         li.add(“asd”);    li.add(“d”);
  19.         li.add(“efasd”);  li.add(“efasd”);
  20.         li.add(“dsssfd”); li.add(“xxxxd”);
  21.         //System.out.println(list);
  22.         //Collections.sort(list);//排序
  23.         //System.out.println(list);
  24.         //sort :public static <T extends Comparable<? super T>> void sort(List<T> list)
  25.         //模拟sort
  26.         //Mysort(list);
  27.         //System.out.println(list);
  28.         Mysort(li,new ComparaByLeng());
  29.         System.out.println(li);
  30.         Collections.sort(li,new ComparaByLeng());
  31.         System.out.println(li);
  32.     }
  33.     /*public static <T extends Comparable<? super T>> void Mysort(List<T> list)
  34.     {
  35.         for (int i = 0; i < list.size()-1; i++) {
  36.             for (int j = i+1; j < list.size(); j++) {
  37.                 if(list.get(i).compareTo(list.get(j))>0){
  38.                     Collections.swap(list, i, j);//工具类  交换
  39.                 }
  40.             }
  41.         }
  42.     }   */
  43.     public static <T> void Mysort(List<T> li,Comparator<? super T> com) {
  44.         for (int i = 0; i < li.size()-1; i++) {
  45.             for (int j = i+1; j < li.size(); j++) {
  46.                 if(com.compare( li.get(i), li.get(j) ) > 0 ){
  47.                     Collections.swap(li, i, j);
  48.                 }
  49.             }
  50.         }
  51.     }
  52. }

 

 

二分查找、最值方法演示

 

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.List;
  5. public class Main
  6. {
  7.     public static void main(String[] args) {
  8.         Colletyions_Demo();
  9.     }
  10.     public static void Colletyions_Demo(){
  11.         List<String> li = new ArrayList<String>();
  12.         li.add(“asd”);    li.add(“d”);
  13.         li.add(“efasd”);  li.add(“efasd”);
  14.         li.add(“dsssfd”); li.add(“xxxxd”);
  15.         Collections.sort(li);
  16.         System.out.println(li);
  17.         int index = Collections.binarySearch(li, ”d”);
  18.         System.out.println(index);//负数代表没找到
  19.         //获取最大值
  20.         String maxstrString = Collections.max(li);
  21.         System.out.println(“max = ”+maxstrString);
  22.         //获取最小值
  23.         String minstrString = Collections.min(li);
  24.         System.out.println(“min = ”+minstrString);
  25.         //取最长
  26.         String maxlenString = Collections.max(li,new ComparaByLeng());
  27.         System.out.println(maxlenString);
  28.     }
  29. }


逆转、替换方法演示

 

 

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.TreeSet;
  5. public class Main
  6. {
  7.     public static void main(String[] args) {
  8.         Colletyions_Demo();
  9.     }
  10.     public static void Colletyions_Demo(){
  11.         //原理
  12. //      TreeSet<String> ts = new TreeSet<String>(new Comparator<String>() {
  13. //          public int compare(String o1, String o2) {
  14. //
  15. //              return o2.compareTo(o1);
  16. //          }
  17. //      });
  18.         //封装后
  19.         TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder());
  20.         ts.add(“ad”);ts.add(“xad”);
  21.         ts.add(“fdagrrgd”);ts.add(“a”);
  22.         ts.add(“gedsad”);
  23.         System.out.println(ts);
  24.         //如果有比较器,逆转
  25.         //长度逆转
  26.         TreeSet<String> td = new TreeSet<String>(Collections.reverseOrder(new ComparaByLeng()));
  27.         td.add(“ad”);td.add(“xad”);
  28.         td.add(“fdagrrgd”);td.add(“a”);
  29.         td.add(“gedsad”);
  30.         System.out.println(td);
  31.         //replaceAll / reverse方法
  32.         List<String> al = new ArrayList<String>();
  33.         al.add(“ads”);
  34.         al.add(“ds”);
  35.         al.add(“s”);
  36.         Collections.reverse(al);
  37.         System.out.println(al);
  38.         Collections.replaceAll(al, ”s”, ”y”);//实际上 -> set(indexOf(“s”),”y”)
  39.         System.out.println(al);
  40.     }
  41. }

 

 


其他方法
1、fill(List<? super T> list, T obj)
使用指定元素替换指定列表中的所有元素。

Collections.fill(list,”asd”);

 

2、shuffle(List<?> list)
使用默认随机源对指定列表进行置换

Collections.shuffle(list);随机

更多方法看API文档。。。

 

将非同步集合转成同步集合的方法

 

static

<T> Collection<T>
synchronizedCollection(Collection<T> c)
返回指定 collection 支持的同步(线程安全的)collection。
static

<T> List<T>
synchronizedList(List<T> list)
返回指定列表支持的同步(线程安全的)列表。
static

<K,V> Map<K,V>
synchronizedMap(Map<K,V> m)
返回由指定映射支持的同步(线程安全的)映射。
static

<T> Set<T>
synchronizedSet(Set<T> s)
返回指定 set 支持的同步(线程安全的)set。
static

<K,V> SortedMap<K,V>
synchronizedSortedMap(SortedMap<K,V> m)
返回指定有序映射支持的同步(线程安全的)有序映射。
static

<T> SortedSet<T>
synchronizedSortedSet(SortedSet<T> s)
返回指定有序 set 支持的同步(线程安全的)有序 set。

 

 

转换原理:

 

  1. List list = new ArrayList();
  2. List = MyCollections.synList(list);//返回一个同步的list
  3. class MyCollections{
  4.     public List synList (List list){
  5.         return new MyList(list);
  6.     }
  7.     private class MyList implements List{
  8.         private List list;
  9.         private static final Object lock = new Object();
  10.         public MyList() {
  11.             super();
  12.             // TODO Auto-generated constructor stub
  13.         }
  14.         public MyList(List list) {
  15.             super();
  16.             this.list = list;
  17.         }
  18.         public boolean add(Object obj){
  19.             synchronized (lock) {
  20.                 return list.add(obj);
  21.             }
  22.         }
  23.         public boolean remove(Object obj){
  24.             synchronized (lock) {
  25.                 return list.remove(obj);
  26.             }
  27.         }
  28.     }
  29. }