联想网站建设摘要微信开发者文档官网
java-集合整理0.9.0
- 基本结构
 - 基本概念
 - 实例化举例
 - 遍历
 - 获取指定值
 
2024年10月17日09:43:16–0.9.0
2024年10月18日11:00:59—0.9.4
基本结构
- Collection 是最顶级的接口。
 - 分为 List 和 Set 两大类。
 - List 分为:ArrayList、LinkedList、Vector。
 - Set 分为:HashSet、TreeSet。
 - HashSet 又包含了 LinkedHashSet。
 
基本概念
- list是有序、可重复、有索引
 - set是无序、不可重复、无索引
 - list、set是单列集合
 - map是双列结合
 
实例化举例
- 指明其实现类为ArrayList
 
Collection collection=new ArrayList<>();
- 指明其实现类为LinkedList
 
Collection collection=new LinkedList<>();
- 指明其实现类为HashSet
 
Collection collection=new HashSet<>();
- 指明其实现类为TreeSet
 
Collection collection=new TreeSet<>();
遍历
- 迭代器
 
Collection<String> coll= new ArrayList<>();
Iterator<String> it = coll.iterator();
while(it.hasNext()){String str = it.next();System.out.print(str);
}
 
- 迭代器加for
 
TreeSet<String> tree = new TreeSet<String>(Arrays.asList("234","56","577","78"));
for(Iterator<String> i=tree.iterator(); i.hasNext();){System.out.println(i.next());
}
 
- for增强
 
Collection<String> coll= new ArrayList<>();
for (String s : coll) {s="qqq";}
 
- forEach的lambda表达式
 
Collection<String> coll= new ArrayList<>();
coll.forEach((String s) ->System.out.println(s));
 
- for循环
 
Collection<String> coll= new ArrayList<>();
for(int i=0;i<coll.size();i++){System.out.print(coll.get(i));
}
 
获取指定值
- list有索引直接可通过get方法获取
 - set没有索引,不能通过get方法获取
 
