集合总结

List 特点:有序(存取一致),可重复的

实现类:

1.ArrayList:数组实现,查找快,增删比较慢,线程不安全的(Java1.5以后出现)
2.LinkedList:链表实现,增删快,查找比较慢
3.Vector:数组实现的,线程安全的,效率比较低(1.5以前的)

Set 特点:无序(存取不一致),不可重复的

实现类:

1.HashSet:Hash表实现的
2.LinkedHashSet:唯一一个可以使Set集合有序的实现类
3.TreeSet:用于集合中数据的排序

Map 特点:存的是键值对,键不能重复,值可以重复

实现类:
1.HashMap:Hash表实现的(1.5以后的),线程不安全的,允许key或者value为null
2.LinkedHashMap:
3.TreeMap:
4.Hashtable:线程安全的(1.5以前的)不允许null的key或value

collection和collections
collection是接口
collection是类(针对上述接口的操作方法)

ListDemo.java

package list;

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


public class ListDemo {
    /*
     * 使用迭代器遍历list集合,如果集合中有
     * 元素a,那么添加一个元素aa
     */
    public static void main(String[] args) {
        //1.使用for循环遍历集合
//      demo1();
        //2.使用foreach语句遍历集合
        /**
         * foreach并不是一个关键字,习惯上将这种特殊的for语句格式称之为“foreach”语句。
         * 从英文字面意思理解foreach也就是“for 每一个”的意思。实际上也就是这个意思。
         */
//      demo2();
        //3.使用迭代器遍历集合
//      demo3();
        //ListIterator有add方法,可以向List中添加对象,而Iterator不能
//      demo4();
    }

    
    /**
     * 1.使用for循环遍历集合
     */
    private static void demo1() {
        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i));
        }
    }

    /**
     * 2.使用foreach语句遍历集合
     */
    private static void demo2() {
        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        
        for (String str : list) {
            System.out.println(str);
        }
    }

    /**
     * 3.使用迭代器遍历集合
     */
    private static void demo3() {
        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        
        Iterator<String> it = list.iterator();
        while(it.hasNext()) {
            String str = it.next();
            System.out.println(str);
        }
    }
    
    private static void demo4() {
        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        /*
         * ListIterator有add方法,可以向List中添加对象,而Iterator不能。
         */
        ListIterator<String> it1 = list.listIterator();
        System.out.println(list);
        while(it1.hasNext()) {
            String str = it1.next();
            System.out.println("str:"+str);
            if("a".equals(str)) {
                it1.add("aa");
            }
        }
        System.out.println(list);
    }
}


Person.java

package list;

public class Person implements Comparable<Person>{
    private String name;
    private int age;
    public Person() {
        super();
    }
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Person other = (Person) obj;
        if (age != other.age)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person [name="+name +",age=" + age +"]";
    }
    
    /*
     * 该方法的返回值决定了集合将如何存储元素
     * 返回0时,只存一个元素
     * 返回正数时,集合正序存储
     * 返回负数时,集合倒序存储
     */
    public int compareTo(Person o) {
        return -1;
    }
}


SetMap.java

package list;

import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class MapDemo {
    public static void main(String[] args) {
        /*
         * map的遍历方式
         * 1.遍历所有的key
         * 2.遍历所有的value
         * 2.遍历所有的键值对
         */
        //遍历所有的key      keySet()
//      demo1();
        //遍历所有的value    values()
//      demo2();
        //遍历所有键值对   entrySet()
//      Map<String,Integer> map = new HashMap<>();
        Map<String,Integer> map = new Hashtable<>();
        /*
         * HashMap允许key或者value为null
         * Hashtable不允许null的key或者value
         */
        map.put("tom", 13);
        map.put("jerry", 18);
        map.put("Jhon", 23);
        Set<Entry<String,Integer>> set = map.entrySet();
        for(Entry<String,Integer> entry : set) {
            System.out.println(entry.getKey()+","+entry.getValue());
        }
        
    }

    private static void demo2() {
        Map<String,Integer> map = new HashMap<>();
        map.put("tom", 10);
        map.put("jerry", 18);
        map.put("Jhon", 23);
        int a = map.get("tom");
        System.out.println(a);
        Collection<Integer> val = map.values();
        System.out.println(val);
    }

    private static void demo1() {
        Map<String,Integer> map = new HashMap<String,Integer>();
        map.put("tom", 10);
        map.put("jerry", 18);
        map.put("Jhon", 23);
        Set<String> set = map.keySet();
        System.out.println(set);
    }
}

MapDemo.java

package list;

import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class MapDemo {
    public static void main(String[] args) {
        /*
         * map的遍历方式
         * 1.遍历所有的key
         * 2.遍历所有的value
         * 2.遍历所有的键值对
         */
        //遍历所有的key      keySet()
//      demo1();
        //遍历所有的value    values()
//      demo2();
        //遍历所有键值对   entrySet()
//      Map<String,Integer> map = new HashMap<>();
        Map<String,Integer> map = new Hashtable<>();
        /*
         * HashMap允许key或者value为null
         * Hashtable不允许null的key或者value
         */
        map.put("tom", 13);
        map.put("jerry", 18);
        map.put("Jhon", 23);
        Set<Entry<String,Integer>> set = map.entrySet();
        for(Entry<String,Integer> entry : set) {
            System.out.println(entry.getKey()+","+entry.getValue());
        }
        
    }

    private static void demo2() {
        Map<String,Integer> map = new HashMap<>();
        map.put("tom", 10);
        map.put("jerry", 18);
        map.put("Jhon", 23);
        int a = map.get("tom");
        System.out.println(a);
        Collection<Integer> val = map.values();
        System.out.println(val);
    }

    private static void demo1() {
        Map<String,Integer> map = new HashMap<String,Integer>();
        map.put("tom", 10);
        map.put("jerry", 18);
        map.put("Jhon", 23);
        Set<String> set = map.keySet();
        System.out.println(set);
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,906评论 18 399
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,442评论 11 349
  • 心微伤,又寒窗,一柄新烛对空房 院里的虞美人残了花,却依旧是一副顶天立地的模样,哪怕已是没了生机,像你一样。 倘若...
    神奇小逗阅读 3,544评论 2 0
  • 一直以来,我们似乎都不太擅长表达对父母的爱,不论是一句直白的“我爱你”,还是日常生活琐碎细节的表达,可能是找不到一...
    一只孤巷的猫阅读 2,865评论 0 1
  • 喜欢看梁朝伟版的《倚天屠龙记》,心里无比疼惜那个楚楚可怜、叫做周芷若的女孩儿。她本质出生贫寒、美丽绝伦、善良美好,...
    迷你欧阅读 4,808评论 0 2