JAVA集合实例

1.ArrayList&LinkedList

package com.allen.study;

import java.util.Iterator;
import java.util.LinkedList;

/**
 * 重点掌握:
 *  1.每个集合对象的创建
 *  2.向集合中添加元素
 *  3.从集合中取出某个元素
 *  4.遍历集合
 * @Author: allen
 * @Date: 2020/11/30 - 11 - 30 - 23:13
 * @Description: com.allen.study
 * @version: 1.0
 */
public class ArrayListTest {
    public static void main(String[] args) {
        //创建集合ArrayList对象
        //ArrayList<String> list = new ArrayList();
        LinkedList<String> list = new LinkedList();
        //向集合中添加元素
        list.add("zhangsan");
        list.add("lisi");
        list.add("wangwu");
        list.add("zhaoliu");
        //用下标取出元素
        String firstEle = list.get(0);
        System.out.println(firstEle);
        //遍历集合
        for(String str : list){
            System.out.println(str);
        }

        for(int i = 0; i < list.size(); i++){
            System.out.println(list.get(i));
        }

        for(Iterator<String> it = list.iterator(); it.hasNext(); ){
            System.out.println(it.next());
        }

        Iterator<String> it1 = list.iterator();
        while(it1.hasNext()){
            System.out.println(it1.next());
        }
    }
}

2.HashSet

package com.allen.study;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;

/**
 * 重点掌握:
 *   1.每个集合对象的创建
 *   2.向集合中添加元素
 *   3.从集合中取出某个元素
 *   4.遍历集合
 *   5.测试Set集合的特点:无序不可重复
 * @Author: allen
 * @Date: 2020/12/1 - 12 - 01 - 0:01
 * @Description: com.allen.study
 * @version: 1.0
 */
public class HashSetTest {
    public static void main(String[] args) {
        //创建对象
        Set<String> set = new HashSet<>();
        //添加元素
        set.add("zhangsan");
        set.add("lisi");
        set.add("wangwu");
        set.add("zhaoliu");
        System.out.println(set.size());

        //遍历Set set集合没有下标
        Iterator<String> it = set.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }

        set.add("lisi");
        System.out.println(set.size());

        for(String str : set){
            System.out.println(str);
        }

        //创建Set集合,存储Student数据
        HashSet<Student> stuSet = new HashSet<>();
        Student s1 = new Student(1,"zhangsan");
        Student s2 = new Student(3,"wangwu");
        Student s3 = new Student(5,"zhaoliu");
        Student s4 = new Student(2,"wangba");
        Student s5 = new Student(4,"liufang");
        Student s6 = new Student(4,"liufang");
        stuSet.add(s1);
        stuSet.add(s2);
        stuSet.add(s3);
        stuSet.add(s4);
        stuSet.add(s5);
        stuSet.add(s6);

        int size = stuSet.size();
        System.out.println(size);
        for(Student stu : stuSet){
            System.out.println(stu);
        }

    }
}

class Student {
    int no;
    String name;

    public Student() {

    }

    public Student(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return no == student.no &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(no, name);
    }
}

3.TreeSet&TreeMap

package com.allen.study;

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

/**
 * 重点掌握:
 *  1.每个集合对象的创建
 *  2.向集合中添加元素
 *  3.从集合中取出某个元素
 *  4.遍历集合
 *  5.TreeSet集合特点:无序不可重复,可排序
 *  6.测试TreeSet集合中存储的类型是自定义的
 * @Author: allen
 * @Date: 2020/12/1 - 12 - 01 - 0:25
 * @Description: com.allen.study
 * @version: 1.0
 */
public class TreeSetTest {
    public static void main(String[] args) {
        //TreeSet<Integer> ts = new TreeSet();
        /*编写比较器可以改变排序规则*/
        TreeSet<Integer> ts = new TreeSet<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;//自动拆箱
            }
        });
        //添加元素
        ts.add(12);
        ts.add(100);
        ts.add(43);
        ts.add(200);
        //遍历
        for(Integer i : ts){
            System.out.println(i);
        }

        ts.add(43);
        ts.add(43);
        System.out.println(ts.size());

        Iterator<Integer> it = ts.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }


        //TreeSet集合中存储自定义集合类型
        TreeSet<A> aTree = new TreeSet<>();
        aTree.add(new A(10));
        aTree.add(new A(40));
        aTree.add(new A(60));
        aTree.add(new A(20));
        System.out.println(aTree.size());

        for(A a : aTree){
            System.out.println(a);
        }

        //TreeSet集合中存储自定义集合类型
        //TreeSet<B> bTree = new TreeSet<>(new BComparator());
        //匿名内部类
        TreeSet<B> bTree = new TreeSet<>(new Comparator<B>() {
            @Override
            public int compare(B o1, B o2) {
                return o1.i - o2.i;
            }
        });
        bTree.add(new B(10));
        bTree.add(new B(40));
        bTree.add(new B(60));
        bTree.add(new B(20));
        System.out.println(bTree.size());

        for(B b : bTree){
            System.out.println(b);
        }

    }
}

//实现Comparable接口
class A implements Comparable<A>{
    int i;
    public A(){

    }

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

    @Override
    public String toString() {
        return "A{" +
                "i=" + i +
                '}';
    }

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

//
class B{
    int i;
    public B(int i){
        this.i = i;
    }

    @Override
    public String toString() {
        return "B{" +
                "i=" + i +
                '}';
    }
}
//比较器
/*
class BComparator implements Comparator<B>{
    @Override
    public int compare(B o1, B o2) {
        return o1.i - o2.i;
    }
}*/

4.HashMap

package com.allen.study;

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

/**
 * 重点掌握:
 *  1.每个集合对象的创建
 *  2.向集合中添加元素
 *  3.从集合中取出某个元素
 *  4.遍历集合
 * @Author: allen
 * @Date: 2020/12/1 - 12 - 01 - 0:52
 * @Description: com.allen.study
 * @version: 1.0
 */
public class HashMapTest {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap();
        map.put(1,"lisi");
        map.put(3,"zhaoliu");
        map.put(2,"wangwu");
        map.put(5,"zhaosi");
        map.put(2,"yaoming");//key重复,value覆盖
        System.out.println(map.get(2));//yaoming
        System.out.println(map.size());//4
        //遍历方法1
        /*先获取所有的key,遍历key的时候通过key获取value*/
        Set<Integer> keys = map.keySet();
        for(Integer key : keys){
            System.out.println(key + "=" + map.get(key));
        }

        //遍历方法2:
        /*将Map集合转换成Set集合,Set集合中每一个元素是Node,Node节点中有key和value*/
        Set<Map.Entry<Integer,String>> nodes = map.entrySet();
        for(Map.Entry<Integer,String> node : nodes){
            System.out.println(node.getKey() + "=" + node.getValue());
        }
    }
}

5.Properies

package com.allen.study;

import java.util.Properties;

/**
 * @Author: allen
 * @Date: 2020/12/1 - 12 - 01 - 1:09
 * @Description: com.allen.study
 * @version: 1.0
 */
public class PropertiesTest {
    public static void main(String[] args) {
        Properties pro = new Properties();
        //添加数据
        pro.setProperty("username", "zhangsan");
        pro.setProperty("password", "123456");
        //获取数据
        String username = pro.getProperty("username");
        String password = pro.getProperty("password");
        System.out.println(username);
        System.out.println(password);
    }
}

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页