Java SE---Collection

a,迭代器 Iterable

    

  

 

 

 

b,Collection

    

    

 

 

 

 

 

 

 

 

1、List

  

 

  

 

package com.an.collection;

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

/**
 * @description:
 * @author: anpeiyong
 * @date: Created in 2020/3/4 10:04
 * @since:
 */
public class ListTest {

    public static void main(String[] args) {
//        iteratorTest();
//        forTest();
//        foreachTest();
//        foreachMethodTest();
//        streamForeachMethodTest();
        parallelStreamForeachMethodTest();
    }

    public static void iteratorTest(){
        List<String> list=new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");

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

    public static void forTest(){
        List<String> list=new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");

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

    public static void foreachTest(){
        List<String> list=new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");

        for (String s:list){
            System.out.println(s);
        }
    }

    public static void foreachMethodTest(){
        List<String> list=new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");

        list.forEach(s -> {
            System.out.println(s);
        });
    }

    public static void streamForeachMethodTest(){
        List<String> list=new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");

        list.stream().forEach(s -> {
            System.out.println(s);
        });
    }

    public static void parallelStreamForeachMethodTest(){
        List<String> list=new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");

        list.parallelStream().forEach(s -> {
            System.out.println(s);
        });
    }


}

  

 

2、Set

 

  

package com.an.collection;

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

/**
 * @description:
 * @author: anpeiyong
 * @date: Created in 2020/3/4 9:53
 * @since:
 */
public class SetTest {

    public static void main(String[] args) {
//        iteratorTest();
        foreachTest();
    }

    public static void iteratorTest(){
        Set<String> set=new HashSet<>();
        set.add("a");
        set.add("b");
        set.add("c");

        Iterator iterator =set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    public static void foreachTest(){
        Set<String> set=new HashSet<>();
        set.add("a");
        set.add("b");
        set.add("c");

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

  

 

3、Map

  

 

  

package com.an.collection;

import java.util.*;
import java.util.Map;

/**
 * @description:
 * @author: anpeiyong
 * @date: Created in 2020/3/4 9:30
 * @since:
 */
public class MapTest {

    public static void main(String[] args) {
//        iteratorTest();
//        iteratorTest2();
//        foreachTest();
//        foreachTest2();
//        foreachMethodTest();
        valuesTest();
    }

    public static void iteratorTest(){
        HashMap<String,String> map=new HashMap<>();
        map.put("1","a");
        map.put("2","b");
        map.put("3","c");

        Set<String> keySet=map.keySet();
        Iterator<String> iterator =keySet.iterator();
        while (iterator.hasNext()){
            String key=iterator.next();
            System.out.println(map.get(key));
        }
    }

    public static void iteratorTest2(){
        HashMap<String,String> map=new HashMap<>();
        map.put("1","a");
        map.put("2","b");
        map.put("3","c");

        Set<Map.Entry<String,String>> entrySet=map.entrySet();
        Iterator<Map.Entry<String,String>> entryIterator=entrySet.iterator();
        while (entryIterator.hasNext()){
            Map.Entry<String,String> entry=entryIterator.next();
            String key=entry.getKey();
            String value=entry.getValue();

            System.out.println(key+":"+value+"==");
        }
    }

    public static void foreachTest(){
        HashMap<String,String> map=new HashMap<>();
        map.put("1","a");
        map.put("2","b");
        map.put("3","c");
        
        Set<String> keySet=map.keySet();
        for (String s:keySet) {
            System.out.println(map.get(s));
        }
    }

    public static void foreachTest2(){
        HashMap<String,String> map=new HashMap<>();
        map.put("1","a");
        map.put("2","b");
        map.put("3","c");

        Set<Map.Entry<String,String>> entrySet=map.entrySet();
        for (Map.Entry<String,String> s:entrySet) {
            System.out.println(s.getKey()+":"+s.getValue());
        }
    }

    public static void foreachMethodTest(){
        HashMap<String,String> map=new HashMap<>();
        map.put("1","a");
        map.put("2","b");
        map.put("3","c");

        map.forEach((k,v)->{
            System.out.println(k+":"+v);
        });
    }

    public static void valuesTest(){
        HashMap<String,String> map=new HashMap<>();
        map.put("1","a");
        map.put("2","b");
        map.put("3","c");

        Collection<String> collection =map.values();
        System.out.println(collection);
    }

}

  

 

posted on 2020-03-04 09:53  anpeiyong  阅读(341)  评论(0编辑  收藏  举报

导航