java.core.ch01_collection

java-collection

1、Iterator

1.1 代码

package com.nash.java.core.ch01_collection;

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

/**
 * 1、迭代器 Iterator
 * 迭代器是用来遍历集合元素的,通过迭代器,可以将遍历与数据分离
 * while(it.hasNext()){
 *          System.out.println(it.next());
 * }
 *
 * 2、Foreach
 * 实现了Iterable接口,即可用Foreach遍历
 * for(String s:set){
 *          System.out.println(s);
 * }
 */
public class IteratorApp {

    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");

        // 1、iterator() 遍历,实现了iterator接口
        System.out.print("Iterator 遍历:\t");
        Iterator it = list.iterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }
        System.out.println();

        // 2、foreach 遍历
        System.out.print("Foreach 遍历:\t");
        for (String li : list){
            System.out.print(li + " ");
        }
    }
}

1.2 结果

Iterator 遍历:    a b c d 
Foreach 遍历: a b c d 

2、ArrayList

2.1 代码

package com.nash.java.core.ch01_collection;

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

/**
 * 数组列表 ArrayList
 * 有序、可重复集合。
 *
 * 容量可以动态增加,底层实现是数组
 * 1、有序
 * 2、元素个数不限
 * 3、元素类型相同、定型
 * 4、可变值
 * 5、可重复
 * 6、查找快、更新慢
 */
public class ArrayListApp {

    public static void main(String[] args) {
        // 初始化、集体赋值
        List list = Arrays.asList("hadoop","hbase","hive");
        ArrayList arrayList = new ArrayList<>(list);
        System.out.println("arrayList:\t" + arrayList.toString());
        System.out.println("equals():\t" + list.equals(arrayList));
        // size() 列表长度
        System.out.println("size():\t" + arrayList.size());
        // isEmpty() 是否为空
        System.out.println("isEmpty():\t" + arrayList.isEmpty());
        // contains() 是否包含
        System.out.println("contains(\"Flink\"):\t" + arrayList.contains("hadoop"));
        // 初始化、逐个赋值
        ArrayList courseList=new ArrayList<>();
        System.out.println("courseList:\t" + courseList);
        // add() 插入
        courseList.add("Java");
        System.out.println("add(\"Java\"):\t" + courseList);
        courseList.add("Python");
        System.out.println("add(\"Python\"):\t" + courseList);
        courseList.add("Scala");
        System.out.println("add(\"Scala\"):\t" + courseList);
        courseList.add(0,"First");
        System.out.println("add(0,\"First\"):\t" + courseList);
        // set() 更新
        courseList.set(0,"Go");
        System.out.println("set(0,\"Go\"):\t" + courseList);
        // get() 查询
        System.out.println("get(3):\t" + courseList.get(2));
        // remove() 删除
        courseList.remove(0);
        System.out.println("remove(0):\t" + courseList);
        courseList.remove("Scala");
        System.out.println("remove(\"Scala\"):\t" + courseList);
        // fori 遍历
        System.out.print("fori():\t");
        for (int i = 0; i < courseList.size(); i++) {
            System.out.print(courseList.get(i) + " ");
        }
        System.out.println();
        // foreach 遍历,实现了iterable接口
        System.out.print("foreach():\t");
        for (String course:courseList){
            System.out.print(course + " ");
        }
        System.out.println();
        // iterator() 遍历,实现了iterator接口
        System.out.print("iterator():\t");
        Iterator courseIterator = courseList.iterator();
        while (courseIterator.hasNext()){
            System.out.print(courseIterator.next() + " ");
        }
        System.out.println();
        // subList(1,2) 子表
        System.out.println("subList(1,2):\t" + courseList.subList(1,2));
    }
}

2.2 结果

