前言–java類集是開發中用得最多的了吧,方便,功能強。
一、類集框架主要接口
接口的繼承關係:
1.List接口—List中允許有重複的數據
List
class Student{
private String name;
private String age;
//無參和有參的構造
getter/setter...
}
public class Test{
public static void main(String[] args){
Collection<Student> c=new ArrayList<Student>();
c.add(new Student("劉歡",10));//Object obj=new Student("劉歡",10);
c.add(new Student("趙四",11));
c.add(new Student("王五",12));
//把集合轉爲數組
Object[] o=c.toArray();
for(int i=0;i<o.length;i++){
System.out.println(o[i]);
//如果要根據集合中的名字取值則:
Student s=(Student)o[i];//向下轉型即可
System.out.println("姓名:"s.getName()+'\t'+"年齡:"+s.getAge());
}
//直接使用迭代(遍歷)輸出
Iterator it=c.iterator();
if(it.hasNext()){
System.out.println(it.next());//遍歷輸出
//如果想直接輸出名字或者年齡,則需要向下轉型
Student stu=(Student)it.next();
System.out.println("姓名:"stu.getName()+'\t'+"年齡:"+stu.getAge());
}
}
}
1.1.LinkedList
LinkedList:表示的是一個鏈表的操作類.
import java.util.LinkedList ;
public class LinkedListDemo02{
public static void main(String args[]){
LinkedList<String> link = new LinkedList<String>() ;
link.add("A") ; // 增加元素
link.add("B") ; // 增加元素
link.add("C") ; // 增加元素
System.out.println("1-1、element()方法找到表頭:" + link.element()) ;
System.out.println("1-2、找完之後的鏈表的內容:" + link) ;
System.out.println("2-1、peek()方法找到表頭:" + link.peek()) ;
System.out.println("2-2、找完之後的鏈表的內容:" + link) ;
System.out.println("3-1、poll()方法找到表頭:" + link.poll()) ;
System.out.println("3-2、找完之後的鏈表的內容:" + link) ;
}
};
1.2Queue接口:是Collection的子接口,定義如下:public interface Queue extends Collection
import java.util.LinkedList ;
public class LinkedListDemo03{
public static void main(String args[]){
LinkedList<String> link = new LinkedList<String>() ;
link.add("A") ; // 增加元素
link.add("B") ; // 增加元素
link.add("C") ; // 增加元素
System.out.print("以FIFO的方式輸出:") ;
for(int i=0;i<=link.size()+1;i++){
System.out.print(link.poll() + "、") ;
}
}
};
2.Set接口–不能加入重複的元素
Set接口常用的子類:
2.1散列存放(無序):HashSet
import java.util.HashSet ;
import java.util.Set ;
public class HashSetDemo01{
public static void main(String args[]){
Set<String> allSet = new HashSet<String>() ;
allSet.add("A") ; // 增加內容
allSet.add("B") ; // 增加內容
allSet.add("C") ; // 增加內容
allSet.add("C") ; // 重複內容
allSet.add("C") ; // 重複內容
allSet.add("D") ; // 增加內容
allSet.add("E") ; // 增加內容
System.out.println(allSet) ;
}
};
2.2有序存放(可自動排序):TreeSet
import java.util.TreeSet ;
import java.util.Set ;
public class TreeSetDemo01{
public static void main(String args[]){
Set<String> allSet = new TreeSet<String>() ;
allSet.add("C") ; // 增加內容
allSet.add("C") ; // 重複內容
allSet.add("C") ; // 重複內容
allSet.add("D") ; // 增加內容
allSet.add("B") ; // 增加內容
allSet.add("A") ; // 增加內容
allSet.add("E") ; // 增加內容
System.out.println(allSet) ;
}
};
深入:使用hashCode(),equals(),取消掉重複元素,
import java.util.Set ;
import java.util.HashSet ;
class Person{
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public boolean equals(Object obj){ // 覆寫equals,完成對象比較
if(this==obj){
return true ;
}
if(!(obj instanceof Person)){
return false ;
}
Person p = (Person)obj ; // 向下轉型
if(this.name.equals(p.name)&&this.age==p.age){
return true ;
}else{
return false ;
}
}
public int hashCode(){
return this.name.hashCode() * this.age ; // 定義一個公式
}
public String toString(){
return "姓名:" + this.name + ";年齡:" + this.age ;
}
};
public class RepeatDemo02{
public static void main(String args[]){
Set<Person> allSet = new HashSet<Person>() ;
allSet.add(new Person("張三",30)) ;
allSet.add(new Person("李四",31)) ;
allSet.add(new Person("王五",32)) ;
allSet.add(new Person("王五",32)) ;
allSet.add(new Person("王五",32)) ;
allSet.add(new Person("趙六",33)) ;
allSet.add(new Person("孫七",33)) ;
System.out.println(allSet) ;
}
};