Java教程

java 集合

本文主要是介绍java 集合,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.集合框架

2.例子

 

 

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());
        }


    }

}

 

这篇关于java 集合的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!