map遍历方式
public static void method1() {
HashMap<String, String> stringHashMap = new HashMap<>();
stringHashMap.put("a", "java");
stringHashMap.put("b", "sun");
stringHashMap.put("c", "oracle");
System.out.println("------1遍历----");
Set<Map.Entry<String, String>> entries = stringHashMap.entrySet();
for (Map.Entry<String, String> entry : entries) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
System.out.println("-------2--------");
// 取出所有的key 放到set中
Set<String> keys = stringHashMap.keySet();
for (String key : keys) {
System.out.println(key + " = " + stringHashMap.get(key));
}
System.out.println("----3遍历------");
stringHashMap.forEach(new BiConsumer<String, String>() {
@Override
public void accept(String s, String s2) {
System.out.println(s + " : " + s2);
}
});
//lambda表达式
stringHashMap.forEach((k, v) -> {
System.out.println(k + " = " + v);
});
}
Collections类
内部都是静态方法
1.1.常用方法
public static void collectionsMethods() {
/*ArrayList<Student> integers = new ArrayList<>();
integers.add(new Student());
integers.add(new Student());
integers.add(new Student());
integers.add(new Student());
// 存取有顺
System.out.println(integers);
// 1, sort() 对list排序
Collections.sort(integers);
Collections.sort(integers, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return 0;
}
});
System.out.println(integers);*/
ArrayList<String> arrayList = new ArrayList<>();
ArrayList<String> arrayList2 = new ArrayList<>();
arrayList.add("avb");
arrayList.add("oracle");
arrayList.add("Sun");
arrayList.add("java");
arrayList2.add("Sun");
arrayList2.add("java");
String max = Collections.max(arrayList);
String min = Collections.min(arrayList);
System.out.println("max " + max);
System.out.println("min " + min);
// Collections.reverse(arrayList);
System.out.println("颠倒顺序 " + arrayList);
// Collections.fill(arrayList, "sm");
System.out.println("集合内容 " + arrayList);
int i = Collections.indexOfSubList(arrayList, arrayList2);
System.out.println("子集合第一次出现索引位置 " + i);
//对list重新洗牌
Collections.shuffle(arrayList);
System.out.println(arrayList);
}
Stream
注意点:
在对集合遍历的过程中,不能直接去调用集合对象的增加和删除方法,会抛异常;如果需要在遍历的过程中对集合元素进行删除 可以使用迭代器
- stream流丰富了对集合的操作,提供了较多方法
生成流的方式
public static void streamCreateMethod(){
ArrayList<String> arrayList = new ArrayList<>();
// 1
Stream<String> stream = arrayList.stream();
// 2
Stream<String> a = Stream.of("a", "b", "c", "d");
// 3,
Stream<String> stream1 = Arrays.stream(new String[]{"1", "2"});
}
中间操作(方法返回值类型都是Stream):
sorted() 对流中元素进行排序
map() 对流中的数据进行相同功能的操作
filter() 使用过滤规则来过滤元素
limit() 截取指定长度的流
终止操作(返回值不是Stream)
只能出现一次而且必须在最后
count() 返回流中元素个数
max() 根据规则得到流中最大值
min() 根据规则得到流中最小值
reduce() 归纳 把流中数据集成为一个数
collect() 收集 常用类转集合,把流中数据转到集合中
public static void streamMethods() {
//数据源头
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(10);
arrayList.add(1);
arrayList.add(4);
arrayList.add(3);
arrayList.add(6);
arrayList.add(12);
// 如何生成流
Stream<Integer> stream1 = arrayList.stream();
Stream<Integer> stream2 = stream1.sorted();
// 对流中的数据进行相同功能的操作
Stream<Integer> stream3 = stream2.map((a) -> {
return a * 2;
});
// 过滤元素
Stream<Integer> stream4 = stream3.filter((a) -> {
if (a > 6) {
// 符合规则的保留下来 即返回true
return true;
}
return false;
});
//限制元素个数
Stream<Integer> stream5 = stream4.limit(3);
/* long count = stream5.count();
System.out.println("count " + count);*/
//
/*Optional<Integer> max = stream3.min((o1, o2) -> {
return o1 - o2;
});
System.out.println("max " + max.get());*/
/* Optional<Integer> reduce = stream3.reduce((a, b) -> {
return a + b;
});
System.out.println("reduce " + reduce.get());*/
List<Integer> collect = stream3.collect(Collectors.toList());
/* List<Integer> collect = stream5.collect(Collectors.toList());
System.out.println(collect);
System.out.println(arrayList);*/
/* Stream<Integer> limit = stream1.sorted().map((a) -> {
return a * 2;
}).filter((a) -> {
if (a > 56) {
return true;
}
return false;
}).limit(3);*/
}