JAVA集合框架中的接口
1.Collection接口偷俭。該接口定義了一個存取一組對象的方法,是最基本的接口
2.Set接口衩婚。該接口繼承Collection接口剔应,它包含的數(shù)據(jù)沒有順序且不可以重復(fù)
3.List接口。該接口繼承Collection接口熬拒,它包含的數(shù)據(jù)有順序且可以重復(fù)
4.Map接口爷光。該接口是一個單獨的接口,不繼承Collection接口澎粟。它是一種把鍵對象和值對象進行關(guān)聯(lián)的容器蛀序,不可以包含重復(fù)鍵
JAVA集合中的實現(xiàn)類
1.HashSet欢瞪。實現(xiàn)了Set接口,為無序集合徐裸,能夠快速定位一個元素遣鼓。需要注意的是,存入HashSet中的對象必須實現(xiàn)HashCode()方法
2.TreeSet重贺。不僅實現(xiàn)了Set接口骑祟,還實現(xiàn)了Sorted接口,可對集合進行自然排序(即升序排序)
3.ArrayList气笙。實現(xiàn)了List接口次企,為有序集合,它的大小可變并且可以像鏈表一樣被訪問潜圃。它是以數(shù)組的方式實現(xiàn)的List接口缸棵,允許快速隨機存取
4.LinkedList。實現(xiàn)了List接口谭期,為有序集合堵第,通過一個鏈表的形式實現(xiàn)List接口,提供最佳順序存取崇堵,適合插入和移除元素型诚。有這個類定義的鏈表也可以像棧或隊列一樣被使用
5.HashMap鸳劳。實現(xiàn)一個“鍵-值”映射的哈希表狰贯,通過鍵獲取值對象,沒有順序赏廓,通過get(key)方法來獲取value的值涵紊,允許存儲空對象,而且允許鍵是空的幔摸。不過摸柄,鍵只能有一個
Collection接口中的方法
Collection常用方法
舉栗子:
package com.moxiaofeng2;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionTest {
public static void main(String[] args) {
//創(chuàng)建集合C
Collection collection =new ArrayList();
//向集合中添加元素
collection.add("Apple");
collection.add("Orange");
collection.add("Banana");
collection.add("Pear");
//創(chuàng)建Array集合
ArrayList arrayList =new ArrayList();
//向集合中添加元素
arrayList.add("Dog");
arrayList.add("Cat");
System.out.println("集合collection中元素個數(shù):" + collection.size());
//如果arraylist集合不為空
if (!arrayList.isEmpty()){
//將集合array里面的元素添加到集合collection中
collection.addAll(arrayList);
}
System.out.println("集合collection中元素個數(shù):" + collection.size());
//返回迭代器
Iterator iterator = collection.iterator();
System.out.println("集合collection中元素:");
//判斷迭代器中是否存在下一個元素
while (iterator.hasNext()){
//使用迭代器循環(huán)輸出集合中的元素
System.out.print(iterator.next() +" ");
}
System.out.println();
//判斷集合中是否包含Cat
if (collection.contains("Cat")) {
System.out.println("-------集合collection中包含Cat--------");
}
//從集合collection中刪除集合arrayList中的所有元素
collection.removeAll(arrayList);
//返回迭代器對象
iterator = collection.iterator();
System.out.println("集合collection中元素:");
while(iterator.hasNext()){
System.out.print(iterator.next() +" ");
}
System.out.println();
//將集合中的元素存放到字符串數(shù)組中
Object[] objects = collection.toArray();
String s ="";
System.out.println("素組中元素:");
for (int i =0; i < objects.length; i++){
//String strings = new String();
//strings = objects[i].toString();
s = (String)objects[i];
//System.out.print(strings + " ");
System.out.print(s +" ");
}
}
}
List接口中的方法
List常用方法
舉栗子:
package com.moxiaofeng2;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ArrayListTest {
public static void main(String[] args) {
//創(chuàng)建初始容量為10的空列表
ArrayList array = new ArrayList();
//添加元素
array.add("cat");
array.add("dog");
array.add("pig");
array.add("sheep");
array.add("pig");
//創(chuàng)建迭代器
Iterator iterator = array.iterator();
System.out.println("array集合中的元素有:");
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
System.out.println();
//替換指定索引處的元素
System.out.println("返回替換集合中索引是1的元素:" + array.set(1,"mouse"));
iterator = array.iterator();
System.out.println("元素替換后集合中的元素:");
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
System.out.println();
//獲取指定索引處集合的元素
System.out.println("獲取集合中索引是2的元素:" + array.get(2));
System.out.println("集合中第一次出現(xiàn)pig的索引:" + array.indexOf("pig"));
System.out.println("集合中最后一次出現(xiàn)dog的索引:" + array.lastIndexOf("pig"));
List list = array.subList(1,4);
iterator = list.iterator();
System.out.println("新集合中的元素");
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
}
}
LinkedList類的方法
LinkedList常用方法
舉栗子:
package com.moxiaofeng2;
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListTest {
public static void main(String[] args) {
//創(chuàng)建LinkedList集合對象
LinkedList linkedList = new LinkedList();
//添加元素
linkedList.add("cat");
linkedList.add("dog");
linkedList.add("pig");
linkedList.add("sheep");
linkedList.addFirst("mouse");
linkedList.addLast("duck");
//創(chuàng)建迭代器遍歷集合元素
Iterator iterator = linkedList.iterator();
System.out.println("linkedList集合中的元素分別有:");
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("獲取集合中第一個元素:" + linkedList.getFirst());
System.out.println("獲取集合中最后一個元素:" + linkedList.getLast());
System.out.println("刪除集合中第一個元素:" + linkedList.removeFirst());
System.out.println("刪除集合中最后一個元素:" + linkedList.removeLast());
iterator = linkedList.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
}
}
TreeSet類提供方法
TreeSet常用方法
舉栗子:
package com.moxiaofeng2;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args) {
//創(chuàng)建TreeSet集合
TreeSet treeSet = new TreeSet();
//添加元素
treeSet.add("45");
treeSet.add("32");
treeSet.add("55");
treeSet.add("87");
treeSet.add("89");
treeSet.add("34");
treeSet.add("99");
//獲取集合中元素個數(shù)
System.out.println("集合中元素一共有:" + treeSet.size());
//創(chuàng)建迭代器,遍歷集合
Iterator iterator = treeSet.iterator();
System.out.println("treeset中集合元素為:");
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("獲取34~89之間的集合:");
//System.out.print(treeSet.subSet("34","89"));
SortedSet sortedSet = treeSet.subSet("34","89");
iterator = sortedSet.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("集合中89之前的元素");
SortedSet sortedSet1 = treeSet.headSet("89");
iterator = sortedSet1.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("獲取45之后的數(shù)據(jù)");
SortedSet sortedSet2 = treeSet.tailSet("45");
iterator = sortedSet2.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("獲取集合中第一個元素:" + treeSet.first());
System.out.println("獲取集合中最后一個元素:" + treeSet.last());
System.out.println("獲取并移除集合中第一個元素:" + treeSet.pollFirst());
System.out.println("獲取并移除集合中最后一個元素:" + treeSet.pollLast());
//遍歷集合中的元素
iterator = treeSet.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
}
}
Map接口提供方法
Map常用方法
舉栗子:
package com.moxiaofeng2;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class HashMapTest {
public static void main(String[] args) {
//創(chuàng)建Map集合
HashMap hashMap = new HashMap();
//添加元素
hashMap.put("1","一代天驕");
hashMap.put("2","成吉思汗");
hashMap.put("3","只識彎弓射大雕");
hashMap.put("4","俱往矣");
hashMap.put("5","數(shù)風流人物");
hashMap.put("5","還看今朝");
System.out.println("指定鍵2獲取值:" + hashMap.get("2"));
Set set = hashMap.keySet();
Iterator iterator = set.iterator();
//獲取HashMap中值的合并輸出
String key = "";
while (iterator.hasNext()){
key = (String)iterator.next();
System.out.println(key + ":" + hashMap.get(key));
}
}
}