java 集合

1.集合框架

2.例子

java 集合

 

 

package com.ligy.studymvc;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;

@SpringBootTest
class StudymvcApplicationTests {
    @Test
    void test3() {
        Set<String> set = new HashSet<>();

        set.add("aa");
        set.add("bb");
        //增强for
        for (String s : set) {
            System.out.println("set:" + s);
        }

    }

    @Test
    void test2() {
        Map<String, Object> map = new HashMap<>();

        //1.put  get
        map.put("a", "aa");
        System.out.println("a:" + map.get("a"));
    }

    @Test
    void test1() {
        //多态
        Collection<String> collection = new ArrayList<>();

        //1.tostring()
        System.out.println(collection);

        //2.add
        collection.add("aa");
        collection.add("aa");
        collection.add("bb");
        collection.add("cc");
        System.out.println(collection);

        //3.remove
        collection.remove("aa");
        System.out.println(collection);

        //4.clear
        //collection.clear();
        //System.out.println(collection);

        //5.isEmpty
        if (collection.isEmpty()) {
            System.out.println("空");
        } else {
            System.out.println("非空");
        }

        //6.contains
        if (collection.contains("bb")) {
            System.out.println("包含:bb");
        } else {
            System.out.println("不包含:bb");
        }

        //7.size()
        System.out.println("size:" + collection.size());

        //8.get
        System.out.println("0:" + ((ArrayList<String>) collection).get(0));

        //9.迭代器。一个个读取,直到最后1个,
        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }


    }

}

 

上一篇:自考新教材p141-4


下一篇:输入一行数字字符,请用数组元素作为计数器来统计每个数字字符出现的个数。