JAVASE学习笔记第17天

集合(Collections---sort)

1、在集合中我们知道List是无无法对元素进行排序的,所以在集合框架中就有了Collections这个类。

2、Collections中的方法都是静态的,没有构造方法

3、Collections中提供了srot的两个用于比较的方法,注意不能对Set集合进行排序。

4、总结: 一般只要涉及到了对象的排序都要涉及到两个接口:一个是Comparable,一个是Comarator

5、示例:

/**

Collections的两种排序方法:

 

1.根据元素的自然顺序对指定的列表进行排序

 public  static <T extends Comparable<? superT>> void sort(List<T> list)

 说明:传进来的集合中元素的类型不确定,但是这个元素的类型一定是Comparable的

 子类,使得元素自身就具备比较性,Comparable中传进来的元素的类型也不确定(Student)

 

 2、根据指定的比较器进行排序

 public static<T> voidsort(List<T> list,Comparator<? super T> c);

*/

import java.util.*;

 

//自定义比较器,根据字符串的长度排序

class StrLenComparator implements Comparator<String>

{

       public intcompare(String s1,String s2)

       {

              int num = newInteger(s1.length()).compareTo(new Integer(s2.length()));

 

              if(num==0)

                     returns1.compareTo(s2);

 

              return num;

       }

}

 

class Demo1

{

       public static voidmain(String[] args)

       {

              ArrayList<String>al = new ArrayList<String>();

 

              al.add("abc");

              al.add("w");

              al.add("w");

              al.add("aeeec");

              al.add("mma");

             

              sop("使用排序前:"+al);

             

              //使用自然顺序排序

              Collections.sort(al);

             

              sop("使用自然顺序排序后:"+al);

 

              //使用指定的比较器进行排序

              Collections.sort(al,newStrLenComparator());

             

              sop("使用指定的比较器进行排序后:"+al);

 

       }

 

       public static voidsop(Object obj)

       {

              System.out.println(obj);

       }

}

 

集合(Collections---max)

/**

集合工具类:Collections中获取最大值

1,根据元素的自然顺序,返回Collection的最大值

       publicstatic<T extends Object & Comparable<? super T>> Tmax(Collection<? extends T> coll);

 

2,根据指定的比较器产生的顺序,返回给定collectin的最大元素

    public static<T>  Tmax(Collection<? extends T> coll,Comparator<? super T> comp);

        

*/

 

import java.util.*;

 

//自定义比较器,根据字符串的长度排序

class StrLenComparator implementsComparator<String>

{

       publicint compare(String s1,String s2)

       {

              intnum = new Integer(s1.length()).compareTo(new Integer(s2.length()));

 

              if(num==0)

                     returns1.compareTo(s2);

 

              returnnum;

       }

}

 

class Demo2

{

       publicstatic void main(String[] args)

       {

              ArrayList<String>al = new ArrayList<String>();

 

              al.add("abc");

              al.add("w");

              al.add("w");

              al.add("aeeec");

              al.add("mma");

             

              sop("使用排序前:"+al);

             

              //使用自然顺序排序

              Collections.sort(al);          

              sop("使用自然顺序排序后:"+al);

 

              //获取最大值:

              Strings1=Collections.max(al);

              sop("使用自然顺序排序后:"+s1);

 

              //使用指定的比较器进行排序

              Collections.sort(al,newStrLenComparator());          

              sop("使用指定的比较器进行排序后:"+al);

 

              //获取最大值:

              Strings2=Collections.max(al,new StrLenComparator());

              sop("使用给定比较器排序后:"+s2);

 

       }

 

       publicstatic void sop(Object obj)

       {

              System.out.println(obj);

       }

}

集合(Collections---binarySearch)

1、binarySearch(二分查找)又叫做halfSearch(折半查找)

2、示例:

