19、集合框架_Collections工具类
一、Collections工具类
Collections和Collection不同,前者是集合的操作类,后者是集合接口 Collections提供的静态方法 addAll():批量添加 sort():排序 binarySearch():二分查找 fill():替换 shuffle():随机排序 reverse():逆序1、addall()
public class CollectionsDemo { public static void main(String[] args) { Listlist = new ArrayList (); list.add("af"); list.add("bg"); list.add("acssf"); list.add("bdfsdfsd"); Collections.addAll(list,"cefsdf","cf1","cg32"); System.out.println(list); }
打印结果为:
/* [af, bg, acssf, bdfsdfsd, cefsdf, cf1, cg32] Process finished with exit code 0 */
2、sort()
public class CollectionsDemo { public static void main(String[] args) { Listlist = new ArrayList (); list.add("af"); list.add("bg"); list.add("acssf"); list.add("bdfsdfsd"); Collections.addAll(list,"cefsdf","cf1","cg32"); System.out.println(list); list.sort(new Comparator () { @Override public int compare(String o1, String o2) { if(o1.length()>o2.length()){ return 1; }else if(o1.length()<o2.length()){ return -1; }else{ return 0; } } }); System.out.println(list); } }
打印结果为:
/* [af, bg, acssf, bdfsdfsd, cefsdf, cf1, cg32] [af, bg, cf1, cg32, acssf, cefsdf, bdfsdfsd] Process finished with exit code 0 */
还可以
public class CollectionsDemo { public static void main(String[] args) { Listlist = new ArrayList (); list.add("af"); list.add("bg"); list.add("acssf"); list.add("bdfsdfsd"); Collections.addAll(list,"cefsdf","cf1","cg32"); System.out.println(list); list.sort(new Comparator () { @Override public int compare(String o1, String o2) { if(o1.length()>o2.length()){ return 1; }else if(o1.length()<o2.length()){ return -1; }else{ return 0; } } }); System.out.println(list); Collections.sort(list); System.out.println(list); Collections.sort(list,new Comparator () { @Override public int compare(String o1, String o2) { if(o1.length()>o2.length()){ return 1; }else if(o1.length()<o2.length()){ return -1; }else{ return 0; } } }); System.out.println(list); } }
打印结果为:
/* [af, bg, acssf, bdfsdfsd, cefsdf, cf1, cg32] [af, bg, cf1, cg32, acssf, cefsdf, bdfsdfsd] [acssf, af, bdfsdfsd, bg, cefsdf, cf1, cg32] [af, bg, cf1, cg32, acssf, cefsdf, bdfsdfsd] Process finished with exit code 0 */
3、binarySearch()二分查找
public class CollectionsDemo { public static void main(String[] args) { Listlist = new ArrayList (); list.add("af"); list.add("bg"); list.add("acssf"); list.add("bdfsdfsd"); Collections.addAll(list,"cefsdf","cf1","cg32"); System.out.println(list); //二分查找的时候需要先进行排序操作,如果没有排序的话,是找不到指定元素的 Collections.sort(list); System.out.println(Collections.binarySearch(list,"acssf")); } }
打印结果为:
/* [af, bg, acssf, bdfsdfsd, cefsdf, cf1, cg32] 0 Process finished with exit code 0 */
4、fill()填充对应元素
public class CollectionsDemo { public static void main(String[] args) { Listlist = new ArrayList (); list.add("af"); list.add("bg"); list.add("acssf"); list.add("bdfsdfsd"); Collections.addAll(list,"cefsdf","cf1","cg32"); System.out.println(list); Collections.fill(list,"mashibing"); System.out.println(list); } }
打印结果为:
/* [af, bg, acssf, bdfsdfsd, cefsdf, cf1, cg32] [mashibing, mashibing, mashibing, mashibing, mashibing, mashibing, mashibing] Process finished with exit code 0 */
Arrays工具类
/** * Arrays提供了数据操作的工具类,包含很多方法 * 集合和数组之间的转换 * 数组转成list: * * */
public class ArraysDemo { public static void main(String[] args) { // int[] array = new int[]{1,2,3,4,5}; Listints = Arrays.asList(1,2,3,4,5); //list转换成数组 Object[] objects = ints.toArray(); } }