Java SE---Collection
a,迭代器 Iterable
b,Collection
1、List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | 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); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2019-03-04 JavaSE---内部类