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