arrayList:  [hadoop, hbase, hive]
equals():   true
size(): 3
isEmpty():  false
contains("Flink"):  true
courseList: []
add("Java"):    [Java]
add("Python"):  [Java, Python]
add("Scala"):   [Java, Python, Scala]
add(0,"First"): [First, Java, Python, Scala]
set(0,"Go"):    [Go, Java, Python, Scala]
get(3): Python
remove(0):  [Java, Python, Scala]
remove("Scala"):    [Java, Python]
fori(): Java Python 
foreach():  Java Python 
iterator(): Java Python 
subList(1,2):   [Python]

3、LinkedList

3.1 代码

package com.nash.java.core.ch01_collection;

import java.util.*;

/**
 * 链表 LinkedList
 * 有序、可重复集合。
 *
 * 底层实现是指针、内部类
 * 1、有序
 * 2、元素个数不限
 * 3、元素类型相同、定型
 * 4、可变值
 * 5、可重复
 * 6、查找慢、插入/删除/更新快
 */
public class LinkedListApp {

    public static void main(String[] args) {
        // 初始化、集体赋值
        List list = Arrays.asList("Java","Python","Scala");
        LinkedList linkedList = new LinkedList<>(list);
        System.out.println("linkedList:\t" + linkedList.toString());
        System.out.println("equals():\t" + list.equals(linkedList));
        // size() 列表长度
        System.out.println("size():\t" + linkedList.size());
        // isEmpty() 是否为空
        System.out.println("isEmpty():\t" + linkedList.isEmpty());
        // contains() 是否包含
        System.out.println("contains(\"Java\"):\t" + linkedList.contains("Java"));
        // 初始化、逐个赋值
        LinkedList courseList=new LinkedList<>();
        System.out.println("courseList:\t" + courseList);
        // add() 插入
        courseList.add("Hadoop");
        System.out.println("add(\"Hadoop\"):\t" + courseList);
        courseList.add("HBase");
        System.out.println("add(\"HBase\"):\t" + courseList);
        courseList.add("Hive");
        System.out.println("add(\"Hive\"):\t" + courseList);
        // add(0,"First")
        courseList.add(0,"First");
        System.out.println("add(0,\"First\"):\t" + courseList);
        // addFirst("Spark")
        courseList.addFirst("Spark");
        System.out.println("addFirst(\"Spark\"):\t" + courseList);
        // addLast("Flink")
        courseList.addLast("Flink");
        System.out.println("addLast(\"Flink\"):\t" + courseList);
        // set() 更新
        courseList.set(0,"Hama");
        System.out.println("set(0,\"Hama\"):\t" + courseList);
        // get() 查询
        System.out.println("get(3):\t" + courseList.get(3));
        // remove() 删除
        courseList.remove(0);
        System.out.println("remove(0):\t" + courseList);
        courseList.remove("First");
        System.out.println("remove(\"First\"):\t" + courseList);
        /**
         * poll()
         * 1、数据结构:队列(Queue)
         * 2、方法:出队(Dequeue)
         * 3、方法说明:查看队首元素,并删除该元素
         * 4、类比:remove() 删除
         */
        System.out.println("poll():\t" + courseList.poll());
        System.out.println("courseList:\t" + courseList);
        /**
         * offer()
         * 1、数据结构:队列(Queue)
         * 2、方法:入队(Deque)
         * 3、方法说明:在队尾插入元素
         * 4、类比:add() 插入
         */
        System.out.println("offer():\t" + courseList.offer("Hama"));
        System.out.println("courseList:\t" + courseList);
        /**
         * peek()
         * 1、数据结构:队列(Queue)
         * 2、方法:查队
         * 3、方法说明:查看队首元素,不删除
         * 4、类比:getFirst()
         */
        System.out.println("peek():\t" + courseList.peek());
        System.out.println("courseList:\t" + courseList);
        // element()、getFirst() 查看首元素
        System.out.println("element():\t" + courseList.element());

        /**
         * pop()
         * 1、数据结构:堆栈(Stack)
         * 2、方法:出栈
         * 3、方法说明:移除堆栈顶部元素
         * 4、类比:remove() 删除
         */
        System.out.println("pop():\t" + courseList.pop());
        System.out.println("courseList:\t" + courseList);
        /**
         * push()
         * 1、数据结构:堆栈(Stack)
         * 2、方法:入栈(Dequeue)
         * 3、方法说明:把元素压入堆栈顶部
         * 4、类比:addFirst() 在顶部插入元素
         */
        courseList.push("Samza");
        System.out.println("push():\t" + courseList);

        // fori 遍历
        System.out.print("fori():\t");
        for (int i = 0; i < courseList.size(); i++) {
            System.out.print(courseList.get(i) + " ");
        }
        System.out.println();
        // foreach 遍历,实现了iterable接口
        System.out.print("foreach():\t");
        for (String course:courseList){
            System.out.print(course + " ");
        }
        System.out.println();
        // iterator() 遍历,实现了iterator接口
        System.out.print("iterator():\t");
        Iterator courseIterator = courseList.iterator();
        while (courseIterator.hasNext()){
            System.out.print(courseIterator.next() + " ");
        }
        System.out.println();
        // subList(1,2) 子表
        System.out.println("subList(1,2):\t" + courseList.subList(1,2));

    }
}

