Collections工具类和泛型     

Collections和Collection

Collections和Collection是不同的,Collections是工具类,用来操作集合的,而Collection是集合接口。Collections中有一系列的静态方法用来操作集合,但是不能更改集合内容。比如不能set()不能remove()元素,可以替换全部元素或者添加同一个元素。

 

static List<String> list =Arrays .asList("one Two three Four five six one".split( " "));//Two 和Four有意为之~
 
   
           public static void main (String[] args) {
 
   
                    // TODO Auto-generated method stub
 
   
System.out .println (list)
 
   
System.out .println ("max="+ Collections. max( list));
 
   
System.out .println ("min="+ Collections. min( list));
 
   
System.out .println ("忽略大小写max="+ Collections. max(
 
   
                    list, String. CASE_INSENSITIVE_ORDER));
 
   
System.out .println ("忽略大小写min="+ Collections. min(
 
   
                    list, String. CASE_INSENSITIVE_ORDER));
 
   
 
 
   
List<String > sublist =Arrays .asList("Four five six" .split (" "))
 
   
System.out .println ("indexOfSublist=="+
 
   
                                      Collections .indexOfSubList(list , sublist ));
 
   
System.out .println ("lastIndexOfSubList=="+
 
   
                                      Collections .lastIndexOfSubList (list, sublist))
 
   
          Collections .replaceAll(list , "one", "YO")
 
   
 
 
   
          System .out.println( "replace"+list );
 
   
          Collections .reverse(list );
 
   
System.out .println ("resever"+ list);
 
   
          Collections .shuffle(list ,new Random(47 ));
 
   
System.out .println ("shuffle"+ ""+list)
 
   
 
 
   
List<String > source =Arrays .asList("dong ting lake" .split (" "))
 
   
          Collections .copy(list , source );
 
   
System.out .println ("copy"+ ""+list)
 
   
          Collections .fill(list , "pop");
 
   
System.out .println ("fill"+ ""+list)
 
   
System.out .println ("frequency"+
 
   
                   Collections .frequency(list , "pop"));
 
   
 
 
   
           }
 
   
 
 
   
}
 
   
/*[one, Two, three, Four, five, six, one]
 
   
max=three
 
   
min=Four
 
   
忽略大小写max=Two
 
   
忽略大小写 min=five
 
   
indexOfSublist==3
 
   
lastIndexOfSubList==3
 
   
replace[YO, Two, three, Four, five, six, YO]
 
   
resever[YO, six, five, Four, three, Two, YO]
 
   
shuffle[three, five, six, YO, Four, Two, YO]
 
   
copy[dong, ting, lake, YO, Four, Two, YO]
 
   
fill[pop, pop, pop, pop, pop, pop, pop]
 
   
frequency7*/

 

泛型

泛型应用在泛型接口,泛型类和泛型方法中,主要作用是使参数类型化,将运行时参数不匹配错误转移到编译时期,避免强制类型转换。

泛型接口比较好理解,就是说这个接口的实现类有许多的类型,再写这个类的时候,并不知道会有什么类型的类来实现这个接口。

interface Solution< T> {
 
   
           public void say ();
 
   
}

实现这个接口的类可以是泛型的,也可以是具体类型的,比如下面这个类是具体类型的,String。

class GenericTest implements Solution< String> {
 
   
 
 
   
