集合框架
1.Collection集合
import org.junit.Test;
import java.util.*;
public class CollectionTest {
@Test
public void collectionTest() {
//接口类型的引用指向实现类的对象,形成多态
Collection collection = new ArrayList();
System.out.println(collection);//[]
//向集合中添加元素,该方法的形参要求是Object类
boolean b = collection.add("one");//[one]
System.out.println("b = " + b);//true
collection.add(2);//[one, 2]
System.out.println(collection);
collection.add('a');//[one, 2, a, 3.14, 高]
System.out.println(collection);
collection.add(3.14);
System.out.println(collection);
collection.add("高");
System.out.println(collection);
System.out.println("================================================");
//判断是否包含对象
b = collection.contains("two");
System.out.println("判断集合是否有two这个个元素");
System.out.println("b = " + b);//false
System.out.println("判断集合是否有one这个元素");
b = collection.contains("one");
System.out.println("b = " + b);//true
//判断是否为空
System.out.println("判断集合是否为空");
b = collection.isEmpty();
System.out.println("b = " + b);//false
System.out.println("删除前集合中的元素有:" + collection);
//删除其中的元素
System.out.println("删除one");
b = collection.remove("one");
System.out.println("删除后集合中的元素有:" + collection);
System.out.println("================================================");
//将集合转化成数组
System.out.println("转换成数组为:");
Object[] objects = collection.toArray();
//遍历数组中的元素
for (int i = 0; i < objects.length; i++) {
System.out.print(objects[i] + " ");
}
System.out.println();
//将数组转化成集合
System.out.println("转换回集合为:");
List objects1 = Arrays.asList(objects);
System.out.println(objects1);
System.out.println("================================================");
//迭代器遍历集合中元素
System.out.println("迭代器遍历后:");
Iterator it = objects1.iterator();
//b = it.hasNext();
//System.out.println("b = " + b);//true
// Object obj = it.next();
// System.out.println("元素:" + obj);
while (it.hasNext() == true) {
Object obj = it.next();
System.out.print(obj + " ");
}
System.out.println();
System.out.println("================================================");
//遍历集合中的元素方式三
System.out.println("foreach遍历后:");
for (Object o : objects1) {
System.out.print(o + " ");
}
}
}
2.List集合
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class ListTest {
@Test
public void listTest() {
List list = new ArrayList();
int size = list.size();
System.out.println(size);//0
System.out.println(list);//[]
//向集合中添加元素
System.out.println("添加元素后:");
list.add(0, "one");
list.add(1, 2);
list.add(2, '3');
list.add(3, 3.14);
System.out.println(list);
List list1 = new ArrayList();
list1.add("two");
list1.add(10);
//将list1中元素添加在list中
System.out.println("将list1中元素添加在list中");
list.addAll(list1);
System.out.println(list);
//根据下标查看集合中指定位置的元素
System.out.println("查看集合中位置3的元素");
Object o = list.get(3);
System.out.println(o);
//根据下标修改集合中指定位置的元素
o = list.set(0, 1);
System.out.println("将位置0修改改为1");
System.out.println(list);
//根据下标删除集合中指定位置的元素
o = list.remove(0);
System.out.println("将集合中" + o + "删除");
System.out.println(list);
List list2 = list.subList(0, 3);
System.out.println("list集合" + list);
System.out.println("子集合中的元素" + list2);
list2.remove(0);
System.out.println("list集合" + list);
System.out.println("子集合中的元素" + list2);
}
}
3.泛型机制
@Test
public void collectionTest02(){
Collection<String> collection = new ArrayList<>();
collection.add("AA");
collection.add("BB");
collection.add("CC");
collection.add("DD");
System.out.println(collection);
}
4.Queue集合
import java.util.LinkedList;
import java.util.Queue;
public class QueueTest {
public static void main(String[] args) {
//准备一个Queue集合
Queue<Integer> queue = new LinkedList<>();
//将数据11,22,33,44,55依次入队并打印
for (int i = 1; i <= 5; i++) {
queue.offer(i * 11);
System.out.println(queue);
}
//查看队首元素
Integer it = queue.peek();
System.out.println("队首元素:" + it);
//将队列中所有数据依次出对并打印
int len = queue.size();
for (int i = 1; i <= len; i++) {
Integer it1 = queue.poll();
System.out.println(it1);
}
}
}
5. Stack
***先进后出
import java.util.Stack;
public class StackTest {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
//测试堆栈是否为空
System.out.print("测试堆栈是否为空:");
System.out.println(stack.empty());
// 入栈并打印
for (int i = 1; i <= 5; i++) {
stack.push(i * 11);
System.out.println(stack);
}
System.out.println("入栈后栈内元素:" + stack);
//测试堆栈是否为空
System.out.print("测试堆栈是否为空:");
System.out.println(stack.empty());
// 查看栈顶元素并打印
System.out.println("栈顶元素为:" + stack.peek());
//返回对象在堆栈中的位置
System.out.println(stack.search(55));
//移除堆栈顶部的对象
stack.pop();
System.out.println(stack);
// 出栈并打印
System.out.println("依次出栈并打印:");
int len = stack.size();
for (int i = 1; i <= len; i++) {
Integer it1 = stack.pop();
System.out.println(it1);
}
}
}
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/2201_75588545/article/details/135904060