3.2 结果

linkedList: [Java, Python, Scala]
equals():   true
size(): 3
isEmpty():  false
contains("Java"):   true
courseList: []
add("Hadoop"):  [Hadoop]
add("HBase"):   [Hadoop, HBase]
add("Hive"):    [Hadoop, HBase, Hive]
add(0,"First"): [First, Hadoop, HBase, Hive]
addFirst("Spark"):  [Spark, First, Hadoop, HBase, Hive]
addLast("Flink"):   [Spark, First, Hadoop, HBase, Hive, Flink]
set(0,"Hama"):  [Hama, First, Hadoop, HBase, Hive, Flink]
get(3): HBase
remove(0):  [First, Hadoop, HBase, Hive, Flink]
remove("First"):    [Hadoop, HBase, Hive, Flink]
poll(): Hadoop
courseList: [HBase, Hive, Flink]
offer():    true
courseList: [HBase, Hive, Flink, Hama]
peek(): HBase
courseList: [HBase, Hive, Flink, Hama]
element():  HBase
pop():  HBase
courseList: [Hive, Flink, Hama]
push(): [Samza, Hive, Flink, Hama]
fori(): Samza Hive Flink Hama 
foreach():  Samza Hive Flink Hama 
iterator(): Samza Hive Flink Hama 
subList(1,2):   [Hive]

4、HashSet

4.1 代码

package com.nash.java.core.ch01_collection;

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

/**
 * 集合实现类 HashSet
 * 无序、不可重复集合。
 *
 * 通过散列机制将信息存储到哈希表
 * 当程序向HashSet集合添加元素时,HashSet会根据该元素的HashCode值,计算它在内存的存储位置,这样可以快速定位该元素。
 * 1、无序
 * 2、元素个数不限
 * 3、元素类型相同、定型
 * 4、可变值
 * 5、不可重复
 * 6、查找慢、插入/删除/更新快
 */
public class HashSetApp {

    public static void main(String[] args) {
        // 初始化、逐个赋值
        HashSet courseSet=new HashSet<>();
        System.out.println("courseSet:\t" + courseSet);
        // size() 列表长度
        System.out.println("size():\t" + courseSet.size());
        // isEmpty() 是否为空
        System.out.println("isEmpty():\t" + courseSet.isEmpty());
        // contains() 是否包含
        System.out.println("contains(\"Java\"):\t" + courseSet.contains("Java"));
        // add() 插入
        courseSet.add("Java");
        System.out.println("add(\"Java\"):\t" + courseSet);
        courseSet.add("Python");
        System.out.println("add(\"Python\"):\t" + courseSet);
        courseSet.add("Scala");
        System.out.println("add(\"Scala\"):\t" + courseSet);
        courseSet.add("Scala");
        System.out.println("add(\"Scala\"):\t" + courseSet);
        // remove() 删除
        courseSet.remove("Scala");
        System.out.println("remove(\"Scala\"):\t" + courseSet);
        // foreach 遍历,实现了iterable接口
        System.out.print("foreach():\t");
        for (String course:courseSet){
            System.out.print(course + " ");
        }
        System.out.println();
        // iterator() 遍历,实现了iterator接口
        System.out.print("iterator():\t");
        Iterator courseIterator = courseSet.iterator();
        while (courseIterator.hasNext()){
            System.out.print(courseIterator.next() + " ");
        }
        System.out.println();

    }
}

