cascade在java_【Java基础】集合
一、Collection接口(超級接口:Iterator)
其下有兩個子接口,Set和List。
1)Set接口的兩個常用子類
TreeSet:有序存放
HashSet:散列存放
2)List接口(允許重復元素)
常用子類:LinkedList、ArrayList、Vector。ArrayList是List接口最常用的一個子類。LinkedList完成的是一個鏈表操作。
二、Map接口
實現(xiàn)子類:HashMap、HashTable、TreeMap等
TreeMap與TreeSet類似,TreeMap中元素根據(jù)key自動排序。
三、集合元素為自定義類時,集合的排序問題及
輸出
自定義類作為集合元素時,集合本身不知道如何排序,必須類實現(xiàn)Comparable接口,并覆寫compareTo()方法。
public class Person implements Comparable {
private String name;
private int age;
public Person(String name, int age) {
this.age = age;
this.name = name;
}
@Override
public int compareTo(Person o) {//按名字順序排序
return this.name.compareTo(o.name);
}
@Override
public String toString() {
return this.name + ":" + this.age;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.ListIterator;
public class Test001 {
public static void main(String[] args) {
ArrayList person = new ArrayList();
person.add(new Person("何明", 20));
person.add(new Person("小米", 22));
person.add(new Person("Jane", 21));
person.add(new Person("Katty", 20));
person.add(new Person("Blue Key",20));
person.add(new Person("喬布斯",20));
Collections.sort(person); //排序
Iterator ite=person.iterator(); //Iterator迭代
while(ite.hasNext())
System.out.println(ite.next());
for(Person p:person) //foreach遍歷
System.out.println(p);
System.out.println(person);
ListIterator lite= person.listIterator();//ListIterator迭代
while(lite.hasNext()) //從前往后遍歷
while(lite.hasPrevious()) //從后向前比遍歷,此操作需要從前往后遍歷一趟
System.out.println(lite.previous());
}
}
//輸出結果:
//[Blue Key:20, Jane:21, Katty:20, 喬布斯:20, 何明:20, 小米:22]
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class MapDemo {
public static void main(String[] args) {
Map map = new TreeMap<>(); //按key排序
map.put("諸葛亮", 3);
map.put("孫悟空", 5);
map.put("愛迪生", 8);
map.put("孔乙己", 11);
map.put("豬八戒", 7);
Set> tree = map.entrySet();
Iterator> ite = tree.iterator();
while(ite.hasNext()) {
Map.Entry e=ite.next();
System.out.println(e.getKey()+"-->"+e.getValue());
}
for (Map.Entry entry : map.entrySet()) {
System.out.print(entry.getKey() + "-->" + entry.getValue()+" ");
//孔乙己-->11 孫悟空-->5 愛迪生-->8 豬八戒-->7 諸葛亮-->3
}
}
}
四、Map的實現(xiàn)子類中,當鍵為自定義類時,要想通過一個匿名對象找到值,則需要覆寫equals()、hashCode()方法區(qū)分是否同一個對象。
//構造一個Person類
public class Person implements Comparable {
public Person(String name, int age) {
this.age = age;
this.name = name;
}
@Override
public int compareTo(Person o) {
return this.name.compareTo(o.name);
}
@Override
public String toString() {
return this.name + ":" + this.age;
}
@Override
public boolean equals(Object obj) {//覆寫equals()方法
if (this == obj) {
return true;
} else if (!(obj instanceof Person)) {
return false;
}
Person p = (Person) obj;
if (this.name == p.name && this.age == p.age) {
return true;
} else {
return false;
}
}
@Override
public int hashCode(){ //覆寫hashCode()方法
//公式:Person.hashcode=name.hashcode*age
return this.name.hashCode()*this.age;
}
private String name;
private int age;
}
//測試代碼
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class MapDemo01 {
public static void main(String[] args) {
Map map = new TreeMap<>();
map.put(new Person("諸葛亮", 9), "zgl");
map.put(new Person("孫悟空", 5), "swk");
map.put(new Person("愛迪生", 8), "ads");
map.put(new Person("孔乙己", 11), "kyj");
map.put(new Person("豬八戒", 7), "zbj");
/*
* 通過一個Person類的匿名對象找到value值
* 需要覆寫Person類的hashCode()方法與equals()方法
*/
System.out.println(map.get(new Person("諸葛亮", 9)));
//輸出TreeMap
Set> tree = map.entrySet();
Iterator> ite = tree.iterator();
while (ite.hasNext()) {
Map.Entry e = ite.next();
System.out.println(e.getKey() + "-->" + e.getValue());
}
//foreach輸出
for (Map.Entry entry : map.entrySet()) {
System.out.println(entry.getKey() + "-->" + entry.getValue());
}
}
}
五、總結
1、元素是否具有排序:只有樹狀的集合(比如TreeSet和TreeMap)才能對自定義的對象和自然元素進行排序,其他都沒有排序。
2、元素的類型是否一致:只要具有排序的集合,里邊元素的類型必須統(tǒng)一,而且不能為null值。(只有TreeSet和TreeMap里邊的元素必須統(tǒng)一,其他集合都可以是任意的類型)
3、元素是否具有排重:只有Set和Map具有排重,List是沒有排重的。
4、從數(shù)據(jù)是否安全:只有Vector和HashTable(Map的實現(xiàn)類)是線程安全的,其他都是不安全的。
總結
以上是生活随笔為你收集整理的cascade在java_【Java基础】集合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java集合类详解和使用_Java 集合
- 下一篇: java中junit_【Java】Jun