java基础笔记——集合Collection--List--Set
1 package collection_show; 2 /* 3 * Collection--List--Set 4 */ 5 6 7 /* 8 * Collection接口:(顺序和重复)------------------------------------------------------------- 9 * 子接口List:(都是)元素有序,可重复的集合 ---”动态”数组 10 * 子接口Set:(都不)元素无序、不可重复的集合 ---类似高中的“集合” 11 * 12 13 14 15 *----实例化: Collection 是接口不能直接实例化,就用子类要实现:向上转型 (Set接口没有其他的方法,基本一样) 16 Collection coll=new ArrayList(); 17 *----方法: 18 ---> coll.add(E e);添加E类型的对象 e 19 ---> coll.addAll(coll2);把coll2全部添加到coll中 20 coll.clear();清楚所有对象 21 coll.contains( Object o);如果中包含对象o,返回true (contains包含) 22 coll.equals( Object o);比较两个对象是否相等 23 coll.isEmpty();判断是否为空,返回boolean型 24 ---> coll.remove( Object o); 移除指定元素的单个实例,如果存在的话 25 coll. removeAll(Collection<?> c);同上 26 coll. retainAll(Collection<?> c); 和删除相反,保留 27 ---> coll.size();返回int 元素个数 28 coll.toArray();返回包含此 collection 中所有元素的数组 返回类型Object[],用 ctrl +1 来显示正确的类型 29 ---> coll.iterator();返回在此 collection 的元素上进行迭代的迭代器 30 31 32 33 34 35 * 子接口List:(都是)元素有序,可重复的集合 ---”动态”数组--------------------------------------------- 36 * 37 *----实例化:List list=new ArrayList(); ----- ArrayList LinkList Vector 38 *----添加的方法: 39 list. add(int index, Object element); 40 list.addAll(int index,Collection coll2); 41 ---> list.get(int index); 42 ---> list.indexOf(Object element); lastIndexOf() 43 ---> list.remove(int index); 44 list.set(int index,Object element); 45 ---> list.subList(int fromIndex,int toIndex);// 包含fromIndex 不包含toIndex下标 46 47 48 49 50 51 * 子接口Set:(都不)元素无序、不可重复的集合 ---类似高中的“集合”------------------------------ 52 * 53 * ----HashSet TreeSet 54 * 55 56 57 58 59 * 主要是: TreeSet 的排序--------按照要求做出排序代码 60 * -----------------------------自然排序和定制排序----------------------------- 61 * ----------------------------自然排序: Comparable 62 1、对于单个类型对象----升序排序(就是说添加的对象是int等单个信息的时候) 63 2、对个属性---- 64 (前提)在 <添加的对象类> 中--实现了java.lang.Comparable接口(包含compareTo()方法) 65 必需要override该接口的compareTo()方法,才能使用排序。 66 (扩展)Comparable接口: 67 只有一个方法,int compareTo(Object obj) 表示按照字典顺序对Unicode比较差值 68 如果该对象小于、等于或大于指定对象,则分别返回负整数、零或正整数。 69 例子:s1.compareTo(s2)==0,表示两个字符串相等 70 例子:******************前提:对象的类implements Comparable 实现了接口 71 @Override 72 public int compareTo(Object o) { 73 Person per=null; 74 if(o instanceof Person){ 75 per=(Person) o; 76 } 77 78 if(this.name.compareTo(per.name)==0){//先比较name 再比较age 79 return this.age-per.age; 80 }else{ 81 return this.name.compareTo(per.name); 82 } 83 //return this.age-per.age;//单独比较age 84 //return this.name.compareTo(per.name);//单独比较name 85 } 86 87 88 89 90 * -------------------定制排序: Comparator !!!添加排序类的对象com在TreeSet实例化对象中 91 在 <测试类> 中----实现java.util.Comparator接口(匿名内部类) 92 重写方法: int compare(T o1, T o2) 来比较 93 比较用来排序的两个参数。根据第一个参数小于、等于或大于第二个参数分别返回负整数、零或正整数。 94 例子: 95 @Test 96 public void testTreeSetSort(){ 97 //定制排序 98 Comparator com=new Comparator() { //匿名内部类 //C c=new C(){..}; 99 @Override 100 public int compare(Object o1, Object o2) { //override compare方法 101 Student stu1 = null; 102 Student stu2 = null; 103 if(o1 instanceof Student){ 104 stu1=(Student) o1; 105 } 106 if(o2 instanceof Student){ 107 stu2=(Student) o2; 108 } 109 if(stu1.getName().compareTo(stu2.getName())==0){ //和自然排序一样 110 return stu1.getAge()-stu2.getAge(); 111 }else{ 112 return stu1.getName().compareTo(stu2.getName()); 113 } 114 } 115 }; 116 Set set=new TreeSet(com); //这个才是重点区别<> 117 set.add(new Student("Zhang3",23)); 118 set.add(new Student("Li4",18)); 119 set.add(new Student("Li4",43)); 120 set.add(new Student("Li4",12)); 121 set.add(new Student("Yao5",20)); 122 set.add(new Student("Liu7",34)); 123 System.out.println(set); 124 } 125 126 127 * 128 * 129 * 130 */ 131 public class Collection_Method { 132 133 }