4.2 结果

courseSet:  []
size(): 0
isEmpty():  true
contains("Java"):   false
add("Java"):    [Java]
add("Python"):  [Java, Python]
add("Scala"):   [Java, Scala, Python]
add("Scala"):   [Java, Scala, Python]
remove("Scala"):    [Java, Python]
foreach():  Java Python 
iterator(): Java Python 

5、TreeSet

5.1 代码

package com.nash.java.core.ch01_collection;

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

/**
 * 集合实现类 TreeSet
 * 有序、不可重复集合。
 *
 * 采用红黑树的数据结构存储集合元素。
 *
 * 当程序向TreeSet集合添加元素时,TreeSet会根据该元素的HashCode值,计算它在内存的存储位置,这样可以快速定位该元素。
 * 1、有序
 * 2、元素个数不限
 * 3、元素类型相同、定型
 * 4、可变值
 * 5、不可重复
 * 6、查找慢、插入/删除/更新快
 */
public class TreeSetApp {
    public static void main(String[] args) {
        // 初始化、逐个赋值
        TreeSet courseSet=new TreeSet<>();
        System.out.println("courseSet:\t" + courseSet);
        // size() 列表长度
        System.out.println("size():\t" + courseSet.size());
        // isEmpty() 是否为空
        System.out.println("isEmpty():\t" + courseSet.isEmpty());
        // contains() 是否包含
        System.out.println("contains(\"Java\"):\t" + courseSet.contains("Java"));
        // add() 插入
        courseSet.add("Java");
        System.out.println("add(\"Java\"):\t" + courseSet);
        courseSet.add("Python");
        System.out.println("add(\"Python\"):\t" + courseSet);
        courseSet.add("Scala");
        System.out.println("add(\"Scala\"):\t" + courseSet);
        courseSet.add("Go");
        System.out.println("add(\"Go\"):\t" + courseSet);
        courseSet.add("PHP");
        System.out.println("add(\"PHP\"):\t" + courseSet);
        // remove() 删除
        courseSet.remove("Scala");
        System.out.println("remove(\"Scala\"):\t" + courseSet);
        // descendingSet() 倒序
        System.out.println("descendingSet():\t" + courseSet.descendingSet());
        System.out.println("courseSet:\t" + courseSet);
        // first() 查看首个元素
        System.out.println("first():\t" + courseSet.first());
        // last() 查看末尾元素
        System.out.println("last():\t" + courseSet.last());
        // foreach 遍历,实现了iterable接口
        System.out.print("foreach():\t");
        for (String course:courseSet){
            System.out.print(course + " ");
        }
        System.out.println();
        // iterator() 遍历,实现了iterator接口
        System.out.print("iterator():\t");
        Iterator courseIterator = courseSet.iterator();
        while (courseIterator.hasNext()){
            System.out.print(courseIterator.next() + " ");
        }
        System.out.println();
    }
}

5.2 结果

courseSet:  []
size(): 0
isEmpty():  true
contains("Java"):   false
add("Java"):    [Java]
add("Python"):  [Java, Python]
add("Scala"):   [Java, Python, Scala]
add("Go"):  [Go, Java, Python, Scala]
add("PHP"): [Go, Java, PHP, Python, Scala]
remove("Scala"):    [Go, Java, PHP, Python]
descendingSet():    [Python, PHP, Java, Go]
courseSet:  [Go, Java, PHP, Python]
first():    Go
last(): Python
foreach():  Go Java PHP Python 
iterator(): Go Java PHP Python 

6、HashMap

6.1 代码