/**

java的集合框架工具:Collections:

 

1,(自然顺序)使用二分搜索法搜索指定的列表,以获得指定对象,如果对象不存在,返回-(插入点)-1;

public static<T> int binarySearch(List<? extendsComparabl<? super T>> list,T key)

 

2,(指定比较器的顺序)使用二分法搜索指定的列表,以获得指定对象,如果不存在,返回的是-(插入点)-1;

 public static <T> intbinarySearch(List<? extends T> list,T key,Comparator<? super T> c);

 

3,binarySearch底层的原理:(只不过这个返回的是插入点)

 

   public static int halfSearch(List<String> list,String key)

       {

              int max,min,mid;

              max=list.size()-1;

              min=0;

 

              while(min<=max)

              {

                     mid=(min+max)>>1;

                     String s=list.get(mid);

 

                     int num =key.compareTo(s);

 

                     if(num>0)

                            min=mid+1;

                     elseif(num<0)

                            max=mid-1;

                     else

                            returnmid;

              }

 

              return min;

       }

 

总结:二分查找只是适用于有序的。

*/

import java.util.*;

 

//自定义比较器,根据字符串的长度排序

class StrLenComparator implements Comparator<String>

{

       public intcompare(String s1,String s2)

       {

              int num = newInteger(s1.length()).compareTo(new Integer(s2.length()));

 

              if(num==0)

                     returns1.compareTo(s2);

 

              return num;

       }

}

 

class Demo3

{

       public static voidmain(String[] args)

       {

              ArrayList<String>al = new ArrayList<String>();

 

              al.add("jjj");

              al.add("b");

              al.add("c");

              al.add("aaadd");

             

              Collections.sort(al);

              sop("使用自然书序排序"+al);      

              //(自然),二分搜索

              int index =Collections.binarySearch(al,"bb");

              sop("二分搜索1:"+index);  

             

              Collections.sort(al,newStrLenComparator());

              //(比较器),二分搜索

              sop("二分搜索1:"+Collections.binarySearch(al,"bb",newStrLenComparator()));

       }

 

       public static voidsop(Object obj)

       {

              System.out.println(obj);

       }

}

 

集合(Collections---替换反转)

/**

1,替换:

@:public static<T> void fill(List<? super T> list,   T obj);

       使用指定元素替换指定列表中的所有元素。

@:public static<T> boolean replaceAll(List<T> list,  T oldVal,T newVal)

    使用 newVal 替换 list 中满足(oldVal==null ? e==null : oldVal.equals(e)) 的每个 e 元素。                   

                                                       

2,反转:

       publicstatic void reverse(List<?> list);

       反转指定列表中元素的顺序。

*/

import java.util.*;

 

class Demo4

{

       publicstatic void main(String[] args)

       {

              ArrayList<Integer>al = new ArrayList<Integer>();

 

              al.add(1);

              al.add(2);

              al.add(3);

              al.add(4);

 

              sop(al);

 

              //反转

              Collections.reverse(al);

              sop(al);

             

              //替换

              Collections.replaceAll(al,1,19);

              sop(al);

 

              //替换1

              Collections.fill(al,3);

              sop(al);

       }

 

       publicstatic void sop(Object obj)

       {

              System.out.println(obj);

       }

}

 

这段时间的学习感受就是:到了一定的水平之后,思考显得更为重要,只有知道自己想要什么,才会知道自己改怎么做,不过这需要很多的枯燥的学习之后,加油加油,阿加西。

 

集合(Collections--reverseOrder)

/**

反序:

1,publicstatic<T> Comparator<T> reverseOrder();

    返回一个比较器,它强行逆转实现了Comparable接口的对象

       collection的自然顺序。

 

2,public static<T> Comparator<T> reverseOrder(Comparator<T> cmp)

   返回一个比较器,它强行逆转指定比较器的顺序。

*/

import java.util.*;

 

//自定义比较器,根据字符串的长度排序

class StrLenComparator implementsComparator<String>

{

       publicint compare(String s1,String s2)

       {

              intnum = new Integer(s1.length()).compareTo(new Integer(s2.length()));

 

              if(num==0)

                     returns1.compareTo(s2);

 

              returnnum;

       }

}

 

class Demo5

{

       publicstatic void main(String[] args)

       {

              method2();

       }

 

       //比较器,自然反转

       publicstatic void method1()

       {

              TreeSet<String>al = new TreeSet<String>(Collections.reverseOrder());

 

              al.add("abc");

              al.add("w");

              al.add("w");

              al.add("aeeec");

              al.add("mma");

             

              sop(al);        

       }

 

//以前定义好的比较器,不要改,我们现在按照指定比较器的逆序

