Java学习随笔之7:java 集合
Collection 父类 (List, ArrayList, HashSet, TreeSet, linkedList, Queue, Stack) Map(HasMap, HasTable)
方法: (首字符为小写)
Add
Remove
Size()
removeAll
Clear
Contains
Collection collection = new ArrayList();
collection.add(111);
collection.add("Tom Liu");
System.out.println(collection.size());
collection.remove(111);
System.out.println(collection.size());
collection.add("Liu Xin");
collection.forEach(obj-> System.out.println(obj)); //Lambda 表达式
Iterator it = collection.iterator(); //用于遍历集合元素
while(it.hasNext())
{
System.out.println((String)it.next());
}
it.forEachRemaining(oo-> System.out.println(oo)); //lambda 遍历 Iterator
for (Object obj: collection) //使用foreach 遍历
{
System.out.println(obj);
}