use of com.huangjunlin.learning.model.Person in project java-code-show by CodeShowZz.
the class StreamTest method collectIntoMapTest.
@Test
public void collectIntoMapTest() {
List<Person> personList = new ArrayList<>();
Person tom = new Person(1, "Tom");
Person jack = new Person(2, "Jack");
Person mary = new Person(3, "Mary");
personList.add(tom);
personList.add(jack);
personList.add(mary);
// 将List中每个Person对象的id和name形成Map
Map<Integer, String> personMap = personList.stream().collect(Collectors.toMap(Person::getId, Person::getName));
log.info("personMap:{}", personMap);
Map<Integer, Person> idToPerson = personList.stream().collect(Collectors.toMap(Person::getId, Function.identity()));
log.info("idToPerson:{}", idToPerson);
// 如果流中有重复的元素,需要过滤掉,否则会报错
Stream<Locale> locales = Stream.of(Locale.getAvailableLocales());
Map<String, String> languageNames = locales.collect(Collectors.toMap(Locale::getDisplayLanguage, Locale::getDisplayLanguage, (String existingValue, String newValue) -> {
return existingValue;
}));
log.info("languageNames:{}", languageNames);
}
use of com.huangjunlin.learning.model.Person in project code-sample by SprintBean.
the class defaultInterface method main.
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, CloneNotSupportedException {
Person tom = new Person();
tom.setName("tom");
Person jack = new Person();
jack.setName("jack");
Person mary = new Person();
Person[] persons = new Person[3];
persons[0] = tom;
persons[1] = jack;
persons[2] = mary;
Arrays.sort(persons, Comparator.comparing(Person::getName, Comparator.nullsFirst(Comparator.naturalOrder())));
for (Person p : persons) {
log.info("" + p);
}
new Thread(() -> {
log.info("hello");
}).start();
}
use of com.huangjunlin.learning.model.Person in project code-sample by SprintBean.
the class StreamTest method collectIntoMapTest.
@Test
public void collectIntoMapTest() {
List<Person> personList = new ArrayList<>();
Person tom = new Person(1, "Tom");
Person jack = new Person(2, "Jack");
Person mary = new Person(3, "Mary");
personList.add(tom);
personList.add(jack);
personList.add(mary);
// 将List中每个Person对象的id和name形成Map
Map<Integer, String> personMap = personList.stream().collect(Collectors.toMap(Person::getId, Person::getName));
log.info("personMap:{}", personMap);
Map<Integer, Person> idToPerson = personList.stream().collect(Collectors.toMap(Person::getId, Function.identity()));
log.info("idToPerson:{}", idToPerson);
// 如果流中有重复的元素,需要过滤掉,否则会报错
Stream<Locale> locales = Stream.of(Locale.getAvailableLocales());
Map<String, String> languageNames = locales.collect(Collectors.toMap(Locale::getDisplayLanguage, Locale::getDisplayLanguage, (String existingValue, String newValue) -> {
return existingValue;
}));
log.info("languageNames:{}", languageNames);
}
use of com.huangjunlin.learning.model.Person in project code-sample by SprintBean.
the class StreamTest method collectResultTest.
@Test
public void collectResultTest() {
String[] names = { "Tom", "Jack", "Mary", "Adams", "HuangJunlin" };
// 遍历流中的每一个元素
Stream.of(names).forEach((x) -> {
log.info(x);
});
// 流转换成数组
String[] result = Stream.of(names).toArray(String[]::new);
// 流转换成List
List<String> result2 = Stream.of(names).collect(Collectors.toList());
// 流转换成Set
Set<String> result3 = Stream.of(names).collect(Collectors.toSet());
// 流转换成TreeSet
TreeSet<String> result4 = Stream.of(names).collect(Collectors.toCollection(TreeSet::new));
// 将流中的元素元素直接连接起来
String result5 = Stream.of(names).collect(Collectors.joining());
// 将流中的元素连接起来 中间隔着特定的符号
String result6 = Stream.of(names).collect(Collectors.joining(", "));
// 对象要先转字符串才能连接起来
List<Person> personList = new ArrayList<>();
personList.add(new Person(1, "Tom"));
personList.add(new Person(2, "Jack"));
String result7 = personList.stream().map(Object::toString).collect(Collectors.joining(", "));
// 统计流中的最大值 平均值 最小值等
IntSummaryStatistics summary = Stream.of(names).collect(Collectors.summarizingInt(String::length));
double averageWordLength = summary.getAverage();
double maxWordLength = summary.getMax();
log.info("averageWordLength:{}", averageWordLength);
log.info("maxWordLength:{}", maxWordLength);
}
use of com.huangjunlin.learning.model.Person in project java-code-show by CodeShowZz.
the class defaultInterface method main.
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, CloneNotSupportedException {
Person tom = new Person();
tom.setName("tom");
Person jack = new Person();
jack.setName("jack");
Person mary = new Person();
Person[] persons = new Person[3];
persons[0] = tom;
persons[1] = jack;
persons[2] = mary;
Arrays.sort(persons, Comparator.comparing(Person::getName, Comparator.nullsFirst(Comparator.naturalOrder())));
for (Person p : persons) {
log.info("" + p);
}
new Thread(() -> {
log.info("hello");
}).start();
}
Aggregations