package com.nash.java.core.ch01_collection;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 * 映射、关联数组 HashMap
 * 无序、key不可重复、具有映射关系的集合。
 *
 * 1、Map中的key不可重复。
 */
public class HashMapApp {

    public static void main(String[] args) {
        // 初始化、逐个赋值
        HashMap courseMap= new HashMap();
        // size() 列表长度
        System.out.println("size():\t" + courseMap.size());
        // isEmpty() 是否为空
        System.out.println("isEmpty():\t" + courseMap.isEmpty());
        System.out.println("courseMap:\t" + courseMap);
        // put() 插入
        courseMap.put(101,"Java");
        System.out.println("put(101,\"Java\"):\t" + courseMap);
        courseMap.put(102,"Python");
        System.out.println("put(102,\"Python\"):\t" + courseMap);
        courseMap.put(103,"Scala");
        System.out.println("put(103,\"Scala\"):\t" + courseMap);
        courseMap.put(104,"Go");
        System.out.println("put(104,\"Go\"):\t" + courseMap);
        courseMap.put(105,"PHP");
        System.out.println("put(105,\"PHP\"):\t" + courseMap);
        // keySet() 查看key组成的集合
        System.out.println("keySet():\t" + courseMap.keySet());
        // values() 查看value组成的集合
        System.out.println("values():\t" + courseMap.values());
        // entrySet() 查看 key=value 组成的集合
        System.out.println("entrySet():\t" + courseMap.entrySet());
        // get() 根据key查询value
        System.out.println("get(101):\t" + courseMap.get(101));
        // remove() 删除
        System.out.println("remove(101):\t" + courseMap.remove(101));
        System.out.println("courseMap:\t" + courseMap);
        // containsKey() 是否包含指定key
        System.out.println("containsKey(102):\t" + courseMap.containsKey(102));
        // containsValue() 是否包含指定value
        System.out.println("containsValue(\"Python\"):\t" + courseMap.containsValue("Python"));

        // iterator遍历key
        Set keys = courseMap.keySet();
        System.out.print("iterator遍历key:\t");
        Iterator keyIterator = keys.iterator();
        while (keyIterator.hasNext()){
            System.out.print(keyIterator.next() + " ");
        }
        System.out.println();
        // iterator遍历key
        Collection values = courseMap.values();
        System.out.print("iterator遍历values:\t");
        Iterator valueIterator = values.iterator();
        while (valueIterator.hasNext()){
            System.out.print(valueIterator.next() + " ");
        }
        System.out.println();
        // foreach遍历key、value
        System.out.print("foreach遍历key、value:\t");
        for (Integer key:keys){
            System.out.print(key + "=" + courseMap.get(key) + " ");
        }

    }
}

6.2 结果

size(): 0
isEmpty():  true
courseMap:  {}
put(101,"Java"):    {101=Java}
put(102,"Python"):  {101=Java, 102=Python}
put(103,"Scala"):   {101=Java, 102=Python, 103=Scala}
put(104,"Go"):  {101=Java, 102=Python, 103=Scala, 104=Go}
put(105,"PHP"): {101=Java, 102=Python, 103=Scala, 104=Go, 105=PHP}
keySet():   [101, 102, 103, 104, 105]
values():   [Java, Python, Scala, Go, PHP]
entrySet(): [101=Java, 102=Python, 103=Scala, 104=Go, 105=PHP]
get(101):   Java
remove(101):    Java
courseMap:  {102=Python, 103=Scala, 104=Go, 105=PHP}
containsKey(102):   true
containsValue("Python"):    true
iterator遍历key:  102 103 104 105 
iterator遍历values:   Python Scala Go PHP 
foreach遍历key、value: 102=Python 103=Scala 104=Go 105=PHP 

7、TreeMap

7.1 代码

package com.nash.java.core.ch01_collection;

import java.util.*;

/**
 * 映射、关联数组 TreeMap
 * key有序、key不可重复、具有映射关系的集合。
 *
 * 1、Map中的key不可重复。
 */
public class TreeMapApp {

