Java 学习之第 4 章 集合类

本贴最后更新于 822 天前,其中的信息可能已经事过景迁

第 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():得到一个所有键的集合

  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3167 引用 • 8207 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...