           public static void main ( String[] args ) {
 
   
Solution <String > gt = new GenericTest ();

实现这个接口的类依然是泛型的类。

class GenericTest< T> implements Solution <T >{
 
    
 
 
    
           public static void main ( String[] args ) {
 
    
Solution <String > gt = new GenericTest <String >()
 
    
Solution <Integer > ss= new GenericTest <Integer >()
 
    
泛型类和泛型方法
 
    
//泛型类
 
     
class Solution3< V, T> {
 
     
           //利用泛型的构造函数
 
     
           public Solution3( V name, T other ) {
 
     
                    super ();
 
     
                    this. name = name;
 
     
                    this. other = other;
 
     
           }
 
     
           public V getName() {
 
     
                    return name
 
     
           }
 
     
           public void setName (V name ) {
 
     
                    this. name = name;
 
     
           }
 
     
           public T getOther() {
 
     
                    return other
 
     
           }
 
     
           public void setOther (T other ) {
 
     
                    this. other = other;
 
     
           }
 
     
          V name;
 
     
          T other;
 
     
}
 
     
public class GenericC {
 
     
 
 
     
           public static void main ( String[] args ) {
 
     
                    //由于这个类的构造方法用到了泛型,那么我们可以在泛型范围内
 
     
                    //使用不同的类型来构造对象,没有使用泛型之前你可能无法这么做
 
     
                    //String,String类型的构造函数
 
     
Solution3 <String ,String > ss = new Solution3 <String , String >( "方案" , "目标" );
 
     
                    //String,integer类型的构造函数
 
     
Solution3 <String ,Integer > si = new Solution3 <String , Integer >( "方案" , 3 );
 
     
System .out .println ( "String+Sting"+ ss. getName ()+ ss. other)
 
     
System .out .println ( "String+Integer"+ si. getName ()+ si. other)
 
     
/*String+Sting方案目标
 
     
String+Integer方案3*/

使用通配符

通配符"?"

class Jiangyou{
 
      
}
 
      
class Solution5< T> {
 
      
          T t;
 
      
           public Solution5( T t ) {
 
      
                    super ();
 
      
                    this. t = t;
 
      
           }
 
      
}
 
      
public class GenericD {
 
      
           //一个使用了泛型类的对象的方法,将泛型对象定义为通配符“?”
 
      
public static void function ( Solution5<?> so ){
 
      
          System . out. println( "方案: " +so .t );
 
      
}
 
      
           public static void main ( String[] args ) {
 
      
                    //通配符"?"的权限很大
 
      
                   Solution5 <String > so5 = new Solution5 <String >( "目标" );
 
      
                    //可以是Number基本数据类型
 
      
                   Solution5 <Number > so7 = new Solution5 <Number >( 6 );
 
      
                    //甚至是Object,这里用了个酱油类
 
      
                   Solution5 <Jiangyou > so6 = new Solution5 <Jiangyou >( new Jiangyou()) ;  
 
      
                    function( so5 );
 
      
                    function( so6 );
 
      
                    function( so7 );
 
      
/*                方案: 目标
 
      
                   方案: Jiangyou@4e515669
 
      
                   方案: 6*/
 
      
上限限定
 
      
           //一个使用了泛型类的对象的方法,将泛型类型上限定为Number的类型
 
       
public static void function ( Solution5<? extends Number> so ){
 
       
          System . out. println( "方案: " +so .t );
 
       
}
 
       
           public static void main ( String[] args ) {
 
       
                    //Number及其子类都可以调用function方法
 
       
                   Solution5 <Integer > solution = new Solution5 <Integer >( 3 );
 
       
                   Solution5 <Double > solution1 = new Solution5 <Double >( 3. 0 );    
 
       
                    //String类型的Solution无法调用function方法
 
       
                   Solution5 <String > solution2= new Solution5< String> ("mubiao" );                    
 
       
                    function( solution );
 
       
                    function( solution1 );
 
       
/*                方案: 3
 
       
                   方案: 3.0*/
 
       
下限限定
 
        
//一个使用了泛型类的对象的方法,将泛型类型下限定为Integer的类型
 
        
public static void function ( Solution5<? super Integer> so ){
 
        
          System . out. println( "方案: " +so .t );
 
        
}
 
        
           public static void main ( String[] args ) {
 
        
                    //Integer及其父类都可以调用function方法
 
        
                   Solution5 <Integer > solution = new Solution5 <Integer >( 3 );
 
        
                    //Double不是Integer的父类无法调用function方法
 
        
                   Solution5 <Double > solution1= new Solution5< Double> (3 .0 );    
 
        
                    //Object类型的Solution调用function方法
 
        
                   Solution5 <Object > solution2 = new Solution5 <Object >( "mubiao" );                    
 
        
                    function( solution );
 
        
                    //function(solution1);
 
        
                    function( solution2 );
 
        
/*                方案: 3
 
        
mubiao*/