第4章 集合类

4.1初识集合

4.1.1 集合概述

也叫容器类,这些类可以存储任意类型的对象,而且长度可变

可分为两大类:单列集合和双列集合

Collection:单列集合类的根接口,用于存储一系列符合某种规则的元素,它有两个重要的子接口,分别是 List和 Set。其中,List的特点是元素有序,元素可重复;Set的特点是元素无序,而且不可重复。List接口的主要实现类有ArrayList和LinkedList,Set接口的主要实现类有HashSet和 TreeSet。

Collection:单列集合类的根接口,用于存储一系列符合某种规则的元素,它有两个重要的子接口,分别是 List和 Set。List的特点是元素有序,元素可重复;Set的特点是元素无序,而且不可重复。List接口的主要实现类有ArrayListLinkedList,Set接口的主要实现类有HashSetTreeSet

Map:双列集合类的根接口,用于存储具有(Key),(Value)映射关系的元素,每个元素都包含一对键值,在使用Map集合时可以通过指定的Key找到对应的Value,例如根据一个员工的工号就可以找到对应的员工。Map接口的主要实现类有HashMap和 TreeMap。(这样子看描述有点像python的元组)

4.1.2 Collection接口简介

方法声明 功能描述
boolean add(Object o) 向集合中添加一个元素
boolean addAll(Collection c) 将指定 Collection中的所有元素添加到该集合中
void clear() 删除该集合中的所有元素
boolean remove(Object o) 删除该集合中指定的元素
boolean removeAll( Collection c) 删除指定集合中的所有元素
boolean isEmpty() 判断该集合是否为空
boolean contains(Object o) 判断该集合中是否包含某个元素
boolean containsAll(Collection c) 判断该集合中是否包含指定集合中的所有元素
Iterator iterator() 返回在该集合的元素上进行迭代的迭代器(Iterator),用于遍历该集合所有元素
int size() 获取该集合元素的个数

4.2 List接口

4.2.1 List接口简介

是线性的

元素有序

方法声明 功能描述
void add(int index,Object element) 将元素element插入到List集合的index处
boolean addAll(int index,Collection c) 将集合c所包含的所有元素插人到 List集合的index处
Object get(int index) 返回集合索引 index处的元素
Object remove(int index) 删除 index索引处的元素
Object set(int index,Object element) 将索引index处元素替换成element对象,并将替换后的元素返回
int indexOf(Object o) 返回对象o在 List集合中出现的位置索引
int lastIndexOf(Object o) 返回对象o在 List集合中最后一次出现的位置索引
List subList(int fromIndex,int toIndex) 返回从索引fromIndex(包括)到toIndex(不包括)处所有元素集合组成的子集合

4.2.2 ArrayList 集合

package com.itheima.example;

import java.util.ArrayList;

public class Example01 {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add("zhangsan");
        list.add("lisi");
        list.add("wangwu");
        list.add("zhaoliu");
        System.out.println("集合的长度是:"+list.size());
        System.out.println("第2个元素是:"+list.get(2));
    }
}

4.2.3 lterator接口

迭代器用于遍历元素

hasNext()方法是判断是否存在下一个元素,存在调用next()方法将元素取出

package com.itheima.example;

import java.util.*;

public class Example02 {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add("zhangsan");
        list.add("lisi");
        list.add("wangwu");
        list.add("zhaoliu");
        Iterator it = list.iterator();
        while (it.hasNext()){
            Object obj = it.next();
            System.out.println(obj);
        }
    }
}

4.2.4 foreach循环

(增强for循环)

package com.itheima.example;

import java.util.*;

public class Example03 {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add("zhangsan");
        list.add("lisi");
        list.add("wangwu");
        list.add("zhaoliu");
        for(Object obj : list){
            System.out.println(obj);
        }
    }
}

4.3 接口

4.3.2 HashSet集合

package com.itheima.example;

import java.util.*;

public class Example05 {
    public static void main(String[] args) {
        HashSet hashSet = new HashSet();
        hashSet.add("zhangsan");
        hashSet.add("lisi");
        hashSet.add("wangwu");
        hashSet.add("lisi");
        Iterator it =hashSet.iterator();
        while(it.hasNext()){
            Object obj = it.next();
            System.out.println(obj);
        }
    }
}

4.4 Map接口(映射)

4.4.1 Map接口简介

方法声明 功能描述
void add(int index,Object element) 将元素element插入到List集合的index处
boolean addAll(int index,Collection c) 将集合c所包含的所有元素插入到List集合的index处
Object get(int index) 返回集合索引index处的元素
Object remove(int index) 删除 index索引处的元素
Object set(int index,Object element) 将索引 index处元素替换成element对象,并将替换后的元素
int indexOf(Object o) 返回对象o在 List集合中出现的位置索引
int lastIndexOf(Object o) 返回对象o在List集合中最后一次出现的位置索引
List subList(int fromIndex,int toIndex) 返回从索引fromIndex(包括)到 toIndex(不包括)处所有元素集合组成的子集合

4.4.2 HashMap集合

package com.itheima.example;

import java.util.*;

public class Example06 {
    public static void main(String[] args) {
        HashMap hashMap = new HashMap();
        hashMap.put("001","zhangsan");
        hashMap.put("002","lisi");
        hashMap.put("003","wnagwu");
        Set keySet=hashMap.keySet();
        Iterator iterator = keySet.iterator();
        while (iterator.hasNext()){
            Object key = iterator.next();
            Object value = hashMap.get(key);
            System.out.println(key+"="+value);
        }
    }
}

4.4.3 Properties集合

主要用于存储字符串类型的键和值

package com.itheima.example;

import java.util.Properties;
import java.util.Set;

public class Example08 {
    public static void main(String[] args) {
        Properties p = new Properties();
        p.setProperty("face", "微软雅黑");
        p.setProperty("size","20px");
        p.setProperty("color","green");
        Set<String>names = p.stringPropertyNames();
        for(String key : names){
            String value =p.getProperty(key);
            System.out.println(key+"="+value);
        }
    }
}

setProperty():将配置项的键和值添加到集合中

getProperty():根据键获取对应的值

stringPropertyNames():得到一个所有键的集合


标题:Python
作者:肖祺彦
地址:url