java Collection中的排序问题 这里讨论list.set.map的排序,包括按照map的value进行排序. 1)list排序 list排序可以直接采用Collections的sort方法,也可以使用Arrays的sort方法,归根结底Collections就是调用Arrays的sort方法. public static <T> void sort(List<T> list, Comparator<? super T> c) { Object[] a
先定义一个实体类 @Data @AllArgsConstructor @NoArgsConstructor public class Human { private String name; private int age; } 下面的操作都基于这个类来进行操作.这里面使用了Lombok类库,它用注解的方式实现了基本的get和set等方法,让代码看起来更加的优雅. JAVA8之前的List排序操作 在Java8之前,对集合排序只能创建一个匿名内部类 new Comparator<Human>(
Collection<int> aa = new Collection<int>(); aa.Add(1); aa.Add(2); aa.Add(3); aa.Add(1); int[] arr = new int[aa.Count]; aa.CopyTo(arr, 0); int[] bb = aa.ToArray(); Array.Sort(bb); int i = 0;
冒泡排序 public class test { public static void main(String[] args) { // TODO Auto-generated method stub int numbers[] = { 6, 2, 4, 1, 5, 9 }; BubbleSort(numbers); } public static void BubbleSort(int [] numbers){ for (int i = 0; i < numbers.length - 1; i
JDK8中包含了许多内建的Java中常用到函数接口,比如Comparator或者Runnable接口,这些接口都增加了@FunctionalInterface注解以便能用在lambda上. name type description Consumer Consumer< T > 接收T对象,不返回值 Predicate Predicate< T > 接收T对象并返回boolean Function Function< T, R > 接收T对象,返回R对象 Supplie