首页 > 编程语言 > 详细

java集合

时间:2021-04-29 09:58:28      阅读:23      评论:0      收藏:0      [点我收藏+]

集合需要掌握的主要内容:

1.怎么创建

2.怎么添加元素

3.怎么取出元素

4.遍历集合

主要的集合类

ArrayList

LinkedList

HashSet

TreeSet

HashMap

Properties

TreeMap

ArrayList 和 LinkedList 其实差不多,一样的方法,HashSet也是的,但是HashSet无序不重复

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        // 创建
        List<String> myList = new ArrayList<>(20);
        // 添加元素
        myList.add("a");
        myList.add("b");
        myList.add("c");
		// for循环遍历
        for (String res : myList){
            System.out.println(res);
        }
        
        String[] strList = new String[myList.size()];
        myList.toArray(strList);
        for (int i = 0; i < strList.length; i++) {
            System.out.println(strList[i]);
        }
		// for循环根据下标遍历
        for (int i = 0; i < myList.size(); i++) {
            myList.get(i);
        }
		// 迭代器遍历,这个方法所有collection都可以用
        Iterator<String> ite = myList.iterator();
        while (ite.hasNext()){
            System.out.println(ite.next());
        }
    }
}

TreeSet

import org.jetbrains.annotations.NotNull;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Objects;
import java.util.TreeSet;

public class TreeSetTest {
    public static void main(String[] args) {
        // 创建
        TreeSet<Integer> a = new TreeSet<>();
        //添加元素
        a.add(1);
        a.add(12);
        a.add(2);
        a.add(45);
        a.add(1000);
		//选择器
        TreeSet<C> c = new TreeSet<>(new Comparator<C>() {
            @Override
            public int compare(C o1, C o2) {
                return o2.c - o1.c;
            }
        });
        // 实例化对象
        C a1 = new C(1);
        C a2 = new C(1324);
        C a3 = new C(1423);
        C a4 = new C(1877);
        C a5 = new C(108);
		// 添加实例化的对象
        c.add(a1);
        c.add(a2);
        c.add(a3);
        c.add(a4);
        c.add(a5);
		// 遍历
        for (C i : c) {
            System.out.println(i);
        }
    }
}

class A implements Comparable<A>{
    int a;

    public A(int a){
        this.a = a;
    }

    public A(){

    }

    @Override
    public String toString() {
        return "A{" +
                "a=" + a +
                ‘}‘;
    }


    @Override
    public int compareTo(@NotNull A o) {
        return this.a - o.a;
    }
}

class C{
    int c;

    public C(int c){
        this.c = c;
    }

    @Override
    public String toString() {
        return "C{" +
                "c=" + c +
                ‘}‘;
    }
}

结果

技术分享图片

HashMap

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HasMapTest {
    public static void main(String[] args) {
    	// 创建
        HashMap<Integer, String> aMap = new HashMap<>();
        // 添加元素
        aMap.put(1, "张三");
        aMap.put(2, "李四");
        aMap.put(3, "王五");
        aMap.put(4, "刘六");
		// 遍历	
        Set<Integer> keys = aMap.keySet();
        for (int i : keys){
            System.out.println(i+"="+aMap.get(i));
        }
		// 遍历
        Set<Map.Entry<Integer, String>> nodes = aMap.entrySet();
        for (Map.Entry<Integer, String> node : nodes){
            System.out.println(node.getKey()+"="+node.getValue());
        }
    }
}

结果

技术分享图片

Properties

package oop.Demo06;

import javafx.animation.ParallelTransition;

import java.util.Properties;

public class PropertiesTest {
    public static void main(String[] args) {
        //创建
        Properties pro = new Properties();
        //添加元素
        pro.setProperty("username","ltan");
        pro.setProperty("password", "fjewj");
		//遍历
        String name = pro.getProperty("username");
        String word = pro.getProperty("password");
		//打印
        System.out.println(name);
        System.out.println(word);
    }
}

结果

技术分享图片

java集合

原文:https://www.cnblogs.com/ltan/p/14716345.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!