------- android培訓、java培訓、期待與您交流! ----------
集合的用途:集合用來可以用來存儲對象,可以簡單的理解成數組
集合和數組的區別:
集合:1、用來存儲對象 2、長度可變3、可以存儲任意數據類型
數組:1、用來存儲基本數據類型 2、數組一旦創建,不可改變
爲什麼會出現這麼多種容器呢??因爲每一種數據對數據存儲的方式不同,這種方式稱之爲:數據結構。
由圖知道:Collection下面有2個子類,一個是 List ,一個是 set。他們有什麼區別?
List:元素是有序的,允許元素重複,因爲他們都自動帶有索引。
Set:元素是無序的,不允許元素重複。沒有索引。
Collection方法:(集合中存儲的都是對象的引用,地址值)
1、添加元素:
①add(Object) :add參數類型 是Object
②addAll(Collection子類):將集合添加到你另一個集合中
2、獲取長度: int size()
3、刪除元素:
①remove(Object):刪除集合中指定元素
②clear():清空集合
4、判斷元素:
①contains(Object):判斷某個元素是否存在
②containsAll(Collection):判斷某一個集合中的所有元素是否存在
③isEmpty():判斷集合是否爲空(其實就是判斷他的size() 是否等於0)
5、取交集 和 不交集
①retainAll(Collection):取2個集合中相同元素——取交集
②removeAll(Collection):取2個集合中不相同元素——(移Collection中的元素)
6、獲取集合中的元素,List ,Set 通用方法:iterator();
List:元素是有序的,元素可以重複,自動帶索引。List下的常用子類:List集合3種遍歷方式(1、iterator 2、ListIterator 3、通過循環獲取size。然後通過角標進行get查詢)
|--ArrayList :數組結構,特點:查詢、修改速度快;增刪稍慢,線程不同步
|--LinkedList:鏈表結構,特點:增刪速度快;查詢、刪除稍慢
|--Vector:數組結構,特點:查詢、修改速度快,增刪稍慢,程序同步
ArrayList獨有方法:
增:①add(index,element):在指定位置插入元素 ②add(index,Collection):在指定位置插入集合
改:①set(index,element):修改指定位置元素
查:①get(index):根據索引返回元素 ②subList(start,end):返回指定位置的集合,包含頭不包含尾 ③listIterator():List集合特有,列表迭代器
刪:①remove(index):根據索引刪除元素。
Collection的迭代器只能刪除元素,而listIterator則可以在迭代過程中增刪改查。
去除ArrayList集合中重複元素:
public class Collection002
{
public static void main(String[] args)
{
ArrayList<String> aList = new ArrayList<>();
aList.add("java001");
aList.add("java002");
aList.add("java003");
aList.add("java001");
aList.add("java003");
System.out.println("原集合:" + aList);
ArrayList<String> newList = removeAgain(aList);
System.out.println("先集合:" + newList);
}
public static ArrayList<String> removeAgain(ArrayList<String> aList)
{
ArrayList<String> aaList = new ArrayList<>(); //定義臨時容器
Iterator<String> it = aList.iterator();
while(it.hasNext())
{
String str = it.next();
if(aaList.contains(str)) //將拿到的元素,拿到臨時容器中查詢,有就繼續循環,沒有就添加。
continue;
aaList.add(str);
}
return aaList;
}
}
import java.util.ArrayList;
import java.util.Iterator;
/*將自定義對象元素存到ArrayList集合中,並去除重複元素
*
* */
public class Collection003
{
public static void main(String[] args)
{
ArrayList<Proson> aList = new ArrayList<>();
aList.add(new Proson("小張",45));
aList.add(new Proson("小張",44));
aList.add(new Proson("小張",45));
aList.add(new Proson("小花",47));
aList.add(new Proson("小張",45));
aList.add(new Proson("小花",47));
System.out.println("原集合" + aList);//直接打印底層調用了 該類的 toString();
ArrayList<Proson> aaList = removeAgain(aList);
System.out.println("現集合" + aaList);
}
public static ArrayList<Proson> removeAgain(ArrayList<Proson> aList)
{
ArrayList<Proson> aaList = new ArrayList<>();
Iterator<Proson> it = aList.iterator();
while(it.hasNext())
{
Proson p = it.next();
if(aaList.contains(p)) //而contains呢,調用了 equals方法。
continue;
aaList.add(p);
}
return aaList;
}
}
class Proson
{
private String name;
private int age;
Proson(String name,int age)
{
this.name = name;
this.age = age;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Proson)) //類型不相同,肯定不是同一個事物
return false;
Proson p = (Proson)obj;
return this.name.equals(p.name) && this.age == p.age; //名字和年齡都相同的話,那肯定是同一個人
}
public String toString()
{
return name + "::" + age;
}
}
set:元素是無序的,元素不能重複,沒有自帶索引
|--hashSet:哈希表結構,保證唯一性的關鍵(hashCode 和 equals),首先對比hashCode,hashCode一樣,才比較equals。
|--TreeSet:二叉樹 結構,保證唯一性的關鍵( Comparator 接口中的 Compare() 和 Compareble 接口中的 CompareTo()),用來排序
將自定義對象存入到hashSet中,並確保唯一性:
import java.util.HashSet;
/*將自定義對象存入到HashSet中,並確保唯一性
* */
public class Collection004
{
public static void main(String[] args)
{
HashSet<Student1> hhs = new HashSet<>();
hhs.add(new Student1("小馬",20));
hhs.add(new Student1("小黑",20));
hhs.add(new Student1("小劍",20));
hhs.add(new Student1("小馬",20));
System.out.println(hhs);
}
}
class Student1
{
private String name;
private int age;
Student1(String name,int age)
{
this.name = name;
this.age = age;
}
public int hashCode()
{
return name.hashCode() + age*3 ; //覆蓋hashCode的時候,最好根據他的成員屬性來返回值
}
public boolean equals(Object obj)
{
if(!(obj instanceof Student1)) //類型不相同,肯定不是同一個事物
return false;
Student1 p = (Student1)obj;
return this.name.equals(p.name) && this.age == p.age; //名字和年齡都相同的話,那肯定是同一個人
}
public String toString()
{
return name + "::" + age;
}
}
import java.util.Comparator;
import java.util.TreeSet;
/*由於Set都是無序的,但是TreeSet提供了排序功能,但是需要實現接口
* 第一種:類自己實現 Compareble接口
* 第二種:我在創建TreeSet集合的時候,實現 Comparator 比較器接口
* */
public class Collection004
{
public static void main(String[] args)
{
TreeSet<Student1> hhs = new TreeSet<>(new Comparator<Student1>(){//內部類:通過構造函數實現接口,並覆蓋方法
public int compare(Student1 s1,Student1 s2)
{
int age = s1.getAge() - s2.getAge(); //我是通過升序,如果要降序 就換成 s2 - s1
if(age==0) //如果年齡相同,我們還得比較姓名
return s1.getName().compareTo(s2.getName());
return age;
}
});
hhs.add(new Student1("小馬",30));
hhs.add(new Student1("小黑",20));
hhs.add(new Student1("小劍",10));
hhs.add(new Student1("小馬",20));
hhs.add(new Student1("小名",19));
hhs.add(new Student1("小名",19));
System.out.println(hhs);
}
}
class Student1
{
private String name;
private int age;
Student1(String name,int age)
{
this.name = name;
this.age = age;
}
/* public int hashCode()
{
return name.hashCode() + age*3 ; //覆蓋hashCode的時候,最好根據他的成員屬性來返回值
}
public boolean equals(Object obj)
{
if(!(obj instanceof Student1)) //類型不相同,肯定不是同一個事物
return false;
Student1 p = (Student1)obj;
return this.name.equals(p.name) && this.age == p.age; //名字和年齡都相同的話,那肯定是同一個人
}*/
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()
{
return name + "::" + age;
}
}