一、继承关系图
二、类图
java中的List接口以及实现类,类图如下:
* @author Josh Bloch * @author Neal Gafter * @see Collection * @see Set * @see ArrayList * @see LinkedList * @see Vector * @see Arrays#asList(Object[]) * @see Collections#nCopies(int, Object) * @see Collections#EMPTY_LIST * @see AbstractList * @see AbstractSequentialList * @since 1.2
三、源码分析
public interface List<E> extends Collection<E> { /** * 定义addAll方法,从某个index开始插入指定集合实例 */ boolean addAll(int index, Collection<? extends E> c); /** * 定义removeAll方法,从某个index开始删除指定集合实例 */ boolean removeAll(Collection<?> c); /** * 定义removeAll方法,从某个index开始删除指定集合实例 */ boolean retainAll(Collection<?> c); /** * jdk1.8新增 * 大意为: * 定义replaceAll方法,根据传递参数的函数式,传递内容是接口类型 * 该接口定义了operator函数,该函数传递给Objects.requireNonNull进行判断 * 匹配成功则进行set值进行替换,使用List迭代器进行迭代替换 * @since 1.8 */ default void replaceAll(UnaryOperator<E> operator) { Objects.requireNonNull(operator); final ListIterator<E> li = this.listIterator(); while (li.hasNext()) { li.set(operator.apply(li.next())); } } /** * jdk1.8新增 * 大意为: * 定义sort方法,根据传递参数的函数式,传递内容是接口类型 * 该接口定义了Comparator函数,该函数传递给Arrays.sort进行判断并排序 * 并根据排序结果,使用迭代器迭代并重新set进List * @since 1.8 */ @SuppressWarnings({"unchecked", "rawtypes"}) default void sort(Comparator<? super E> c) { Object[] a = this.toArray(); Arrays.sort(a, (Comparator) c); ListIterator<E> i = this.listIterator(); for (Object e : a) { i.next(); i.set((E) e); } } /** * List要求,定义get方法,获取指定index的值 */ E get(int index); /** * List要求,定义set方法,在指定index的元素设置为目标元素 */ E set(int index, E element); /** * List要求,定义add方法,在指定index添加指定元素 */ void add(int index, E element); /** * List要求,定义remove方法,从指定的index删除该元素并重新调整List */ E remove(int index); /** * List要求,定义indexOf方法,正序查询指定元素第一次出现的index序号 */ int indexOf(Object o); /** * List要求,定义lastIndexOf方法,倒叙查询指定元素第一次出现的的index序号 */ int lastIndexOf(Object o); /** * List要求,定义ListIterator迭代器方法,获取该List的迭代器 */ ListIterator<E> listIterator(); /** * List要求,定义ListIterator迭代器方法,获取从指定index开始的指定迭代器 */ ListIterator<E> listIterator(int index); /** * List要求,定义subList方法,从起始和结束index拆分出新的list */ List<E> subList(int fromIndex, int toIndex); /** * jdk1.8新增 * 大意为: * 根据当前的list内容进行排序,进行迭代器拆分,拆分成新的迭代器 * 用于多线程迭代使用 * @since 1.8 */ @Override default Spliterator<E> spliterator() { return Spliterators.spliterator(this, Spliterator.ORDERED); }
四、JDK8对list的流处理
1. 跟据某个属性分组OfficeId:
Map<String, List<IncomeSumPojo>> collect = list.stream().collect(Collectors.groupingBy(IncomeSumPojo::getOfficeId));
2. 根据某个属性分组OfficeId,汇总某个属性Money:
Map<String, Double> collect = list.stream().collect(Collectors.groupingBy(IncomeSumPojo::getOfficeId,Collectors.summingDouble(IncomeSumPojo::getMoney)));
3. 根据某个属性添加条件过滤数据:
list = list.stream().filter(u -> !u.getAmount().equals("0.00")).collect(Collectors.toList());
4. 判断一组对象里面有没有属性值是某个值:
List<Menu> menuList = UserUtils.getMenuList();
boolean add = menuList.stream().anyMatch(m -> "plan:ctPlan:add".equals(m.getPermission()));
5. 取出一组对象的某个属性组成一个新集合:
List<String> tableNames=list.stream().map(User::getMessage).collect(Collectors.toList());
6. list去重复:
list = list.stream().distinct().collect(Collectors.toList());
实例如下:
package com.security.common.elk; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class TEST { public static void main(String[] args) { List<Student> list = new ArrayList<>(); Student student1 = new Student(); student1.setAge("12"); student1.setSex(0); Student student2 = new Student(); student2.setAge("13"); student2.setSex(2); Student student3 = new Student(); student3.setAge("11"); student3.setSex(1); Student student4 = new Student(); student4.setAge("18"); student4.setSex(1); Student student5 = new Student(); student5.setAge("18"); student5.setSex(0); Student student6 = new Student(); student6.setAge("18"); student6.setSex(2); Student student7 = new Student(); student7.setAge("18"); student7.setSex(2); list.add(student1); list.add(student2); list.add(student3); list.add(student4); list.add(student5); list.add(student6); list.add(student7); List<Demo> demos = new ArrayList(); // 原始数据 System.out.println("原始数据 组装list<demo>*******************"); demos = list.stream().map(student -> new Demo(student.getAge(), student.getSex())).collect(Collectors.toList()); demos.forEach(demo -> { System.out.println("年龄 " + demo.getAge() + " 性别 " + demo.getSex() + ","); }); // 只取sex为0 System.out.println("只取sex为0****************"); List<Demo> demorm = demos.stream().filter(demo -> demo.getSex() == 0).distinct().collect(Collectors.toList()); demorm.forEach(demo -> { System.out.println("年龄 " + demo.getAge() + " 性别 " + demo.getSex() + ","); }); // 筛选年龄大于12岁 System.out.println("筛选年龄大于12岁的*************"); List<Demo> demoFilter = demos.stream().filter(demo -> Integer.valueOf(demo.getAge()) > 12).collect(Collectors.toList()); demoFilter.forEach(demo -> { System.out.println("年龄 " + demo.getAge() + " 性别 " + demo.getSex() + ","); }); // 排序 System.out.println("排序******************"); List<Demo> demoSort = demos.stream().sorted((s1, s2) -> s1.getAge().compareTo(s2.getAge())).collect(Collectors.toList()); demoSort.forEach(demo -> { System.out.println("年龄 " + demo.getAge() + " 性别 " + demo.getSex() + ","); }); // 倒序 System.out.println("倒序****************"); ArrayList<Demo> demoArray = (ArrayList) demos; Comparator<Demo> comparator = (h1, h2) -> h1.getAge().compareTo(h2.getAge()); demos.sort(comparator.reversed()); //or demos.sort(Comparator.comparing(Demo::getAge).reversed()); demos.forEach(demo -> { System.out.println("年龄 " + demo.getAge() + " 性别 " + demo.getSex() + ","); }); // 多条件正序 System.out.println("多条件排序正序****************"); demoArray.sort(Comparator.comparing(Demo::getSex).thenComparing(Demo::getAge)); demoArray.forEach(demo -> { System.out.println("年龄 " + demo.getAge() + " 性别 " + demo.getSex() + ","); }); // 多条件倒序 System.out.println("多条件排序倒序 sex 倒序****************"); demoArray.sort(Comparator.comparing(Demo::getSex).reversed().thenComparing(Demo::getAge)); demoArray.forEach(demo -> { System.out.println("年龄 " + demo.getAge() + " 性别 " + demo.getSex() + ","); }); // 按照年龄分组 System.out.println("根据age分组结果为Map****************"); Map demoOder = demos.stream().collect(Collectors.groupingBy(Demo::getAge)); System.out.println(demoOder); } } class Demo { private String name; private Integer sex; private String age; public Demo(String age, Integer sex) { this.age = age; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } } class Student { private String age; private Integer sex; public String getAge() { return age; } public void setAge(String age) { this.age = age; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } }
java.util.List是一个接口,该接口继承了Collection接口,因此同样方法,作为接口只是定义规范不同。 实际的不同要看独特的定义或独特的实现才可以。