       publicstatic void method2()

       {

              TreeSet<String>al = new TreeSet<String>(Collections.reverseOrder(newStrLenComparator()));

 

              al.add("abc");

              al.add("w");

              al.add("w");

              al.add("aeeec");

              al.add("mma");

             

              sop(al);        

       }

 

       publicstatic void sop(Object obj)

       {

              System.out.println(obj);

       }

}

集合(Collections--SynList)

 

/**

1,public static voidswap(List<?> list,int i,int j)

       在指定的列表的指定位置处交换元素

 

2,public static voidshuffle(List<?> list)

       使用默认随机源对指定列表进行置换

 

       publicstatic void shuffle(List<?> list,Random rnd );

       使用指定的随机源对指定的列表进行置换。

 

3、为了解决多线程问题

       1,publicstatic<T> Collection<T> synchronizedCollection(Collection<T>c)

              返回指定collection支持的同步(线程安全的)Collection

 

       2,publicstatic<T> List<T> synchronizedList(List<T> list);

              返回指定列表支持的同步(线程安全的)列表

      

       3,publicstatic<K,V>  Map<K,V>synchronizedMap(Map<K,V> m);

              返回由指定映射支持的同步(线程安全的)映射。

 

       4、publicstatic<T> Set<T> synchronizedSet(Set<T> s);

              返回指定Set支持的同步(线程安全的)set;

      

*/

 

集合(Arrays)

1、Arrays:用于操作数组的工具类,里面的所有的方法都是静态的。

2、/**

3、1、toString(数组)  :返回指定数组内容的字符串

4、 

5、2、pulbic  static<T> List<T> asList(T   a)

6、        将数组变成List集合

7、

8、 好处:可以使用集合的思想和方法来操作数组中的元素

9、 注意:

10、                  1、将数组变成集合,不可以使用集合的增删方法,因为数组的长度是固定的。

11、                  2、如果数组中的元素都是对象,那变成集合时,数组中的元素就直接转换为集合中的元素

12、                       如果数组中的元素是基本的数据类型,那么会将数组作为集合中的元素存在。

13、            

14、           */

15、           import java.util.*;

16、            

17、           class Demo8

18、           {

19、                  public static void main(String[] args)

20、                  {

21、                         String[] arr={"lili","ddd","ddd"};

22、            

23、                         //使用toString,将数组转换为字符串

24、                         sop(Arrays.toString(arr));

25、                        

26、                         //[[I@1fb8ee3],作为数组

27、                         int[] arr1= {1,3,4};

28、                         List<int[] > list1 =Arrays.asList(arr1);

29、                         sop(list1); 

30、                        

31、                         //[1, 2, 3]  ,作为集合中元素

32、                         Integer[] arr2={1,2,3};

33、                         List<Integer> list2 =Arrays.asList(arr2);

34、                         sop(Arrays.asList(list2));

35、            

36、                  }

37、            

38、                  public static void sop(Object obj)

39、                  {

40、                         System.out.println(obj);

41、                  }

42、           }

 

 

 

 

集合(集合转换为数组)

/**

1、将集合转换为数组的方法:

       Collection接口中的toArray()方法:

              1,Object[]toArray() 

              2,<T>T[] toArray(T[] a)

 

2、好处:  

       为了限制对元素的操作,不需要进行增删。

 

3、指定类型的数组到底要多长呢?

       1,当指定类型的数组的长度小于或者等于集合的size的时候,

              那么该方法的内部会创建一个新的数组,数组的长度就是集合的size

 

       2,当指定类型的数组的长度大于集合的size的时候,

              那么该方法的内部会创建一个新的数组,而数组的长度就是指定的长度

      

       综上,我们创建一个数组的时候,最好让数组最优,那么让数组的长度为集合的size

 

*/

import java.util.*;

 

class Demo9

{

       publicstatic void main(String[] args)

       {     

              ArrayList<Integer>list = new ArrayList<Integer>();

 

              list.add(1);

              list.add(2);

              list.add(3);

              list.add(4);

 

              Integer[]m=list.toArray(new Integer[list.size()]);

              sop(Arrays.toString(m));

       }     

 

