List

对集合中对象进行字典排序:
public class TestMain {

 static class ComparatorUser implements Comparator {
  public int compare(Object o1, Object o2) {
   try {
    // 取得比较对象的汉字编码,并将其转换成字符串
    String s1 = new String(o1.toString().getBytes("GB2312"), "ISO-8859-1");
    String s2 = new String(o2.toString().getBytes("GB2312"), "ISO-8859-1");
    // 运用String类的 compareTo()方法对两对象进行比较
    return s1.compareTo(s2);
   } catch (Exception e) {
    e.printStackTrace();
   }
   return 0;
  }
 }
 public static void main(String[] args) throws Exception {

  List<String> mList = new ArrayList<String>();
  mList.add("car");
  mList.add("are");
  mList.add("mm");
  mList.add("ztree");
  mList.add("东海湾");
  mList.add("傲来");
  mList.add("东海湾-岩洞");
  mList.add("傲来药店");

  Collections.sort(mList, new ComparatorUser());
  for (int i = 0; i < mList.size(); i++) {
   System.out.println(mList.get(i));
  }
 }
}

 

1、/**
  * 去除集合中的重复元素
  * @param  SameElist:包含重复元素的集合
  * @return  没有重复元素的集合       
  * @author  liyuhao
  */
 public List<String> getNotSameElist(List<String> SameElist)
 {
   Set<String> hashSet = new HashSet<String>(SameElist);
   List<String> notSameEList = new ArrayList<String>(hashSet);
   return notSameEList;
 }

2.数组转list:

 String[] arr = new String[] {"1", "2"}; 
    List<String> list = Arrays.asList(arr);
    for (String i : list) {
     System.out.println("元素:"+i);
    }

List转数组:

String[] toBeStored = list.toArray(new String[list.size()]);

3.效果:123,456,789

实现:

public static void main(String[] args) {

System.out.println(stringArrayJoin(new String[]{"123","456","789"},","));

}

public static String stringArrayJoin( String[] strArray, String separator ) {

   StringBuffer strbuf = new StringBuffer();

   for( int i = 0; i < strArray.length; i++ ) {

       strbuf.append( separator ).append( strArray[i] );

   }

           // return strbuf.toString();   //  ,123,456,789

    return strbuf.deleteCharAt( 0 ).toString();

}


java中给集合快速取值最大值和最小值:

http://blog.sina.com.cn/s/blog_85987afc01013ss1.html


你可能感兴趣的:(List)