1、集合数组互相转换
List<String> strList = new ArrayList<String>();
strList.add("张三");
strList.add("李四");
strList.add("王五");
strList.add("赵六");
strList.add("钱七");
strList.forEach(
i->{
System.out.println("原始集合中的内容:==="+i);
});
System.out.println("==========================");
String[] strArray = strList.toArray(new String[strList.size()]);
Arrays.stream(strArray).forEach(i->System.out.println("转换后的数组中的内容:==="+i));
System.out.println("==========================");
String[] strArray1 = new String[]{"张三","李四","王五","赵六","钱七"};
Arrays.stream(strArray1).forEach(i->System.out.println("转换前的数组中的内容:==="+i));
System.out.println("==========================");
List<String> strList1 = Arrays.asList(strArray1);
strList1.forEach(i->{
System.out.println("转换后的集合中的内容:==="+i);
});
2.获取集合最大值最小值
2.1 普通集合
2.1.1 字符串根据每个字符的ASCLL码的值进行对比(从前往后进行比较)
List<String> stringList = new ArrayList<String>();
stringList.add("q");
stringList.add("qw");
stringList.add("qwertyuiop");
stringList.add("ba");
stringList.add("ab");
stringList.add("ac");
System.out.println("最大值: " + Collections.max(stringList));
System.out.println("最小值: " + Collections.min(stringList));
2.1.2 数值类型根据数字的大小进行对比
List<Integer> integerList = new ArrayList<Integer>();
integerList.add(0);
integerList.add(5);
integerList.add(985);
integerList.add(98);
integerList.add(6568);
integerList.add(-1);
integerList.add(-12);
System.out.println("最大值: " + Collections.max(integerList));
System.out.println("最小值: " + Collections.min(integerList));
2.2 集合中存放对象。根据集合中的对象的某个属性的最大值最小值获取对象
2.2.1 创建一个实体类Person
package com.sm.text;
public class Person {
private String name;
private Integer age;
public Person() {
super();
}
public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "person [name=" + name + ", age=" + age + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
2.2.2 获取根据某个属性获取最大值最小值。或者获取最大值最小值的对象。
List<Person> personList = new ArrayList<Person>();
Person person1 = new Person("张三", 14);
Person person2 = new Person("李四", 17);
Person person3 = new Person("王五", 18);
Person person4 = new Person("赵六", 30);
Person person5 = new Person("钱七", 50);
personList.add(person1);
personList.add(person2);
personList.add(person3);
personList.add(person4);
personList.add(person5);
Person maxAgePerson = personList.stream().max(Comparator.comparing(Person::getAge)).get();
int maxAge1 = personList.stream().max(Comparator.comparing(Person::getAge)).get().getAge();
System.out.println("最大年龄的人==="+maxAgePerson);
System.out.println("最大年龄的人的年龄+++int类型==="+maxAge1);
double maxAge2 = personList.stream().mapToDouble(Person::getAge).max().getAsDouble();
System.out.println("最大年龄的人的年龄+++double类型==="+maxAge2);
Person minAgePerson1 = personList.stream().min(Comparator.comparing(Person::getAge)).get();
int minAge11 = personList.stream().min(Comparator.comparing(Person::getAge)).get().getAge();
System.out.println("最小年龄的人==="+minAgePerson1);
System.out.println("最小年龄的人的年龄+++int类型==="+minAge11);
double minAge12 = personList.stream().mapToDouble(Person::getAge).min().getAsDouble();
System.out.println("最小年龄的人的年龄+++double类型==="+minAge12);
2.2.3 获取集合中某个属性的值的总和(该属性必须是数值型)
List<Person> personList = new ArrayList<Person>();
Person person1 = new Person("张三", 14);
Person person2 = new Person("李四", 17);
Person person3 = new Person("王五", 18);
Person person4 = new Person("赵六", 30);
Person person5 = new Person("钱七", 50);
personList.add(person1);
personList.add(person2);
personList.add(person3);
personList.add(person4);
personList.add(person5);
int sumAge1 = personList.stream().collect(Collectors.summingInt(Person::getAge));
System.out.println("所有人的年龄总和++++int类型方式一==="+sumAge1);
int sumAge2 = personList.stream().mapToInt(Person -> Person.getAge()).sum();
System.out.println("所有人的年龄总和++++int类型方式二===="+sumAge2);
double sumAge3 = personList.stream().mapToDouble(Person::getAge).sum();
System.out.println("所有人的年龄总和++++double类型===="+sumAge3);
2.2.4 获取集合中某个属性的值的平均值(该属性必须是数值型)
List<Person> personList = new ArrayList<Person>();
Person person1 = new Person("张三", 14);
Person person2 = new Person("李四", 17);
Person person3 = new Person("王五", 18);
Person person4 = new Person("赵六", 30);
Person person5 = new Person("钱七", 50);
personList.add(person1);
personList.add(person2);
personList.add(person3);
personList.add(person4);
personList.add(person5);
double avgAge1 = personList.stream().mapToInt(Person -> Person.getAge()).average().getAsDouble();
System.out.println("所有人的年龄平均值++++方式一===="+avgAge1);
double avgAge2 = personList.stream().mapToDouble(Person::getAge).average().getAsDouble();
System.out.println("所有人的年龄平均值++++方式二===="+avgAge2);
2.2.4 根据某个属性进行过滤
List<Person> personList = new ArrayList<Person>();
Person person1 = new Person("张三", 14);
Person person2 = new Person("李四", 17);
Person person3 = new Person("王五", 18);
Person person4 = new Person("赵六", 30);
Person person5 = new Person("钱七", 50);
personList.add(person1);
personList.add(person2);
personList.add(person3);
personList.add(person4);
personList.add(person5);
List<Person> persons1 = personList.stream().filter(Person -> Person.getAge() >= 30).collect(Collectors.toList());
System.out.println("大于等于30岁的人===="+persons1);
List<Person> persons2 = personList.stream().filter(Person -> !Person.getName().equals("张三")).collect(Collectors.toList());
System.out.println("姓名不是张三的人===="+persons2);
2.2.4 根据某个属性的过滤条件查询数量
List<Person> personList = new ArrayList<Person>();
Person person1 = new Person("张三", 14);
Person person2 = new Person("李四", 17);
Person person3 = new Person("王五", 18);
Person person4 = new Person("赵六", 30);
Person person5 = new Person("钱七", 50);
personList.add(person1);
personList.add(person2);
personList.add(person3);
personList.add(person4);
personList.add(person5);
long count1 = personList.stream().filter(Person -> Person.getAge() >= 30).count();
System.out.println("大于等于30岁的人的数量===="+count1);
long count2 = personList.stream().filter(Person -> Person.getName().equals("张三")).count();
System.out.println("姓名是张三的的数量===="+count2);