1.集合的常用框架
根据数据结构划分:
Collection
list set
ArraryList ,LinkList,Vector TreeSet,HashSet
2.集合的存储
都是存储的对象的引用。
二:程序--Collection的公共方法
1.Collection的公共方法(常用)
增删查该
1 import java.util.ArrayList; 2 3 public class Test39 { 4 5 public static void pubTest(){ 6 ArrayList a1=new ArrayList(); 7 a1.add("java1"); 8 a1.add("java2"); 9 a1.add("java3"); 10 System.out.println(a1); 11 System.out.println(a1.size()); 12 System.out.println(a1.isEmpty()); 13 a1.remove("java1"); 14 System.out.println(a1); 15 System.out.println(a1.contains("java3")); 16 } 17 public static void pubTest2(){ 18 ArrayList a1=new ArrayList(); 19 a1.add("java1"); 20 a1.add("java2"); 21 a1.add("java3"); 22 ArrayList a2=new ArrayList(); 23 a2.add("java1"); 24 a2.add("java2"); 25 a2.add("java4"); 26 //a1.retainAll(a2); 27 //System.out.println(a1);//java1,java2 28 a1.removeAll(a2); 29 System.out.println(a1);//java3 30 31 } 32 public static void main(String[] args) { 33 //pubTest(); 34 pubTest2(); 35 } 36 }
2.Collection的常用方法(迭代器)
1 import java.util.ArrayList; 2 import java.util.Iterator; 3 4 public class Test40 { 5 6 public static void main(String[] args) { 7 ArrayList a1=new ArrayList(); 8 a1.add("java1"); 9 a1.add("java2"); 10 a1.add("java3"); 11 Iterator it=a1.iterator(); 12 while(it.hasNext()){ 13 System.out.println(it.next()); 14 } 15 16 for(Iterator i=a1.iterator();i.hasNext();){ 17 System.out.println(i.next()); 18 } 19 } 20 21 }
三:程序--ArrayList
1.ArrayList 增删该查方法
add(index,E)
addAll(index,E)
remove(index);
set(index,E)
get(index);
iterator();
subList(head,tail);
2.程序
1 import java.util.ArrayList; 2 3 public class Test41 { 4 5 public static void main(String[] args) { 6 ArrayList a2=new ArrayList(); 7 a2.add(0, "java0"); 8 a2.add(1, "java1"); 9 a2.add(2, "java2"); 10 a2.add(3, "java3"); 11 System.out.println(a2); 12 a2.remove(3); 13 System.out.println(a2); 14 a2.set(2, "java02"); 15 System.out.println(a2); 16 System.out.println(a2.get(0)); 17 System.out.println(a2.subList(0, 2)); 19 } 20 }
3.附加的方法
indexOf(E)
4.程序
1 public static void main(String[] args){ 2 ArrayList a2=new ArrayList(); 3 a2.add(0, "java0"); 4 a2.add(1, "java1"); 5 a2.add(2, "java2"); 6 System.out.println(a2.indexOf("java1")); 7 }
5.程序--ArrayList特有的迭代器listIterator
1 import java.util.ArrayList; 2 import java.util.ListIterator; 3 4 public class Test42 { 5 6 public static void main(String[] args) { 7 listT(); 8 } 9 10 private static void listT() { 11 ArrayList a2=new ArrayList(); 12 a2.add(0, "java0"); 13 a2.add(1, "java1"); 14 a2.add(2, "java2"); 15 a2.add(3, "java3"); 16 //System.out.println(a2); 17 ListIterator lt=a2.listIterator(); 18 while(lt.hasNext()){ 19 System.out.print(lt.next()); 20 } 21 System.out.println(); 22 while(lt.hasPrevious()){ 23 System.out.print(lt.previous()); 24 } 25 System.out.println(); 26 while(lt.hasNext()){ 27 Object obj=lt.next(); 28 if(obj.equals("java2")){ 29 lt.remove(); 30 } 31 } 32 System.out.println(a2); 33 } 34 35 }
四:程序--Vector
1.
1 import java.util.Enumeration; 2 import java.util.Vector; 3 4 public class Test43 { 5 6 public static void main(String[] args) { 7 Vector v=new Vector(); 8 v.add("java1"); 9 v.add("java2"); 10 v.add("java3"); 11 Enumeration en=v.elements(); 12 while(en.hasMoreElements()){ 13 System.out.println(en.nextElement()); 14 } 15 } 16 17 }
五:程序--LinkList
1.
1 import java.util.LinkedList; 2 3 public class Test44 { 4 5 public static void main(String[] args) { 6 fir(); 7 } 8 public static void fir(){ 9 LinkedList l=new LinkedList(); 10 l.addFirst("java1"); 11 l.addFirst("java2"); 12 System.out.println(l); 13 l.addLast("java3"); 14 System.out.println(l); 15 System.out.println(l.getFirst()); 16 System.out.println(l.getLast()); 17 System.out.println(l.removeFirst());//return last,then remove 18 System.out.println(l.size()); 19 } 20 }
六:set
1.概述
set的功能和Collection一致。
七:程序--HashSet的元素唯一性
先判断对象的hashcode,如果相同,在判断是否相同,如果值不同,在原有对象之后依次顺延。
1 import java.util.HashSet; 2 import java.util.Iterator; 3 4 public class Test45 { 5 public static void main(String[] args) { 6 hashs(); 7 } 8 private static void hashs() { 9 HashSet h=new HashSet(); 10 h.add(new per(1,"zhangsan")); 11 h.add(new per(2,"lisi")); 12 h.add(new per(3,"wangwu")); 13 h.add(new per(2,"lisi")); 14 Iterator it=h.iterator(); 15 while(it.hasNext()){ 16 per p=(per)it.next(); 17 System.out.println(p.getName()+".."+p.getNum()); 18 } 19 } 20 } 21 class per{ 22 private int num; 23 private String name; 24 per(){} 25 per(int num,String name){ 26 this.num=num; 27 this.name=name; 28 } 29 public int getNum() { 30 return num; 31 } 32 public void setNum(int num) { 33 this.num = num; 34 } 35 public String getName() { 36 return name; 37 } 38 public void setName(String name) { 39 this.name = name; 40 } 41 public int hashCode() { 42 return name.hashCode()+num; 43 } 44 public boolean equals(Object obj) { 45 if(!(obj instanceof per)){ 46 return false; 47 } 48 per p=(per)obj; 49 return this.name.equals(name)&&this.num==p.num; 50 } 51 52 }
八:TreeSet
1.程序--按照自然顺序进行排序
1 import java.util.Iterator; 2 import java.util.TreeSet; 3 4 public class Test46 { 5 public static void main(String[] args) { 6 TreeSet t=new TreeSet(); 7 t.add("abdd"); 8 t.add("fff"); 9 t.add("ad"); 10 t.add("aaa"); 11 Iterator it=t.iterator(); 12 while(it.hasNext()){ 13 System.out.println(it.next()); 14 } 15 } 16 }
2.程序--对对象自定义排序(让对象具有比较性)
1 import java.util.Iterator; 2 import java.util.TreeSet; 3 4 public class Test47 { 5 6 public static void main(String[] args) { 7 TreeSet t=new TreeSet(); 8 t.add(new student("zhangsan",19)); 9 t.add(new student("lisi",19)); 10 t.add(new student("zhaoliu",21)); 11 t.add(new student("lisi",19)); 12 t.add(new student("zhangsan",18)); 13 Iterator it=t.iterator(); 14 while(it.hasNext()){ 15 student st=(student)it.next(); 16 System.out.println(st.getname()+"....."+st.getage()); 17 } 18 } 19 20 } 21 class student implements Comparable{ 22 private String name; 23 private int age; 24 student(String name,int age){ 25 this.name=name; 26 this.age=age; 27 } 28 public int getage(){ 29 return age; 30 } 31 public String getname(){ 32 return name; 33 } 34 public int compareTo(Object obj) { 35 if(!(obj instanceof student)){ 36 throw new RuntimeException("do not stu"); 37 } 38 student stu=(student)obj; 39 if(this.age>stu.getage()){ 40 return 1; 41 } 42 if(this.age==stu.getage()){ 43 return this.name.compareTo(stu.getname()); 44 } 45 return -1; 46 } 47 }
3.程序--让集合具有比较性(比较器)
1 import java.util.Comparator; 2 import java.util.Iterator; 3 import java.util.TreeSet; 4 5 public class Test48 { 6 7 public static void main(String[] args) { 8 TreeSet t=new TreeSet(new mycomarator()); 9 t.add(new student2("zhangsan",19)); 10 t.add(new student2("lisi",19)); 11 t.add(new student2("zhaoliu",21)); 12 t.add(new student2("lisi",19)); 13 t.add(new student2("zhangsan",18)); 14 Iterator it=t.iterator(); 15 while(it.hasNext()){ 16 student2 st=(student2)it.next(); 17 System.out.println(st.getname()+"....."+st.getage()); 18 } 19 } 20 21 } 22 class mycomarator implements Comparator{ 23 public int compare(Object o1, Object o2) { 24 student2 st1=(student2)o1; 25 student2 st2=(student2)o2; 26 int num=st1.getname().compareTo(st2.getname()); 27 if(num==0){ 28 return new Integer(st1.getage()).compareTo(new Integer(st2.getage())); 29 } 30 return num; 31 } 32 33 } 34 class student2 implements Comparable{ 35 private String name; 36 private int age; 37 student2(String name,int age){ 38 this.name=name; 39 this.age=age; 40 } 41 public int getage(){ 42 return age; 43 } 44 public String getname(){ 45 return name; 46 } 47 public int compareTo(Object obj) { 48 if(!(obj instanceof student2)){ 49 throw new RuntimeException("do not stu"); 50 } 51 student2 stu=(student2)obj; 52 if(this.age>stu.getage()){ 53 return 1; 54 } 55 if(this.age==stu.getage()){ 56 return this.name.compareTo(stu.getname()); 57 } 58 return -1; 59 } 60 }