java集合工具类
1 //集合类工具 2 //集合类工具Collection:排序,复制,反转等操作 3 //数组工具类Arrays:排序,复制,反转等操作 4 package col; 5 6 import java.util.ArrayList; 7 import java.util.Collection; 8 import java.util.Collections; 9 import java.util.List; 10 11 public class Demo6 { 12 13 public static void main(String[] args) { 14 List<String> list = new ArrayList<>(); 15 list.add("w"); 16 list.add("a"); 17 list.add("b"); 18 list.add("x"); 19 list.add("a"); 20 list.add("i"); 21 Collections.sort(list); 22 System.out.println(list); 23 System.out.println(Collections.max(list)); 24 System.out.println(Collections.min(list)); 25 // 二分查找法,(使用前必须保证有序) 26 System.out.println(Collections.binarySearch(list, "i")); 27 // 混洗,洗牌:打乱已有顺序 28 Collections.shuffle(list); 29 System.out.println(list); 30 Collections.reverse(list); 31 System.out.println(list); 32 Collections.swap(list, 2, 3); 33 System.out.println(list); 34 Collections.replaceAll(list, "a", "A"); 35 System.out.println(list); 36 Collections.fill(list, "H"); 37 System.out.println(list); 38 } 39 40 }
1 package col; 2 3 import java.util.Arrays; 4 5 public class Demo7 { 6 7 public static void main(String[] args) { 8 int[] arr = new int[] { 5, 4, 3, 8, 2, 9 }; 9 Arrays.sort(arr); 10 for (int x : arr) { 11 System.out.print(x + " "); 12 } 13 System.out.println(); 14 /*Arrays.fill(arr, 5); 15 for (int x : arr) { 16 System.out.print(x + " "); 17 }*/ 18 int pos=Arrays.binarySearch(arr, 3); 19 System.out.println(pos); 20 } 21 22 }
1 package col; 2 3 public class Student implements Comparable { 4 private int id; 5 private String name; 6 private int age; 7 private String city; 8 public Student() { 9 } 10 public Student(int id, String name, int age, String city) { 11 super(); 12 this.id = id; 13 this.name = name; 14 this.age = age; 15 this.city = city; 16 } 17 public int getId() { 18 return id; 19 } 20 public void setId(int id) { 21 this.id = id; 22 } 23 public String getName() { 24 return name; 25 } 26 public void setName(String name) { 27 this.name = name; 28 } 29 public int getAge() { 30 return age; 31 } 32 public void setAge(int age) { 33 this.age = age; 34 } 35 public String getCity() { 36 return city; 37 } 38 public void setCity(String city) { 39 this.city = city; 40 } 41 @Override 42 public String toString() { 43 return "Student [id=" + id + ", name=" + name + ", age=" + age + ", city=" + city + "]"; 44 } 45 //重写比较器接口中的方法 46 47 /* 48 {1,3,5,7 }升序 49 {7,5,3,1 }降序 50 //输入源 3 1 7 5 51 {3} 52 53 返回值: 54 1正数:当前对象大, 降序 55 -1负数:传入的对象大 ,升序 56 0:一样大 57 */ 58 59 public int compareTo(Object o) { 60 Student inputstudent=(Student)o; 61 //根据学号排序,降序 62 int result =this.id>inputstudent.id?-1:( this.id == inputstudent.id ?0:1 );
//int result =this.id<inputstudent.id?1:( this.id == inputstudent.id ?0:-1 ); 63 if(result==0) { 64 result=this.name.compareTo(inputstudent.name); 65 } 66 return result; 67 //return 1;//当前对象,比传入对象大 68 } 69 } 70 71 package col; 72 73 public class Teacher { 74 private int id; 75 private String name; 76 private int age; 77 private String city; 78 public Teacher(int id, String name, int age, String city) { 79 this.id = id; 80 this.name = name; 81 this.age = age; 82 this.city = city; 83 } 84 public Teacher() { 85 super(); 86 } 87 public int getId() { 88 return id; 89 } 90 public void setId(int id) { 91 this.id = id; 92 } 93 public String getName() { 94 return name; 95 } 96 public void setName(String name) { 97 this.name = name; 98 } 99 public int getAge() { 100 return age; 101 } 102 public void setAge(int age) { 103 this.age = age; 104 } 105 public String getCity() { 106 return city; 107 } 108 public void setCity(String city) { 109 this.city = city; 110 } 111 @Override 112 public String toString() { 113 return "Teacher [id=" + id + ", name=" + name + ", age=" + age + ", city=" + city + "]"; 114 } 115 116 } 117 118 package col; 119 120 import java.util.Comparator; 121 122 /*思路:将比较的对象(Student)实现Comparable接口, 123 * 重写连的compareTo()方法。在compareTo()中编写比较的逻辑。重点是返回值,-1 ,0,1 ; 124 Comparator:外部比较器 (无侵入性,不影响原有代码) 125 先定义一个外部比较器*/ 126 public class MyComparatorWithId implements Comparator { 127 128 @Override 129 public int compare(Object o1, Object o2) { 130 Teacher t1=(Teacher)o1; 131 Teacher t2=(Teacher)o2; 132 return t2.getId()-t1.getId(); 133 } 134 135 136 } 137 138 package col; 139 140 import java.util.ArrayList; 141 import java.util.Collections; 142 import java.util.List; 143 144 public class Demo8 { 145 //内部比较器 146 public static void main(String[] args) { 147 List<Student> students = new ArrayList<>(); 148 Student s1 = new Student(10, "张三", 23, "阜阳"); 149 Student s2 = new Student(15, "李四", 21, "亳州"); 150 Student s3 = new Student(13, "王二", 26, "宿州"); 151 Student s4 = new Student(17, "麻子", 65, "合肥"); 152 students.add(s1); 153 students.add(s2); 154 students.add(s3); 155 students.add(s4); 156 Collections.sort(students); 157 System.out.println(students); 158 System.out.println("-------"); 159 160 Teacher t1=new Teacher(12, "贤明", 56, "山东"); 161 Teacher t2=new Teacher(42, "大明", 16, "贵州"); 162 Teacher t3=new Teacher(13, "小明", 76, "四川"); 163 List<Teacher> teachers=new ArrayList<Teacher>(); 164 teachers.add(t1); 165 teachers.add(t2); 166 teachers.add(t3); 167 Collections.sort(teachers, new MyComparatorWithId()); 168 System.out.println(teachers); 169 } 170 171 }
道阻且长,行则将至