    public static void main(String[] args) {
        // 初始化、逐个赋值
        TreeMap courseMap= new TreeMap();
        // size() 列表长度
        System.out.println("size():\t" + courseMap.size());
        // isEmpty() 是否为空
        System.out.println("isEmpty():\t" + courseMap.isEmpty());
        System.out.println("courseMap:\t" + courseMap);
        // put() 插入
        courseMap.put(101,"Java");
        System.out.println("put(101,\"Java\"):\t" + courseMap);
        courseMap.put(102,"Python");
        System.out.println("put(102,\"Python\"):\t" + courseMap);
        courseMap.put(103,"Scala");
        System.out.println("put(103,\"Scala\"):\t" + courseMap);
        courseMap.put(104,"Go");
        System.out.println("put(104,\"Go\"):\t" + courseMap);
        courseMap.put(105,"PHP");
        System.out.println("put(105,\"PHP\"):\t" + courseMap);

        // descendingMap() 倒序
        System.out.println("descendingMap():\t" + courseMap.descendingMap());
        // descendingKeySet() 倒序查看key组成的集合
        System.out.println("descendingKeySet():\t" + courseMap.descendingKeySet());
        // headMap() 倒序查看key组成的集合

        // keySet() 查看key组成的集合
        System.out.println("keySet():\t" + courseMap.keySet());
        // values() 查看value组成的集合
        System.out.println("values():\t" + courseMap.values());
        // entrySet() 查看 key=value 组成的集合
        System.out.println("entrySet():\t" + courseMap.entrySet());
        // get() 根据key查询value
        System.out.println("get(101):\t" + courseMap.get(101));
        // remove() 删除
        System.out.println("remove(101):\t" + courseMap.remove(101));
        System.out.println("courseMap:\t" + courseMap);
        // containsKey() 是否包含指定key
        System.out.println("containsKey(102):\t" + courseMap.containsKey(102));
        // containsValue() 是否包含指定value
        System.out.println("containsValue(\"Python\"):\t" + courseMap.containsValue("Python"));

        // iterator遍历key
        Set keys = courseMap.keySet();
        System.out.print("iterator遍历key:\t");
        Iterator keyIterator = keys.iterator();
        while (keyIterator.hasNext()){
            System.out.print(keyIterator.next() + " ");
        }
        System.out.println();
        // iterator遍历key
        Collection values = courseMap.values();
        System.out.print("iterator遍历values:\t");
        Iterator valueIterator = values.iterator();
        while (valueIterator.hasNext()){
            System.out.print(valueIterator.next() + " ");
        }
        System.out.println();
        // foreach遍历key、value
        System.out.print("foreach遍历key、value:\t");
        for (Integer key:keys){
            System.out.print(key + "=" + courseMap.get(key) + " ");
        }
    }
}

7.2 结果

size(): 0
isEmpty():  true
courseMap:  {}
put(101,"Java"):    {101=Java}
put(102,"Python"):  {101=Java, 102=Python}
put(103,"Scala"):   {101=Java, 102=Python, 103=Scala}
put(104,"Go"):  {101=Java, 102=Python, 103=Scala, 104=Go}
put(105,"PHP"): {101=Java, 102=Python, 103=Scala, 104=Go, 105=PHP}
descendingMap():    {105=PHP, 104=Go, 103=Scala, 102=Python, 101=Java}
descendingKeySet(): [105, 104, 103, 102, 101]
keySet():   [101, 102, 103, 104, 105]
values():   [Java, Python, Scala, Go, PHP]
entrySet(): [101=Java, 102=Python, 103=Scala, 104=Go, 105=PHP]
get(101):   Java
remove(101):    Java
courseMap:  {102=Python, 103=Scala, 104=Go, 105=PHP}
containsKey(102):   true
containsValue("Python"):    true
iterator遍历key:  102 103 104 105 
iterator遍历values:   Python Scala Go PHP 
foreach遍历key、value: 102=Python 103=Scala 104=Go 105=PHP 

你可能感兴趣的:(java.core.ch01_collection)