Collection接口
常用方法
boolean add()向集合添加元素,该方法的形参要求是Object类型package Collection1; import org.junit.Test; import java.util.*; /** * @Author: 张辉 * @Description: * @Date Created in 2024-01-29 8:35 * @Modified By: */ public class CollectionAdd { @Test public void collectionTest(){ //接口类型的引用指向实现的对象,形成多态 Collection collection = new ArrayList(); collection.add("我上早八"); collection.add(6); collection.add('c'); collection.add(6.66); collection.add("happy"); System.out.println(collection); //判断是否包含对象 System.out.println(collection.contains(6)); System.out.println(collection.contains("n")); //判断集合是否为空 System.out.println(collection.isEmpty()); //从集合中删除对象 collection.remove(6); System.out.println(collection); //返回包含对象的个数 System.out.println(collection.size()); //将集合转换成数组 Object[] objects = collection.toArray(); System.out.println(objects); //遍历数组中的元素 for(int i = 0;i<objects.length;i++){ System.out.println(objects[i]); } //将数组转换成集合 List ob1 = Arrays.asList(objects); System.out.println(ob1); //清空集合 collection.clear(); System.out.println(collection); } }
Iterator对象为迭代器,主要用于遍历Collection中的元素,不能遍历Map中的元素
使用foreach循环遍历集合,可以迭代Collection和集合不能遍历Map不需要引索访问
package Collection1; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; /** * @Author: 张辉 * @Description: * @Date Created in 2024-01-29 9:54 * @Modified By: */ public class Iterator01 { @Test public void iterator01(){ //接口类型的引用指向实现的对象,形成多态 Collection collection = new ArrayList(); collection.add("我上早八"); collection.add("为什么寒假要上早八"); collection.add("zb为什么这么**"); collection.add("哈哈哈哈"); collection.add("happy"); System.out.println(collection); System.out.println("------------------------------------"); //通过迭代器遍历集合中的元素,方式2 Iterator it = collection.iterator(); while (it.hasNext() == true){ Object ob = it.next(); System.out.println(ob); } System.out.println("------------------------------------"); //遍历集合中的元素,方式三 for(Object o : collection){ System.out.println(o); } } }
List接口(Collection子接口)
先进后出
Vector从jdk1.0开始(线程不安全)(被抛弃)
Collection从1.2开始
ArrayList从1.2开始(线程不安全)
如果需要线程安全的List集合时synchronizedList(List<T>list)或者
CopyOnWriteArrayList<E>
常用方法package Collection1; import org.junit.Test; import java.util.ArrayList; import java.util.List; /** * @Author: 张辉 * @Description: * @Date Created in 2024-01-29 10:55 * @Modified By: */ public class List01 { @Test public void ListTest(){ List list = new ArrayList(); System.out.println(list.size()); System.out.println(list); list.add(0,"笑了"); list.add(1,2); list.add(2,3.12); list.add(3,"笑了"); System.out.println(list); List list1 = new ArrayList(); list1.add("c"); list.addAll(list1); System.out.println(list); System.out.println("---------------------"); //根据下标查看集合中指定位置的元素 Object o = list.get(2); System.out.println(o); System.out.println("---------------------"); //根据下标修改集合中置顶位置的元素 o = list.set(0,6); System.out.println(o); System.out.println(list); System.out.println("---------------------"); //根据下标删除指定位置的元素 o = list.remove(1); System.out.println(o); System.out.println(list); System.out.println("---------------------"); //获取子List段,在改变子List会改变List List list2 = list.subList(0,3); System.out.println(list); System.out.println(list2); list2.remove(0); System.out.println(list); System.out.println(list2); } }
泛型机制
本质就是参数化类型,也就是让数据类型作为参数传递
Queue接口
该集合的主要用于描述具有先进先出特征的数据结构,叫做队列
该集合主要实现类是LinkedList类,因为该类在增删方面比较有优势
package Collection1; import org.junit.Test; import java.util.LinkedList; import java.util.Queue; /** * @Author: 张辉 * @Description: * @Date Created in 2024-01-29 17:17 * @Modified By: */ public class Queue01 { @Test public void QueueTest(){ Queue<Integer> q = new LinkedList<>(); //入队 for(int i=1;i<6;i++){ q.offer(i*11); } System.out.println(q); System.out.println("--------------------------"); //查看队首元素并打印 Integer it = q.peek(); System.out.println(it); System.out.println("--------------------------"); //将队列中所有的元素依次出队并打印 int len = q.size();//不用len的话q越来越短,只能输出1/2的数 for(int i = 1;i<=len;i++){ Integer it1 = q.poll(); System.out.println(it1); } } }
Stack
package Collection1; import org.junit.Test; import java.util.Stack; /** * @Author: 张辉 * @Description: * @Date Created in 2024-01-29 18:04 * @Modified By: */ public class StackTest01 { @Test public void StackTest(){ Stack<Integer> st = new Stack<>(); for(int i=1;i<6;i++){ st.add(i*11); } System.out.println(st);//入栈并打印 System.out.println("--------------"); Integer it = st.peek(); System.out.println(it); System.out.println("--------------"); int len = st.size(); for(int i=0;i<len;i++){ Integer it1 = st.pop(); System.out.println(it1); } } }
Set接口(HashSet类 TreeSet类)
该集合中元素没有先后放入次序,不允许重复,该集合主要的实现类是:HashSet(哈希表)和TreeSet(二叉树)类@Test public void SetTAdd01(){ Set<String> set = new HashSet<>(); set.add("早上嚎"); System.out.println(set); set.add("大声发"); System.out.println(set); set.add("做唛阿"); System.out.println(set); set.add("早上嚎"); System.out.println(set); }
//随即生成10个1-20的数放入集合并打印 @Test public void randomSetTest(){ Random ra = new Random(); Set<Integer> set = new HashSet<>(); Set<Integer> set1 = new HashSet<>(); for(int i=0;set.size()<10;i++){ set.add(ra.nextInt(20)+1); } for(int i=0;i<10;i++){ set1.add(ra.nextInt(20)+1); } System.out.println(set); System.out.println(set1); }
@Test public void SetTest(){ Set<Object> set = new HashSet<>(); Set<Object> set1 = new HashSet<>(); //判断是否为空 System.out.println(set.isEmpty()); System.out.println("--------------------------------"); //将一个set里面的元素全部加到另一个set里面 set.add("zh"); set1.add("真nb"); set1.add("hh"); System.out.println(set); System.out.println(set1); set.addAll(set1); System.out.println(set); System.out.println("--------------------------------"); //如果set中存在指定的元素,则将其删除 set.remove("hh"); System.out.println(set); System.out.println("--------------------------------"); //仅保留set中包含在collection中的元素 set.retainAll(set1); System.out.println(set); System.out.println("--------------------------------"); //如果set包含指定元素则返回true System.out.println(set.contains("nb")); System.out.println("--------------------------------"); }
Map接口
Map<K,V>
K-此映射所维护的键(Key)的类型,相当于目录
V-映射值(Value)的类型,相当于内容
该集合key不可以重复,而且一个key只能对应一个value
该集合主要实现(HashMap,TreeMap)@Test public void hashMapTest(){ Map<Integer,String> map = new HashMap<>(); //向集合中添加元素 map.put(1,"张辉"); map.put(2,"zh"); map.put(3,"真nb"); map.put(4,"nb"); System.out.println(map); System.out.println("---------------------------"); System.out.println(map.get(1)); System.out.println("---------------------------"); System.out.println(map.equals("zh")); System.out.println("---------------------------"); System.out.println(map.keySet()); System.out.println("---------------------------"); System.out.println(map.containsKey(1)); System.out.println(map.containsValue("zh")); System.out.println("---------------------------"); System.out.println("删除的值为"+map.remove(2)); System.out.println(map); }
遍历方式
方式一:自动调用toString方法
方式二:使用keySet方法遍历Map集合中的元素
方式三:调用entrySet方法变脸Map集合中的元素
@Test public void iteratorMapTest(){ Map<Integer,String> map = new HashMap<>(); //向集合中添加元素 map.put(1,"张辉"); map.put(2,"zh"); map.put(3,"真nb"); map.put(4,"nb"); //Map集合遍历,方式1 System.out.println(map); System.out.println("-----------------------------------"); //方式二:使用keySet方法遍历Map集合中的元素 Set<Integer> set = map.keySet(); for(Integer it:set){ System.out.println(map.get(it)); } System.out.println("-----------------------------------"); //调用entrySet方法变脸Map集合中的元素,方式三 Set <Map.Entry<Integer,String>> entries = map.entrySet(); for(Map.Entry<Integer,String> e: entries){ System.out.println(e); } }