List:有序的集合,可以重复
set:无序的集合,不可以重复
map:key-value(web基础)
List和set Collection:
Collection<E>
add(E)用于添加元素
remove(E):用于删除元素
size():返回元素的个数
isEmpty():个数是否为0
add和remove方法的使用
package cn.tedu.collection;
import java.util.ArrayList;
import java.util.Collection;
//测试Collection接口的常用方法
public class Demo {
/**
* jdk5.0之后出现的
* Collection<String> coll = new ArrayList<String>();
* @param args
* jdk1.7之后
* Collection<String> coll = new ArrayList<>();
*/
public static void main(String[] args) {
Collection<String> coll = new ArrayList<>();
//必须只能在尖括号里面放入引用类型
Collection<Integer> coll1 = new ArrayList<>();
System.out.println(coll.isEmpty());//true
coll1.add(1);//里面写进本数据类型是因为其进行了自动封箱
coll.add("abcde");
coll.add("hello");
boolean b =coll.isEmpty();//null size=0
System.out.println(b);//false
System.out.println(coll.size());//得到集合里面的元素的个数
//看集合里面是否包含“hello”
System.out.println(coll.contains("hello"));//true
coll.remove("hello");
System.out.println(coll.size());//1
coll.clear();
System.out.println(coll.isEmpty());//true
}
}
将list按顺序输出到服务台的三种方式:
for(E e:collection){}:增强for循环输出
iterator:使用迭代器
forEach():使用lambda表达式输出
package cn.tedu.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.omg.Messaging.SyncScopeHelper;
public class Demo2 {
public static void main(String[] args) {
Collection<String> coll = new ArrayList<>();
Collection<Integer> coll1 = new ArrayList<>();
coll1.add(1);//里面写进本数据类型是因为其进行了自动封箱
coll.add("abcde");
coll.add("