回忆一下java的集合
1.常见ArrayList操作
import java.util.ArrayList;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
ArrayList<String> list1=new ArrayList<String>();//创建list集合
ArrayList<String> list2=new ArrayList<String>();//创建list集合
//添加集合元素
list1.add("one");
list1.add("two");
list1.add("three");
System.out.println("list1集合中的元素数量为:=>"+list1.size());
//添加集合元素
list2.add("two");
list2.add("four");
list2.add("six");
System.out.println("list2集合中的元素数量为:=>"+list2.size());
//删除集合中某个元素
list2.remove(2);//删除集合中第三个元素
System.out.println("remove方法之后list2集合的元素数量为:=>"+list2.size());
//遍历集合元素
Iterator it1=list2.iterator();
System.out.println("list2集合的元素为如下:");
while(it1.hasNext()){
System.out.print(it1.next()+"、");
}
System.out.println("执行removeAll方法");
list1.removeAll(list2);
System.out.println("removeAll()之后list1集合元素数量为:=>"+list1.size());
System.out.println("集合list1的元素如下:");
Iterator it2=list1.iterator();
while(it2.hasNext()){
System.out.println(it2.next()+"、");
}
}
}
2.set集合遍历方式
public static void main(String[] args) {
HashSet<String> courseSet = new HashSet<String>(); // 创建一个空的 Set 集合
String course1 = new String("Java入门教程");
String course2 = new String("Python基础教程");
String course3 = new String("C语言学习教程");
String course4 = new String("Golang入门教程");
courseSet.add(course1); // 将 course1 存储到 Set 集合中
courseSet.add(course2); // 将 course2 存储到 Set 集合中
courseSet.add(course3); // 将 course3 存储到 Set 集合中
courseSet.add(course4); // 将 course4 存储到 Set 集合中
Iterator<String> it = courseSet.iterator();
while (it.hasNext()) {
System.out.println("《" + (String) it.next() + "》"); // 输出 Set 集合中的元素
}
System.out.println("有" + courseSet.size() + "套精彩教程!");
}
3.map集合遍历方式
public class Test09 {
public static void main(String[] args) {
HashMap users = new HashMap();
users.put("11", "张浩太"); // 将学生信息键值对存储到Map中
users.put("22", "刘思诚");
users.put("33", "王强文");
users.put("44", "李国量");
users.put("55", "王路路");
System.out.println("******** 学生列表 ********");
Iterator it = users.keySet().iterator();
while (it.hasNext()) {
// 遍历 Map
Object key = it.next();
Object val = users.get(key);
System.out.println("学号:" + key + ",姓名:" + val);
}
Scanner input = new Scanner(System.in);
System.out.println("请输入要删除的学号:");
int num = input.nextInt();
if (users.containsKey(String.valueOf(num))) { // 判断是否包含指定键
users.remove(String.valueOf(num)); // 如果包含就删除
} else {
System.out.println("该学生不存在!");
}
System.out.println("******** 学生列表 ********");
it = users.keySet().iterator();
while (it.hasNext()) {
Object key = it.next();
Object val = users.get(key);
System.out.println("学号:" + key + ",姓名:" + val);
}
}
}
- 在 for 循环中使用 entries 实现 Map 的遍历(最常见和最常用的)。
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("Java入门教程", "http://c.biancheng.net/java/");
map.put("C语言入门教程", "http://c.biancheng.net/c/");
for (Map.Entry<String, String> entry : map.entrySet()) {
String mapKey = entry.getKey();
String mapValue = entry.getValue();
System.out.println(mapKey + ":" + mapValue);
}
}
- 使用 for-each 循环遍历 key 或者 values,一般适用于只需要 Map 中的 key 或者 value 时使用。性能上比 entrySet 较好。
Map<String, String> map = new HashMap<String, String>();
map.put("Java入门教程", "http://c.biancheng.net/java/");
map.put("C语言入门教程", "http://c.biancheng.net/c/");
// 打印键集合
for (String key : map.keySet()) {
System.out.println(key);
}
// 打印值集合
for (String value : map.values()) {
System.out.println(value);
}
- 使用迭代器(Iterator)遍历
Map<String, String> map = new HashMap<String, String>();
map.put("Java入门教程", "http://c.biancheng.net/java/");
map.put("C语言入门教程", "http://c.biancheng.net/c/");
Iterator<Entry<String, String>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
Entry<String, String> entry = entries.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + ":" + value);
}
- 通过键找值遍历,这种方式的效率比较低,因为本身从键取值是耗时的操作。
for(String key : map.keySet()){
String value = map.get(key);
System.out.println(key+":"+value);
}
3.Collection集合遍历
- lambda遍历集合
// 创建一个集合
Collection objs = new HashSet();
objs.add("C语言中文网Java教程");
objs.add("C语言中文网C语言教程");
objs.add("C语言中文网C++教程");
// 调用forEach()方法遍历集合
objs.forEach(obj->System.out.println("集合objs的元素为=>"+obj));
- Iterator接口遍历集合
public class IteratorTest {
public static void main(String[] args) {
// 创建一个集合
Collection objs = new HashSet();
objs.add("C语言中文网Java教程");
objs.add("C语言中文网C语言教程");
objs.add("C语言中文网C++教程");
// 调用forEach()方法遍历集合
// 获取books集合对应的迭代器
Iterator it = objs.iterator();
while (it.hasNext()) {
// it.next()方法返回的数据类型是Object类型,因此需要强制类型转换
String obj = (String) it.next();
System.out.println(obj);
if (obj.equals("C语言中文网C语言教程")) {
// 从集合中删除上一次next()方法返回的元素
it.remove();
}
// 对book变量赋值,不会改变集合元素本身
obj = "C语言中文网Python语言教程";
}
System.out.println(objs);
}
}
3.lambda遍历迭代器
Lambda 表达式来遍历集合元素。
public class IteratorEach {
public static void main(String[] args) {
// 创建一个集合
Collection objs = new HashSet();
objs.add("C语言中文网Java教程");
objs.add("C语言中文网C语言教程");
objs.add("C语言中文网C++教程");
// 获取objs集合对应的迭代器
Iterator it = objs.iterator();
// 使用Lambda表达式(目标类型是Comsumer)来遍历集合元素
it.forEachRemaining(obj -> System.out.println("迭代集合元素:" + obj));
}
}
- foreach 循环迭代访问集合元素,而且更加便捷。如下程序示范了使用 foreach 循环来迭代访问集合元素。
public class ForeachTest {
public static void main(String[] args) {
// 创建一个集合
Collection objs = new HashSet();
objs.add("C语言中文网Java教程");
objs.add("C语言中文网C语言教程");
objs.add("C语言中文网C++教程");
for (Object obj : objs) {
// 此处的obj变量也不是集合元素本身
String obj1 = (String) obj;
System.out.println(obj1);
if (obj1.equals("C语言中文网Java教程")) {
// 下面代码会引发 ConcurrentModificationException 异常
objs.remove(obj);
}
}
System.out.println(objs);
}
}
- 使用 Predicate 来过滤集合。
Java 8 起为 Collection 集合新增了一个 removeIf(Predicate filter) 方法,该方法将会批量删除符合 filter 条件的所有元素。该方法需要一个 Predicate 对象作为参数,Predicate 也是函数式接口,因此可使用 Lambda 表达式作为参数。
public class ForeachTest {
public static void main(String[] args) {
// 创建一个集合
Collection objs = new HashSet();
objs.add(new String("C语言中文网Java教程"));
objs.add(new String("C语言中文网C++教程"));
objs.add(new String("C语言中文网C语言教程"));
objs.add(new String("C语言中文网Python教程"));
objs.add(new String("C语言中文网Go教程"));
// 使用Lambda表达式(目标类型是Predicate)过滤集合
objs.removeIf(ele -> ((String) ele).length() < 12);
System.out.println(objs);
}
}