       publicstatic void sop(Object obj)

       {

              System.out.println(obj);

       }}

集合(增强for循环)

/**

1、Collection实现了接口Iterable<T>,使得我们在取出元素的时候,可以使用高级for循环

2、Iterable接口

       1,实现这个接口允许对象成为“foreach“语句的目标

       2,有Iterator<T>iterator()方法

3,注意:出现在1.5版本以后,

     注意:集合不能是map ,

        注意:必须指定泛型

 

4,高级for循环的格式:

       for(数据类型 变量名 : 被遍历的集合(Collection)或者数组) 

       {

             

       }

 

5、迭代器和高级for循环的区别:

       高级for循环只能够对集合的元素进行获取,但是不能对集合进行操作

 

       迭代器除获取之外,还可以进行remove集合中的元素,

       如果是ListIterator,还可以在集合中遍历的过程中进行增删改查

 

6、传统的for循环和高级的for循环的区别

        高级for 循环有一个局限性,就是必须有被遍历的目标

 

        建议在遍历数组的时候,使用的for循环,因为传统的for循环可以定义角标

 

 

*/

import java.util.*;

 

class Test1

{

       publicstatic void main(String[] args)

       {

              //示例1:

 

              ArrayList<String>al = new ArrayList<String>();

 

              al.add("java01");

              al.add("java02");

              al.add("java03");

              al.add("java04");

 

              //使用迭代器遍历集合

              for(Iterator<String>it = al.iterator();it.hasNext();)

              {

                     System.out.println(it.next());

              }

             

              System.out.println("---------------------");

 

              //使用高级for循环

              for(Stringstr : al)

              {

                     System.out.println(str);                  

              }

 

              System.out.println("---------------------");

 

              //示例2:

 

              int[]arr = {1,2,3,4};

             

              //传统的for循环的迭代

              for(intx =0;x<arr.length;x++)

              {

                     System.out.println(arr[x]);

              }

 

              System.out.println("---------------------");

 

              //使用高级for循环

              for(inta : arr)

              {

                     System.out.println(a);

              }     

 

              System.out.println("---------------------");

 

              //示例3:

 

              HashMap<Integer,String>hm = new HashMap<Integer,String>();

 

              hm.put(1,"java01");

              hm.put(2,"java02");

              hm.put(3,"java03");

             

              //使用keySet

              Set<Integer>keySet =hm.keySet();

              for(Integeri : keySet)

              {

                     System.out.println(i+"..."+hm.get(i));

              }

 

              System.out.println("---------------------");

 

              //使用entrySet

              Set<Map.Entry<Integer,String>>entrySet = hm.entrySet();

              for(Map.Entry<Integer,String>me : entrySet)

              {

                     System.out.println(me.getKey()+"..》》》》"+me.getValue());

              }

       }

}

集合(可变参数)

/**

JDK1.5新特性:

1、方法的可变参数: ...

2、概述:其实就是建立数组这一个工程封装了起来,

               不用每一次手动的建立数组对象,只要将要操作的元素作为参数传递即可。

                        隐式的将这些参数封装为数组。

*/

class Test2

{

       publicstatic void main(String[] args)

       {

              show("jjj",1,2,3,4);

              show("lllddd",2);

       }

 

       //可变参数 

       publicstatic void show(String str,int... arr)

       {

              System.out.println(arr.length);     }}

 

集合(静态导入)

/**

JDK1.5的新特性:

 

StaticImport : 静态导入

 

格式:

import static java.util.Arrays.*;

 

注意:当类名重名的时候,需要指定具体的包名。

        当方法重名的时候,需要指定具体的类名或者对象

 */

 

import static java.util.Arrays.*;  //导入Arrays类中的所有的静态成员

import static java.lang.System.*; //导入System这个类中的所有静态成员

 

class Test3

{

       publicstatic void main(String[] args)

       {

              int[]arr={1,2,3};

 

              out.println(Arrays.toString(arr));//这就是好处咯

 

              sort(arr);//省略了Arrays

 

       //    out.println(Arrays.toString(arr)); //这个不能胜率,因为Object类中也有toString方法

 

       }

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值