package com.demo.springlearn.entity;
import java.util.*;
public class MapIterator {
public static void main(String[] args) {
Map coursesMap = new HashMap();
coursesMap.put(1, "C");
coursesMap.put(2, "C++");
coursesMap.put(3, "Java");
coursesMap.put(4, "Spring Framework");
coursesMap.put(5, "Hibernate ORM framework");
Iterator> iterator = coursesMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry next = iterator.next();
Integer key = next.getKey();
String value = next.getValue();
System.out.println(key + value);
}
Iterator iterator1 = coursesMap.keySet().iterator();
while (iterator.hasNext()) {
Integer key = iterator1.next();
String value = coursesMap.get(key);
System.out.println(key + value);
}
for (Map.Entry entry : coursesMap.entrySet()) {
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key + value);
}
coursesMap.forEach((key, value) -> {
System.out.println(key);
System.out.println(value);
}
);
coursesMap.entrySet().stream().forEach(integerStringEntry -> {
System.out.println(integerStringEntry.getKey()+integerStringEntry.getValue());
});
List personList = new ArrayList<>();
personList.add(new Person("欧阳雪",18,"中国",'F'));
personList.add(new Person("Tom",24,"美国",'M'));
personList.add(new Person("Harley",22,"英国",'F'));
personList.add(new Person("向天笑",20,"中国",'M'));
personList.add(new Person("李康",22,"中国",'M'));
personList.add(new Person("小梅",20,"中国",'F'));
personList.add(new Person("何雪",21,"中国",'F'));
personList.add(new Person("李康",22,"中国",'M'));
// 1)找到年龄大于18岁的人并输出;
personList.stream().filter(person ->
person.getAge()>20).forEach(System.out::println);
long countChina = personList.stream().filter(person -> person.getCountry().equals("中国")).count();
System.out.println(countChina);
personList.stream().filter(person -> person.getSex()=='F').limit(2).forEach(System.out::println);
}
}
package com.fc.qa.pointservice.model;
import lombok.Data;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Data
class Person {
private String name;
private Integer age;
private String country;
private char sex;
public Person(String name, Integer age, String country, char sex) {
this.name = name;
this.age = age;
this.country = country;
this.sex = sex;
}
public static void main(String[] args) {
List personList = new ArrayList<>();
personList.add(new Person("欧阳雪", 18, "中国", 'F'));
personList.add(new Person("Tom", 24, "美国", 'M'));
personList.add(new Person("Harley", 22, "英国", 'F'));
personList.add(new Person("向天笑", 20, "中国", 'M'));
personList.add(new Person("李康", 22, "中国", 'M'));
personList.add(new Person("小梅", 20, "中国", 'F'));
personList.add(new Person("何雪", 21, "中国", 'F'));
personList.add(new Person("李康", 22, "中国", 'M'));
List country = new ArrayList<>();
personList.stream().sorted((p1, p2) -> {
return p1.getAge().compareTo(p2.getAge());
}).collect(Collectors.groupingBy(Person::getCountry)).forEach((k,v)-> System.out.println(k+v));
Map> pp = personList.stream().sorted((p1, p2) -> {
return p1.getAge().compareTo(p2.getAge());
}).collect(Collectors.groupingBy(Person::getCountry));
for(Map.Entry> p:pp.entrySet()){
System.out.print(p.getKey());
System.out.println(p.getValue());
}
}
}
浅谈对Java双冒号::的理解