黑马程序员 java基础集合框架之集合工具类Collections
2014-12-03 13:01
579 查看
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
Collections概述
有时候List集合中存入了一堆元素,而我们又想对List集合进行排序,我们知道只有TreeSet,或者TreeMap才有排序的功能。而List集合本身没有排序的功能,这时我们就需要使用Collections这个工具类来完成排序这个功能。
Collections常用的方法
static <T> void sort(List<T> list) 根据元素的自然顺序对指定的集合进行排序(也就是T必须自身实现Comparable接口)
static <T> void sort(List<T>list,Comparator<? super T>) 根据指定的比较器然后对指定的集合进行排序,(T本身不一定要实现Comparable接口)
static <T> T max(List<T> list)根据元素的自然顺序排序后获取最大的那个值。
static<T> T max(List<T>list,Comparator<? super T>)根据比较器的排序,然后返回集合中最大的那个值。
static <T> T min(List<T> list)根据元素的自然顺序排序后获取最小的那个值。
static<T> T min(List<T>list,Comparator<? super T>)根据比较器的排序,然后返回集合中最小的那个值。
static<T>int binarySearch(List<T> list, T t) 先按照元素默认的顺序进行排序,排序完成后,然后再根据二分查找法搜索制定的列表,如果列表中包含这个指定的元素,那么就返回该元素在列表中的索引值,如果列表不包含这个元素,那么就返回该元素在该列表中要插入的地方的索引值的相反数再减去一。
static <T>int binarySearch(list<T>list, T key,Comparator<? super T >)先根据比较器进行排序。完成排序后,然后再根据二分查找法搜索制定的列表,如果列表中包含这个指定的元素,那么就返回该元素在列表中的索引值,如果列表不包含这个元素,那么就返回该元素在该列表中要插入的地方的索引值的相反数再减去一。
static <T> void fill<List<T> list, T t) 使用指定的元素可以替换集合中所有的元素。
static <T> boolean replaceAll(List<T》 t, T oldvalue, T newValue)使用新的元素,替换集合中指定的所有的元素的值。
static <T> void reverse() 用于翻转集合中的元素,
static <T> Comparator<T> reverseOrder() 返回一个比较器,它强行逆转实现了Comparable 接口的对象 collection 的自然顺序。
static <T> Comparator<T> reverseOrder(Comparator<T> comp)返回一个比较器,它强行逆转了指定比较器的顺序。
Collections实例一:
需求:使用Collections对学生进行排序。1方法一:让学生自身实现Comparable接口,具备比较性,2方法二:自定义一个比较器,然后根据比较器的规则,对学生进行排序,代码如下:
Collections实例二
Collections实例四
需求:使用Collections.binarySearch()方法查找List集合中指定字符串的索引下标。
Collections中的其它几个重要方法:
Collections中有两个重载方法reverseOrder()和reverseOrder(Comparator),reverserOrder()这个方法可以强制的逆转集合中元素自身默认的比较顺序。而reverseOrder(Comparator())这个方法可以强制的逆转比较器的比较的顺序。
Collections实例六
Collections集合工具类中的另外几个重要的方法,
我们大家都知道,集合很高效,但是高效的同时也存在着安全隐患,通过查阅java帮助文档我们发现List,Set,Map集合中等多个都是线程不安全的,这样如果在多线程中操作集合,那么就有可能出现安全隐患,而Collections集合工具类帮我们提供了synchroziedCollection(), synchronizedList(),synchronizedSet(),synchronizedMap()方法帮我们返回线程安全的集合,另外swap()方法可以置换集合中两个指定位置的元素。而shuffle()这个方法可以使用默认的随机源,帮我们随机的置换列表中的位置。运用这个方法我们在生成一张扑克牌或者筛子的随机点数使用这个方法。
代码如下:
Collections概述
有时候List集合中存入了一堆元素,而我们又想对List集合进行排序,我们知道只有TreeSet,或者TreeMap才有排序的功能。而List集合本身没有排序的功能,这时我们就需要使用Collections这个工具类来完成排序这个功能。
Collections常用的方法
static <T> void sort(List<T> list) 根据元素的自然顺序对指定的集合进行排序(也就是T必须自身实现Comparable接口)
static <T> void sort(List<T>list,Comparator<? super T>) 根据指定的比较器然后对指定的集合进行排序,(T本身不一定要实现Comparable接口)
static <T> T max(List<T> list)根据元素的自然顺序排序后获取最大的那个值。
static<T> T max(List<T>list,Comparator<? super T>)根据比较器的排序,然后返回集合中最大的那个值。
static <T> T min(List<T> list)根据元素的自然顺序排序后获取最小的那个值。
static<T> T min(List<T>list,Comparator<? super T>)根据比较器的排序,然后返回集合中最小的那个值。
static<T>int binarySearch(List<T> list, T t) 先按照元素默认的顺序进行排序,排序完成后,然后再根据二分查找法搜索制定的列表,如果列表中包含这个指定的元素,那么就返回该元素在列表中的索引值,如果列表不包含这个元素,那么就返回该元素在该列表中要插入的地方的索引值的相反数再减去一。
static <T>int binarySearch(list<T>list, T key,Comparator<? super T >)先根据比较器进行排序。完成排序后,然后再根据二分查找法搜索制定的列表,如果列表中包含这个指定的元素,那么就返回该元素在列表中的索引值,如果列表不包含这个元素,那么就返回该元素在该列表中要插入的地方的索引值的相反数再减去一。
static <T> void fill<List<T> list, T t) 使用指定的元素可以替换集合中所有的元素。
static <T> boolean replaceAll(List<T》 t, T oldvalue, T newValue)使用新的元素,替换集合中指定的所有的元素的值。
static <T> void reverse() 用于翻转集合中的元素,
static <T> Comparator<T> reverseOrder() 返回一个比较器,它强行逆转实现了Comparable 接口的对象 collection 的自然顺序。
static <T> Comparator<T> reverseOrder(Comparator<T> comp)返回一个比较器,它强行逆转了指定比较器的顺序。
Collections实例一:
需求:使用Collections对学生进行排序。1方法一:让学生自身实现Comparable接口,具备比较性,2方法二:自定义一个比较器,然后根据比较器的规则,对学生进行排序,代码如下:
import java.util.*; class JavaCollection1_33 { public static void main(String[] args) { //1让学生自身具备比较性 ArrayList<Student> list=new ArrayList<Student>(); list.add(new Student("zhangsan",20)); list.add(new Student("lisi",23)); list.add(new Student("wangwu",26)); list.add(new Student("zhaoliu",23)); Collections.sort(list); for(int i=0;i<list.size();i++) { Student s=list.get(i); sop(s.getName()+":"+s.getAge()); } //2创建一个比较器,然后再进行比较。 sop("***********************"); Collections.sort(list,new MyComp()); for(int i=0;i<list.size();i++) { Student s=list.get(i); sop(s.getName()+":"+s.getAge()); } } public static void sop(Object obj) { System.out.println(obj); } } //自定义比较器:按照学生姓名的字符长短进行排序 class MyComp implements Comparator<Student> { public int compare(Student s1,Student s2) { int num=s1.getName().length()-s2.getName().length(); if(num==0) { return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge())); } return num; } } //方法1,让学生对象自身具备比较性,实现Comparable接口 class Student implements Comparable<Student> { private String name; private int age; Student(String name,int age) { this.name=name; this.age=age; } public String getName() { return name; } public int getAge() { return age; } public int compareTo(Student t) { int num=this.age-t.age; if(num==0) { return this.name.compareTo(t.getName()); } return num; } }运行结果:
Collections实例二
import java.util.*; class JavaCollection1_34 { public static void main(String[] args) { maxAndMinDemo(); sop("\n\n"); maxAndMinDemo2(); } public static void maxAndMinDemo() { ArrayList<String> al=new ArrayList<String>(); al.add("c"); al.add("bbc"); al.add("a"); al.add("acdc"); al.add("abc"); sop("排序前:\n"); for(Iterator<String> it=al.iterator();it.hasNext();) { sop(it.next()); } Collections.sort(al); sop("\n排序后:\n"); for(Iterator<String> it=al.iterator();it.hasNext();) { sop(it.next()); } sop("\n最大的数:"+Collections.max(al)); sop("\n最小的数:"+Collections.min(al)); } //自定义比较器 public static void maxAndMinDemo2() { ArrayList<String> al=new ArrayList<String>(); al.add("c"); al.add("bbc"); al.add("a"); al.add("acdc"); al.add("abc"); sop("排序前:\n"); for(Iterator<String> it=al.iterator();it.hasNext();) { sop(it.next()); } Collections.sort(al,new MyComp()); sop("\n排序后:\n"); for(Iterator<String> it=al.iterator();it.hasNext();) { sop(it.next()); } sop("\n最大的数:"+Collections.max(al,new MyComp())); sop("\n最小的数:"+Collections.min(al,new MyComp())); } public static void sop(Object obj) { System.out.print(obj+" "); } } class MyComp implements Comparator<String> { public int compare(String s1,String s2) { int num=s1.length()-s2.length(); if(num==0) { return s1.compareTo(s2); } return num; } }运行结果:
Collections实例四
需求:使用Collections.binarySearch()方法查找List集合中指定字符串的索引下标。
import java.util.*; class JavaCollection1_35 { public static void main(String[] args) { ArrayList<String>list=new ArrayList<String>(); list.add("afda"); list.add("zz"); list.add("bbb"); list.add("dfljka"); list.add("a"); list.add("zzz"); Collections.sort(list); for(int i=0;i<list.size();i++) { System.out.println(list.get(i)); } System.out.println("查找的索引是:"+halfSerch(list,"zzz")); //这里是通过String对象的默认方法进行排序的。 System.out.println("通过Collections的binarySearch方法查找得到的结果是:"+Collections.binarySearch(list,"zzz")); //同理我们也可以自定义一个比较器, Collections.sort(list,new MyComp()); System.out.println("按照字符串的长度排序后得到的结果是:\n"); for(int i=0;i<list.size();i++) { System.out.println(list.get(i)); } System.out.println("通过Collections的binarySearch方法并自定义比较器查找得到的结果是:"+Collections.binarySearch(list,"zzz",new MyComp())); } //二分查找实例原理 public static int halfSerch(List<String> list,String key) { int max=list.size()-1; int min=0; int mid; while(min<=max) { mid=(max+min)>>1; String value=list.get(mid); if(value.compareTo(key)>0) { max=mid-1; } else if(value.compareTo(key)<0) { min=mid+1; } else return mid; } return -min-1; } }运行结果:
Collections中的其它几个重要方法:
import java.util.*; class JavaCollection1_36 { public static void main(String[] args) { ArrayList<String> al=new ArrayList<String>(); al.add("aaaa"); al.add("b"); al.add("bcd"); al.add("adb"); al.add("daf"); al.add("dfsa"); System.out.println("集合最初的内容:"); printList(al); //fill()使用指定元素替换指定列表中的所有元素。 System.out.println("集合替换后的内容:"); //Collections.fill(al,"ccc"); printList(al); //replace()方法可以使用新的值替换旧 b01a 的值.(替换集合中所有的元素); Collections.replaceAll(al,"aaaa","bbbb"); printList(al); System.out.println("使用reverse方法"); Collections.reverse(al); printList(al); fillText(al,2,5,"fff"); } public static void printList(List<String> list) { for(int i=0;i<list.size();i++) { System.out.println(list.get(i)); } } //练习:使用fill方法替换集合中部分的元素 public static void fillText(List<String> list,int start,int end,String value) { List<String> sublist=list.subList(start,end);//注意这里是使用List接口的subList()方法返回集合的一个子视图,注意对这个视图的操作也回同样影响基集合的结果。 Collections.fill(sublist,value); sop(list); } public static void sop(Object obj) { System.out.println(obj); } }运行结果:
Collections中有两个重载方法reverseOrder()和reverseOrder(Comparator),reverserOrder()这个方法可以强制的逆转集合中元素自身默认的比较顺序。而reverseOrder(Comparator())这个方法可以强制的逆转比较器的比较的顺序。
Collections实例六
import java.util.*; class JavaCollection1_37 { public static void main(String[] args) { //使用Collections.reverseOrder()方法强行的逆转了元素比较的顺序,本来是从小到大的顺序,现在逆转后变成了从大到小的顺序了 TreeSet<Student> al=new TreeSet<Student>(Collections.reverseOrder()); al.add(new Student("zhangsan",21)); al.add(new Student("lisi",34)); al.add(new Student("wangwu",35)); al.add(new Student("zhaoliu",21)); al.add(new Student("baba",65)); for(Iterator<Student>it=al.iterator();it.hasNext();) { Student s=it.next(); sop(s.getName()+":"+s.getAge()); } sop("*******************************"); //Collections.reverserOrder()方法有一个重载方法,这个方法强行的逆转了指定比较器的比较顺序。 TreeSet<Student> al2=new TreeSet<Student>(Collections.reverseOrder(new MyComp())); al2.add(new Student("zhangsan",21)); al2.add(new Student("lisi",34)); al2.add(new Student("lisi",64)); al2.add(new Student("wangwu",35)); al2.add(new Student("zhaoliu",21)); al2.add(new Student("baba",65)); for(Iterator<Student>it=al2.iterator();it.hasNext();) { Student s=it.next(); sop(s.getName()+":"+s.getAge()); } } public static void sop(Object obj) { System.out.println(obj); } } class MyComp implements Comparator<Student> { public int compare(Student s1,Student s2) { int num=s1.getName().compareTo(s2.getName()); if(num==0) { return s1.getAge()-s2.getAge(); } return num; } } //让学生自身具备了比较性,先比较年龄,在比较年龄。从小到大 class Student implements Comparable<Student> { private String name; private int age; Student(String name,int age) { this.name=name; this.age=age; } public String getName() { return name; } public int getAge() { return age; } public int compareTo(Student s) { int num=this.age-s.getAge(); if(num==0) { return this.name.compareTo(s.getName()); } return num; } }运行结果如下:
Collections集合工具类中的另外几个重要的方法,
我们大家都知道,集合很高效,但是高效的同时也存在着安全隐患,通过查阅java帮助文档我们发现List,Set,Map集合中等多个都是线程不安全的,这样如果在多线程中操作集合,那么就有可能出现安全隐患,而Collections集合工具类帮我们提供了synchroziedCollection(), synchronizedList(),synchronizedSet(),synchronizedMap()方法帮我们返回线程安全的集合,另外swap()方法可以置换集合中两个指定位置的元素。而shuffle()这个方法可以使用默认的随机源,帮我们随机的置换列表中的位置。运用这个方法我们在生成一张扑克牌或者筛子的随机点数使用这个方法。
代码如下:
import java.util.*; class JavaCollection1_38 { public static void main(String[] args) { ArrayList<String> al=new ArrayList<String>(); al.add("xg"); al.add("hj"); al.add("wmz"); al.add("xiaoming"); al.add("zhangsan"); System.out.println(al); Collections.swap(al,2,3); System.out.println(al); Collections.shuffle(al); System.out.println(al); } }
相关文章推荐
- 黑马程序员——Java语言基础:集合框架(Collection、Map,工具类Collections、Arrays)
- 黑马程序员——Java基础--集合框架工具类:Collections、Arrays、其他对象
- 黑马程序员——Java基础---泛型、集合框架工具类:Collections和Arrays、JDK 1.5新特性
- Java基础---泛型、集合框架工具类:collections和Arrays (黑马程序员)
- 黑马程序员 java 基础 毕向东 面向对象 集合框架 工具类 Collections and Arrays
- 黑马程序员——Java基础---泛型、集合框架工具类:collections和Arrays
- 黑马程序员——Java基础---泛型、集合框架工具类:collections和Arrays
- 黑马程序员——Java基础---泛型、集合框架工具类:collections和Arrays
- 黑马程序员——Java之集合框架工具类Collections、Arrays以及Java新特性
- 黑马程序员—java基础学习--Map集合、Collections,Arrays工具类
- 黑马程序员————Java基础之集合框架工具类
- 黑马程序员-----------JAVA基础---------集合框架工具类
- 黑马程序员_Java基础_集合框架工具类
- 黑马程序员_Java第17天知识总结_集合框架的工具类_Collections_Arrays_将数组变成list集合_集合变数组_高级for循环_可变参数
- 黑马程序员——Java基础---泛型、集合框架工具类
- 黑马程序员-JAVA基础-Java 集合之Collections 工具类
- Java基础---泛型、集合框架工具类:collections和Arrays
- 黑马程序员--java基础复习集合框架工具类及其他对象
- 黑马程序员——JAVA基础——集合---概述、Collection中共性方法、List、Map、工具类Collections和Arrays
- 黑马程序员——Java基础知识——集合框架、集合工具类、Arrays