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*/
泛型