One 许增宝

集合框架
Collection有序允许重复:ListArrayListLinkedListQueue. LinkedList...
扫描右侧二维码阅读全文
30
2019/04

集合框架

Collection

有序允许重复:

  1. List

    1. ArrayList
    2. LinkedList
  2. Queue
    . LinkedList

无序不可重复

. Set
. HashSet

List

  1. List 是元素有序并且可以重复的集合,称为序列;
  2. List 可以精确的控制每个元素的插入位置,或删除某个位置的元素;
  3. List 的两个主要实现类是 ArrayList 和 LinkedList;

ArrayList

. ArrayList 底层是由数组实现的
. 动态增长
. 再列表尾部插入或删除数据非常有效
. 更适合查找和更新元素
. ArrayList 中的元素可以为 null

代码示例:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestEquals {
    public static void main(String[] args){
        List list = new ArrayList();
        list.add("Java");
        list.add("C");
        list.add("C++");
        list.add("Go");
        list.add("Swift");
        // 输出列表中元素的个数
        System.out.println("列表中的元素个数为:" + list.size());
        // 遍历输出所有编程语言
        // 或者在for循环中用 get() 方法遍历也行
        list.forEach(obj->System.out.println(obj + " "));
        
        // 移除 C++
        System.out.println("***********************************");
        list.remove(2); // 或者 list.remove("C++")
        list.forEach(obj->System.out.println(obj + " "));
    }
}

公告代码:

/***************** Notice *******************/
package cn.onexzb.www;

import java.util.Date;

public class Notice {
    private int id;
    private String content;
    private String author;
    private Date creatTime;
    public Notice(int id, String content, String author, Date creatTime) {
        super();
        this.id = id;
        this.content = content;
        this.author = author;
        this.creatTime = creatTime;
    }
    public Notice(int id, String content, String author) {
        super();
        this.id = id;
        this.content = content;
        this.author = author;
    }
    public int getId() {
        return id;
    }
    public Date getCreatTime() {
        return creatTime;
    }
    public void setCreatTime(Date creatTime) {
        this.creatTime = creatTime;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    
}



/***************** Main *******************/
package cn.onexzb.www;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Main {
    public static void main(String[] args){
        Notice notice1 = new Notice(1, "第一条公告!", "管理员", new Date());
        Notice notice2 = new Notice(2, "第二条公告!", "老师", new Date());
        Notice notice3 = new Notice(3, "第三条公告!", "学生", new Date());
        
        List list = new ArrayList();
        list.add(notice1);
        list.add(notice2);
        list.add(notice3);
        
        // 遍历打印
        for(int i = 0; i < list.size(); i++){
            System.out.println(
            ((Notice)(list.get(i))).getId() + " " 
            + ((Notice)(list.get(i))).getContent() + " " 
            + ((Notice)(list.get(i))).getCreatTime());
        }
        System.out.println("***************** 分割线 ***********************");
        // 删除第2条公告
        list.remove(1);
        for(int i = 0; i < list.size(); i++){
            System.out.println(
            ((Notice)(list.get(i))).getId() + " " 
            + ((Notice)(list.get(i))).getContent() + " " 
            + ((Notice)(list.get(i))).getCreatTime());
        }
        // 在指定位置添加公告
        System.out.println("***************** 分割线 ***********************");
        Notice notice4 = new Notice(0, "在指定位置添加公告", "许增宝", new Date());
        list.add(0, notice4);
        for(int i = 0; i < list.size(); i++){
            System.out.println(
            ((Notice)(list.get(i))).getId() + " " 
            + ((Notice)(list.get(i))).getContent() + " " 
            + ((Notice)(list.get(i))).getCreatTime());
        }
    }
}

关于 Set

代码:

/***************** Cat *******************/
package cn.onexzb.www;

public class Cat {
    private String name;
    private String age;
    private String species;
    
    public Cat(String name, String age, String species) {
        super();
        this.name = name;
        this.age = age;
        this.species = species;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSpecies() {
        return species;
    }

    public void setSpecies(String species) {
        this.species = species;
    }

    @Override
    public String toString() {
        return "[姓名:" + name + ", 年龄:" + age + ", 品种:" + species + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((age == null) ? 0 : age.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((species == null) ? 0 : species.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        // 判断 obj 是否是 Cat 类的对象
        if (obj.getClass() == Cat.class){
            Cat cat = (Cat)obj;
            return 
                    cat.getName().equals(name) && 
                    cat.getAge().equals(age) &&
                    cat.getSpecies().equals(species); 
        }
            
        return true;
    }
    
    
}






/***************** CatTest *******************/
package cn.onexzb.www;

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

public class CatTest {
    public static void main(String[] args) {
        Cat huahua = new Cat("花花", "六个月", "黄猫");
        Cat heihei = new Cat("黑黑", "十二个月", "黑猫");
        
        Set<Cat> set = new HashSet<Cat>();
        
        set.add(huahua);
        set.add(heihei);
        
        Iterator<Cat> it = set.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
        
        System.out.println("*****************增加重复猫*****************");
        // 插入一个与之前相同的猫
        // 这里必须重写 equals,否则不行
        Cat heihei1 = new Cat("黑黑", "十二个月", "黑猫");
        set.add(heihei1);
        
        it = set.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
        
        // 删除猫 heihie
        for(Cat temp : set){
            if(temp == heihei)
                set.remove(heihei);
        }
        System.out.println("******************删除猫****************");
        it = set.iterator();
        while(it.hasNext()) {
            System.out.println(it.next());
        }
        
        // 查找 猫 huahua
        // 通过对象查找
        System.out.println("******************查找猫****************");
        System.out.println("******************通过对象查找****************");
        if(set.contains(huahua)){
            System.out.println("huahua找到了!");
            System.out.println(huahua);
        }
        else
            System.out.println("huahua没找到!");
        System.out.println("******************通过对象中元素查找****************");
        it = set.iterator();
        while(it.hasNext()){
            if(it.next().getName() == "花花"){
                System.out.println("花花找到了!");
                System.out.println(huahua);
            }
            else
                System.out.println("花花没找到!");
        }
    }
}

关于 Map

代码:

package cn.onexzb.www;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;

public class MyMap {
    public static void main(String[] args){
        Map<String, String> animal = new HashMap<String, String>();
        Scanner console = new Scanner(System.in);
        // 添加数据
        for(int i = 0; i < 3; i++){
            System.out.println("输入 Key 的值:\n");
            String key = console.next();
            System.out.println("输入 Value 的值:\n");
            String val = console.next();
            animal.put(key, val);
        }
        
        // 打印数据
        Iterator<String> it = animal.values().iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
}
最后修改:2019 年 05 月 01 日 02 : 15 AM

发表评论