java__集合的工具类
来源:互联网 发布:unity 删除整个数组 编辑:程序博客网 时间:2024/05/29 19:34
Collections与Arrays集合框架中的工具类:特点:该工具类中的方法都是静态的。Collections:常见方法:1,对list进行二分查找:前提该集合一定要有序。int binarySearch(list,key);//必须根据元素自然顺序对列表进行升级排序//要求list 集合中的元素都是Comparable 的子类。int binarySearch(list,key,Comparator);2,对list集合进行排序。sort(list); //对list进行排序,其实使用的事list容器中的对象的compareTo方法sort(list,comaprator);//按照指定比较器进行排序3,对集合取最大值或者最小值。max(Collection)max(Collection,comparator)min(Collection)min(Collection,comparator)4,对list集合进行反转。reverse(list);8,可以将不同步的集合变成同步的集合。Set synchronizedSet(Set<T> s)Map synchronizedMap(Map<K,V> m)List synchronizedList(List<T> list)Arrays:用于对数组操作的工具类1,二分查找,数组需要有序binarySearch(int[])binarySearch(double[])2,数组排序sort(int[])sort(char[])……2,将数组变成字符串。 toString(int[])3,复制数组。 copyOf(boolean[] original, int newLength) original:源数组 newLength:新数组长度4,复制部分数组。copyOfRange(boolean[] original, int from, int to) original:源数组from :开始拷贝的索引to:结束拷贝索引5,比较两个数组是否相同。元素对应位置的元素是否一致equals(int[],int[]);6,将数组变成集合。List asList(T[]);这样可以通过集合的操作来操作数组中元素,但是不可以使用增删方法,add,remove。因为数组长度是固定的,会出现UnsupportOperationExcetion。可以使用的方法:contains,indexOf。。。如果数组中存入的基本数据类型,那么asList会将数组实体作为集合中的元素。如果数组中的存入的引用数据类型,那么asList会将数组中的元素作为集合中的元素。import java.util.ArrayList;import java.util.Collections;import java.util.Arrays;import java.util.List;class Demo1 { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(4); list.add(3); list.add(1); list.add(2); list.add(3); // 排序 Collections.sort(list); // 折半查找的前提是排序好的元素 System.out.println( Collections.binarySearch( list , 8 ) ); // 找不到返回-插入点-1 // 反序集合输出 Collections.reverse( list ); System.out.println( list ); // 求最值 System.out.println( Collections.max( list ) ); // 4 // fill() 使用指定的元素替换指定集合中的所有元素 // Collections.fill( list, 5 ); System.out.println( list ); // 将数组转换为集合 Integer is[] = new Integer[]{6,7,8}; List<Integer> list2 = Arrays.asList(is); list.addAll( list2 ); System.out.println( list ); // 将List转换为数组 Object [] ins = list.toArray(); System.out.println( Arrays.toString( ins ) ); }}集合的练习问题: 定义一个Person数组,将Person数组中的重复对象剔除?思路: 1. 描述一个Person类 2. 将数组转换为Arrays.asList() List 3. Set addAll( list ) 4. hashCode()且equals()import java.util.Arrays;import java.util.Set;import java.util.List;import java.util.HashSet;// 1. 描述Person类class Person { public String name; public int age; public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } public String toString() { return getClass().getName() + " : name=" + this.name + " age=" + this.age; } // 4. 重写hashCode和equals() public int hashCode() { return this.age; } public boolean equals(Object o) { Person p = null; if (o instanceof Person) p = (Person) o; return this.name.equals(p.name) && (this.age == p.age); }}class Demo2 { public static void main(String[] args) { Person[] ps = new Person[] { new Person("jack", 34), new Person("lucy", 20), new Person("lili", 10), new Person("jack", 34) }; // 遍历数组 System.out.println(Arrays.toString(ps)); // 2. 将自定义对象数组转换为List集合 List<Person> list = Arrays.asList(ps); // 3. 将List转换为Set Set<Person> set = new HashSet<Person>(); set.addAll(list); System.out.println(set); }}
0 0
- java__集合的工具类
- java__集合02
- java__集合03
- 黑马程序员Java__集合(数组)和类
- Java__子类中调用父类重写的方法。
- 集合框架的工具类
- 集合框架的工具类
- 集合框架的工具类
- Java__内部类,抽象类,继承,接口,异常的处理...等
- java__泛型的类型参数推测
- 1006--Java集合--操作集合的工具类:Collections
- 集合之Collections——集合的工具类
- Java集合之集合工具类的使用
- Collections 操作集合的工具类
- 操作集合的工具类 collections
- java开发工具类的集合
- 操作集合的工具类:Collections
- 操作集合的工具类:Collections
- eclipse 快捷键
- Pointable类(Leap::Pointable)
- linux常用命令(33):df 命令
- Linux 安装Python3.4.3
- lintcode-最长公共子串-79
- java__集合的工具类
- rectint 函数
- linux grep命令
- 开启新的篇章
- 标准SQL的update语句三种用法
- POJ 1703Find them, Catch them(经典并查集)
- 将mongodb启动做成服务
- java中Map,List与Set的区别
- TCP/IP三次握手与四次握手