集合操作类Collections和Arrays,JDK1.5新特性
2013-09-11 21:00
260 查看
1----------------------------------------------------------------
/*
java.util.包
1.Collections 这个类是Collection的一个操作类
2.Arrays 这个类是数组的操作类,,这两个类中所有的方法都是static
Collections下常用方法
1.sort
1.1 public static void sort(List list)
作用:对List集合中的元素按照元素的自然顺序排序。
1.2 public static void sort(List list,Comparator c);
作用:对List集合中的元素按照指定的比较器c进行排序.
2.max min
2.1 max
2.1.1
public static T max(Collection<? extends T> c)
得到Collection中的元素的最大值(按元素的自然顺序)
2.1.2
public static T max(Collection c,Comparator)
得到collection中的元素的最大值(按指定的比较器)
2.2 min
2.2.1
public static T min(Collection<? extends T> c)
得到Collection中的元素的最小值(按元素的自然顺序)
2.2.2
public static T min(Collection c,Comparator)
得到collection中的元素的最小值(按指定的比较器)
3. reverse
定义 public static void reverse(List list);
作用 集合中的元素反转
4. shuffle
定义 public static void shuffle(List list);
作用 集合中的元素乱序
5. swap
定义 public static void swap(List list,int i,int j)
作用 将list集合中的i,j位置上的元素交换。
6. binarySearch
6.1 public static int binarySearch(List list, T key)
作用 使用二分法查找,key在list集合中的索引位置 元素的比较是使用元素的自然顺序。
如果存在,返回这个索引,如果不存在,返回(-(插入点)-1)
插入点就是key这个元素如果存入list集合中的位置。
6.2 public static int binarySearch(List list,T key,Comparator c)
作用 使用二分法查找,key在list集合中的索引位置 元素的比较使用的是c这个比较器
如果存在,返回这个索引,如果不存在,返回(-(插入点)-1)
插入点就是key这个元素如果存入list集合中的位置。
7. reverseOrder
7.1 public static Comparator reverseOrder() 得到集合元素的自然顺序的反序
7.2 public static Comparator reverseOrder(Comparator c)得到c这个比较器的反的比较器
*/
/*
Arrays 是数组的操作类
1.fill 作用是将数组中的元素填充成指定的值。
2.copyOf 作用是复制数组,可以复制数组中的一部分。
3.sort 作用是对数组中的元素进行排序
4.toString
5. asList
作用是将一个数组转换成集合
为什么这么做,因为集合中的操作方法更丰富。那我们就可以使用集合中的方法。
数组转换成集合后,不能使用集合的添加与删除操作,也就是说不能更改长度.
注意:
如果asList操作时 参数是一个数组,
如果数组中元素是引用类型,将这个数组中的所有元素直接放入到集合中
如果数组中元素是基本类型,那么是将这个数组做为元素放入到集合中。
*/
public static List synchronizedList(List list);
作用 将一个非同步的list集合变成同步的List
注意一下起名的规范
Xxx synchronizedXxx(Xxx x)
Xxx 可以代表 Collection List Set Map
它们的作用都是将不同步的集合变成同步的。
2--------------------------------------------------------------------------------------
3.-----------------------------------------------------------------------------------------------
Collections-reverseOrder
4.-----------------------------------------------------------------------------------
5.-------------------------------------------------------------------------
/*jdk1.5 新特性
* 高级for循环
* 格式:
* for(数据类型 变量名:被遍历的集合(Collection)或者数组)
* {
* }
* 对集合进行遍历
* 只能获取集合元素,但是不能对集合进行操作
* 迭代器除了遍历,还可以进行remove集合中元素的动作
* 如果是用ListIterator,还可以在遍历过程中对集合进行增删改查的动作
*
* 传统for循环和高级for的区别
* 高级for有一个局限性,必须有被遍历的目标
* 建议在遍历数组的时候还是使用传统for
jdk1.5 新特性
静态导入
简化写法
在应用时,如果方法名重复,那么通过参数是可以区分,
如果区分不开,那么这时必须指定是哪个类中的方法。
jdk1.5 新特性
动态参数
动态参数写法 类型...
动态参数注意:动态参数后面不要在写其它参数,如果有的话,写在动态参数前面。
/*
java.util.包
1.Collections 这个类是Collection的一个操作类
2.Arrays 这个类是数组的操作类,,这两个类中所有的方法都是static
Collections下常用方法
1.sort
1.1 public static void sort(List list)
作用:对List集合中的元素按照元素的自然顺序排序。
1.2 public static void sort(List list,Comparator c);
作用:对List集合中的元素按照指定的比较器c进行排序.
2.max min
2.1 max
2.1.1
public static T max(Collection<? extends T> c)
得到Collection中的元素的最大值(按元素的自然顺序)
2.1.2
public static T max(Collection c,Comparator)
得到collection中的元素的最大值(按指定的比较器)
2.2 min
2.2.1
public static T min(Collection<? extends T> c)
得到Collection中的元素的最小值(按元素的自然顺序)
2.2.2
public static T min(Collection c,Comparator)
得到collection中的元素的最小值(按指定的比较器)
3. reverse
定义 public static void reverse(List list);
作用 集合中的元素反转
4. shuffle
定义 public static void shuffle(List list);
作用 集合中的元素乱序
5. swap
定义 public static void swap(List list,int i,int j)
作用 将list集合中的i,j位置上的元素交换。
6. binarySearch
6.1 public static int binarySearch(List list, T key)
作用 使用二分法查找,key在list集合中的索引位置 元素的比较是使用元素的自然顺序。
如果存在,返回这个索引,如果不存在,返回(-(插入点)-1)
插入点就是key这个元素如果存入list集合中的位置。
6.2 public static int binarySearch(List list,T key,Comparator c)
作用 使用二分法查找,key在list集合中的索引位置 元素的比较使用的是c这个比较器
如果存在,返回这个索引,如果不存在,返回(-(插入点)-1)
插入点就是key这个元素如果存入list集合中的位置。
7. reverseOrder
7.1 public static Comparator reverseOrder() 得到集合元素的自然顺序的反序
7.2 public static Comparator reverseOrder(Comparator c)得到c这个比较器的反的比较器
*/
import java.util.*; class Demo2 { public static void main(String[] args) { new CollectionsDemo().method6(); } } class CollectionsDemo { //sort public void method1(){ /* //使用sort按照自然顺序排序 List<String> list=new ArrayList<String>(); list.add("abc"); list.add("aaa"); list.add("sdf"); list.add("rwds"); Collections.sort(list); System.out.println(list); */ List<Student> list=new ArrayList<Student>(); list.add(new Student(20)); list.add(new Student(21)); list.add(new Student(19)); Collections.sort(list, new Comparator<Student>(){ public int compare(Student s1,Student s2){ return s1.age-s2.age; } } ); System.out.println(list); } //max public void method2(){ /* //按照集合中的元素的自然顺序得到最大值 List<String> list=new ArrayList<String>(); list.add("abc"); list.add("aaa"); list.add("sdf"); list.add("rwds"); String max=Collections.max(list); System.out.println(max); */ List<Student> list=new ArrayList<Student>(); list.add(new Student(20)); list.add(new Student(21)); list.add(new Student(19)); Student max=Collections.max(list,new Comparator<Student>(){ public int compare(Student s1,Student s2){ return s1.age-s2.age; } }); System.out.println(max); } //reverse 反转 //shuffle 乱序 //swap 指定位置元素置换 public void method3(){ List<String> list=new ArrayList<String>(); list.add("abc"); list.add("aaa"); list.add("sdf"); list.add("rwds"); System.out.println(list); Collections.swap(list,1,2); System.out.println(list); //Collections.sort(list);//升序 //想要降序 //Collections.reverse(list);//将集合中的元素反转。 //System.out.println(list); //Collections.shuffle(list); //System.out.println(list); } public void method4(){ /* List<Integer> list=new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.add(6); //二分法查找 int index=Collections.binarySearch(list,5); System.out.println(index); */ List<Student> list=new ArrayList<Student>(); list.add(new Student(19)); list.add(new Student(20)); list.add(new Student(21)); int index=Collections.binarySearch(list,new Student(190), new Comparator<Student>(){ public int compare(Student s1,Student s2){ return s1.age-s2.age; } } ); System.out.println(index); } //reverseOrder(Comparator c) public void method5(){ //比较器对象 Comparator<String> c=new Comparator<String>(){ public int compare(String s1,String s2){ return s1.length()-s2.length(); } }; TreeSet<String> set=new TreeSet<String>(c); set.add("asdf"); set.add("dsdfe"); set.add("dsadfas"); set.add("fisudjsd"); //要按照元素的长度进行排序 System.out.println(set); //set这个集合的比较器是按照元素的长度进行排序 //得到比较器 Comparator<String> rc=Collections.reverseOrder(c); //得到c这个比较器的反转的操作 List<String> list=new ArrayList<String>(); list.add("dsadfas"); list.add("fisudjsd"); list.add("asdf"); list.add("dsdfe"); Collections.sort(list,rc); System.out.println(list); } //无参 public void method6(){ List<Student> list=new ArrayList<Student>(); list.add(new Student(20)); list.add(new Student(21)); list.add(new Student(19)); Collections.sort(list);//自然顺序,是按照年龄从小到大 System.out.println(list); Collections.sort(list,Collections.reverseOrder()); //将自然顺序逆序,简单说就是按照年龄从大到小. System.out.println(list); } } class Student implements Comparable<Student> { int age; public Student(int age){ this.age=age; } public String toString(){ return "年龄:"+age; } public int compareTo(Student s){ return age-s.age; } }
/*
Arrays 是数组的操作类
1.fill 作用是将数组中的元素填充成指定的值。
2.copyOf 作用是复制数组,可以复制数组中的一部分。
3.sort 作用是对数组中的元素进行排序
4.toString
5. asList
作用是将一个数组转换成集合
为什么这么做,因为集合中的操作方法更丰富。那我们就可以使用集合中的方法。
数组转换成集合后,不能使用集合的添加与删除操作,也就是说不能更改长度.
注意:
如果asList操作时 参数是一个数组,
如果数组中元素是引用类型,将这个数组中的所有元素直接放入到集合中
如果数组中元素是基本类型,那么是将这个数组做为元素放入到集合中。
*/
import java.util.*; class Demo4 { public static void main(String[] args) { method(); } //asList注意 public static void method(){ int[] arr={1,2,3}; List<int[]> list=Arrays.asList(arr); //发现集合中存储的不是Integer对象,是int[]类型 for(int i=0;i<list.size();i++){ int[] arr1=list.get(i); for(int j=0;j<arr1.length;j++){ System.out.println(arr1[j]); } } } //asList public static void method0(){ String[] s=new String[]{"a","b","c"}; //将数组转换成List List<String> list=Arrays.asList(s); //对于这些添加与删除的方法不支持.简单说不能更改长度,因为数组是定长的。 list.add("d"); //java.lang.UnsupportedOperationException System.out.println(list); } public static void method1(){ String[] s=new String[10]; Arrays.fill(s,"abc"); for(int i=0;i<s.length;i++){ System.out.println(s[i]); } } public static void method2(){ String[] s=new String[]{"a","b","c"}; //Arrays.fill(s,"abc"); String[] st=Arrays.copyOf(s,2); for(int i=0;i<st.length;i++){ System.out.println(st[i]); } } public static void method3(){ int[] arr={1,2,3,4,5,6}; System.out.println(Arrays.toString(arr)); } }集合体系 jdk1.2出现的集合类基本都是不同步
public static List synchronizedList(List list);
作用 将一个非同步的list集合变成同步的List
注意一下起名的规范
Xxx synchronizedXxx(Xxx x)
Xxx 可以代表 Collection List Set Map
它们的作用都是将不同步的集合变成同步的。
class Demo3 { public static void main(String[] args) { List list=new ArrayList(); //这个是不同步的 List slist=Collections.synchronizedList(list);//同步的。 } }
package cn.baidu.com; import java.util.*; /* * 集合框架的工具类 */ public class CollectionsDemo { public static void main(String[] args) { binarySearchDemo(); } public static void binarySearchDemo() { List<String> list =new ArrayList<String>(); list.add("aafasdfsdfsa"); list.add("bbdfa"); list.add("acdd"); list.add("sddfaxdd"); sop(list); Collections.sort(list); int index=Collections.binarySearch(list,"acdd"); sop("binarySearch查找的index="+index); int index1=halfSerach2(list,"acdd",new StrLenComparator()); sop("我的二分法查找到的index为:"+index); } public static void sortDemo() { List<String> list =new ArrayList<String>(); list.add("aafasdfsdfsa"); list.add("bbdfa"); list.add("acdd"); list.add("sddfaxdd"); sop(list); Collections.sort(list); sop(list); Collections.sort(list,new StrLenComparator()); sop(list); } public static void maxDemo() { List<String> list =new ArrayList<String>(); list.add("aafasdfsdfsa"); list.add("bbdfa"); list.add("acdd"); list.add("sddfaxdd"); sop(list); String max=Collections.max(list); sop("max="+max); String max1=Collections.max(list,new StrLenComparator()); sop("maxlen="+max1); } public static void sop(Object obj) { System.out.println(obj); } public static int halfSerach(List<String> list,String key) { int max,min,mid; max=list.size()-1; min=0; while(min<=max) { mid=(max+min)>>1; String str=list.get(mid);//根据索引查找中间数 int num=str.compareTo(key); if(num>0) max=mid-1; else if(num<0) min=mid+1; else return mid; } return -min-1;//返回-index-1 } //对象可能没有比较性,需要构造一个比较器 public static int halfSerach2(List<String> list,String key,Comparator<String> cmp) { int max,min,mid; max=list.size(); min=0; mid=(max+min)>>1; while(min<=max) { String str=list.get(mid);//根据索引查找中间数 int num=cmp.compare(str, key); if(num>0) max=mid-1; else if(num<0) min=mid+1; else return mid; } return -min-1;//返回-index-1 } } class StrLenComparator implements Comparator<String> { public int compare(String s1,String s2) { if(s1.length()>s2.length()) return 1; if(s1.length()<s2.length()) return -1; return s1.compareTo(s2); } }
2--------------------------------------------------------------------------------------
//数组变集合 import java.util.*; public class ArraysDemo { public static void main(String[] args) { //int[] arr={2,3,4}; //System.out.println(Arrays.toString(arr)); String[] arr={"dsf","err","wer"}; //把数组变成list集合有什么好处 /* * 可以使用集合的思想和方法来操作数组中的元素。 * 注意:将数组变成集合,不可以使用集合的增删方法,会产生UnsupportedOperationException, * 因为数组的长度是固定的 */ List<String> list=Arrays.asList(arr); sop(list); sop("contains="+list.contains("dsf")); sop(list); int[] num1={4,5,5,3,2}; List<int[]> li1=Arrays.asList(num1); sop(li1); Integer[] nums={4,5,5,3,2}; List<Integer> li=Arrays.asList(nums); sop(li); /* * 如果数组中的元素都是对象,那么变成集合时,数组中的元素就直接转成集合中的元素 * 如果数组中的元素都是基本数据类型,那么将该数组作为集合中的元素存在。 */ } public static void sop(Object obj) { System.out.println(obj); } public static boolean myContains(String[] arr,String key) { for(int x=0;x<arr.length;x++) { if(arr[x].equals(key)) return true; } return false; } }
3.-----------------------------------------------------------------------------------------------
Collections-reverseOrder
import java.util.*; class StrComparator implements Comparator<String> { public int compare(String s1,String s2) { int num=s1.compareTo(s2); if(num>0) return -1; if(num<0) return 1; return num; } } class StrLenComparator implements Comparator<String> { public int compare(String s1,String s2) { if(s1.length()>s2.length()) return 1; if(s1.length()<s2.length()) return -1; return s1.compareTo(s2); } } class CollectionsDemo { public static void main(String[] args) { orderDemo(); } public static void orderDemo() { //TreeSet<String> ts=new TreeSet<String>(new StrComparator()); //TreeSet<String> ts=new TreeSet<String>(Collections.reverseOrder());//反转 TreeSet<String> ts=new TreeSet<String>(Collections.reverseOrder(new StrLenComparator()));//逆转一个比较器 ts.add("sdfsdf"); ts.add("bdf"); ts.add("adfsf"); ts.add("wdff"); Iterator<String> it=ts.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
4.-----------------------------------------------------------------------------------
/* 集合变数组 Collection 接口 toArray(); <T> T[] toArray(T[] t); */ import java.util.*; class Demo5 { public static void main(String[] args) { List<String> list=new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); String[] st=list.toArray(new String[list.size]); //相当于告诉集合应该装入什么样的数组中。 /* 参数作用:是声明一个数组,将集合中的元素都装入到数组中, 如果数组长度小于集合长度,得到的数组长度与集合的长度一样。 如果数组长度大于集合长度,得到的数组是我们声明的数组的长度,无元素填充的位置用null。 在开发使用时,我们一般指定都直接使用集合的长度 这种操作的作用是什么? 数组的长度是定长的,为了不更改集合的长度。 */ for(int i=0;i<st.length;i++){ System.out.println(st[i]); } } }
import java.util.*; public class CollectionToArray { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<String> al=new ArrayList<String>(); al.add("abc1"); al.add("abc2"); al.add("abc3"); String[] arr=al.toArray(new String[al.size()]); System.out.println(Arrays.toString(arr)); } }
5.-------------------------------------------------------------------------
/*jdk1.5 新特性
* 高级for循环
* 格式:
* for(数据类型 变量名:被遍历的集合(Collection)或者数组)
* {
* }
* 对集合进行遍历
* 只能获取集合元素,但是不能对集合进行操作
* 迭代器除了遍历,还可以进行remove集合中元素的动作
* 如果是用ListIterator,还可以在遍历过程中对集合进行增删改查的动作
*
* 传统for循环和高级for的区别
* 高级for有一个局限性,必须有被遍历的目标
* 建议在遍历数组的时候还是使用传统for
import java.util.*; public class ForEachDemo { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<String> al=new ArrayList<String>(); al.add("abc1"); al.add("abc2"); al.add("abc3"); for(String s:al) { s="kk"; System.out.println(s); } Iterator<String> it=al.iterator(); while(it.hasNext()) { System.out.println(it.next()); } HashMap<Integer,String> hm=new HashMap<Integer,String>(); hm.put(1,"a"); hm.put(2,"b"); hm.put(3,"c"); Set<Integer> keyset=hm.keySet(); for(Integer i:keyset) { System.out.println(i+"::"+hm.get(i)); } Set<Map.Entry<Integer, String>> entrySet=hm.entrySet(); for(Map.Entry<Integer, String> me:entrySet) { } for(Map.Entry<Integer, String> me:hm.entrySet()) { System.out.println(me.getKey()+"-----"+me.getValue()); } } }
import java.util.*; class Demo6 { public static void main(String[] args) { //List<String> list=new ArrayList<String>(); //list.add("a"); //list.add("b"); //list.add("c"); //list.add("d"); //第一种方式 List集合特有 /* for(int i=0;i<list.size();i++){ System.out.println(list.get(i)); } //ListIterator还可以使用 */ //第二种方式 iterator Collection所有集合通用 /* for(Iterator<String> it=list.iterator();it.hasNext();){ System.out.println(it.next()); }*/ //增强for /* for(String s:list){ System.out.println(s); }*/ /* //增强for操作数组 String[] st={"a","b","c"}; for(String s:st){ System.out.println(s); }*/ //for不能直接操作Map,还是需要间接操作 //Map<Integer,String> map=new HashMap<Integer,String>(); //map.put(1,"a"); //map.put(11,"b"); //map.put(111,"c"); /* Set<Integer> set=map.keySet(); //增强for可以直接迭代Set for(int key:set){ System.out.println("KEY:"+key+" VALUE:"+map.get(key)); }*/ /* Set<Map.Entry<Integer,String>> set=map.entrySet(); for(Map.Entry<Integer,String> me:set){ System.out.println("KEY:"+me.getKey()+" VALUE:"+me.getValue()); }*/ //增强for遍历二维数组 int[][] arr={{1,2,3},{4,5,6},{7,8,9}}; for(int[] a:arr){ for(int n:a){ System.out.print(n+" "); } System.out.println(); } } }
jdk1.5 新特性
静态导入
简化写法
在应用时,如果方法名重复,那么通过参数是可以区分,
如果区分不开,那么这时必须指定是哪个类中的方法。
import java.util.*; import static java.util.Collections.*; //导入Collections所有static方法 import static java.util.Arrays.*; class Demo7 { public static void main(String[] args) { List<Integer> list=new ArrayList<Integer>(); list.add(22); list.add(12); list.add(32); sort(list);//它是Collections中的sort System.out.println(list); int[] arr={24,22,53}; sort(arr); //System.out.println(toString(arr)); //认为与Object类中继承的toString方法相同 System.out.println(Arrays.toString(arr)); } //public static void sort(List list){ // System.out.println("本类的sort方法"); //} }
jdk1.5 新特性
动态参数
动态参数写法 类型...
动态参数注意:动态参数后面不要在写其它参数,如果有的话,写在动态参数前面。
import java.util.*; class Demo8 { public static void main(String[] args) { Demo8 d=new Demo8(); d.add(1,2); //相当于将1,2都传递到a数组中 d.add(1,2,3,4,5); d.method(); } public void add(int b,int... a){ //对于这个程序来说 a相当于是一个数组 System.out.println(a.length); } //List<T> asList(T... a) public void method(){ List<Integer> list=Arrays.asList(1,2,3,4,5); System.out.println(list); } /* //求a与b的和 public void add(int a,int b){ System.out.println(a+b); } //求a,b,c的和 public void add(int a,int b,int c){ System.out.println(a+b+c); } // 如果我们求多个数的和,应用想到应用数组. public void add(int[] arr){ } */ }
相关文章推荐
- Java集合框架-4.【集合工具类:Collections、Arrays】【JDK1.5新特性】
- 黑马程序员——Java之集合框架工具类Collections、Arrays以及Java新特性
- 黑马程序员——Java基础---泛型、集合框架工具类:Collections和Arrays、JDK 1.5新特性
- 黑马程序员---集合框架工具类Collections,数组工具类Arrays常见方法以及1.5版本新特性(高级for,可变参数和静态导入)
- JDK1.5新特性,基础类库篇,集合框架(Collections)
- Collections五种返回集合的特性分析
- 黑马程序员——Java基础---集合<二>Map,Arrays,Collections
- 黑马程序员_学习笔记第16天——Collections、jdk1.5新特性
- 集合框架3(Map集合,Collections,Arrays工具类)
- 黑马程序员---java----集合框架工具类:collections,Arrays和一些类
- 黑马程序员——Java基础--集合框架工具类:Collections、Arrays、其他对象
- (14)多线程与并发库之java5同步集合类的应用【包含jdk1.5新特性 ConcurrentHashMap】
- 黑马程序员——Java基础---泛型、集合框架工具类:collections和Arrays
- Java——集合工具类和jdk1.5有关集合的新特性
- 16-常用对象API(集合框架-工具类-Collections-其他方法&将非同步集合转成同步集合的方法) 17-常用对象API(集合框架-工具类-Arrays-方法介绍)_20171202
- 黑马程序员--集合框架(map集合,Collections,Arrays)
- day17集合框架的工具类Collections。二分法查找原理 。操作数组的工具类Arrays,集合变数组。高级for循环。可变参数
- 黑马程序员_毕向东JAVA基础_集合(3)Map&Collections&Arrays&增强for循环
- 对比集合工具类Collections进一步学习数组工具类Arrays
- Java基础---泛型、集合框架工具类:collections和Arrays