文章目录
一、背景
-
公司同事都在用lamda,自己一直没有尝试去用,感觉好low,尤其在代码审核的时候,感觉到尴尬
-
话不多说,下面是List lamda常用操作写法
二、List常用操作
1. 遍历
注:需要运行测试的同学到第三章copy源码运行
persons为一个List集合,Person对象可查看文章最后源码
- 每个person的name后面都加一个at符号
persons.forEach(person -> { person.setName(person.getName() + "@"); });
2. 过滤
- 把上海的person过滤出来
List<Person> personsFiltered = persons.stream().filter(person -> { return "上海".equals(person.getCity()); }).collect(Collectors.toList());
3. 排序
- 按生日从小到大排序
List<Person> personsSorted = persons.stream().sorted((a, b) -> { return (int) (a.getBirthday().getTime() - b.getBirthday().getTime()); }).collect(Collectors.toList());
4. 按属性分组
- 按城市分组
Map<String, List<Person>> cityGroup = persons.stream() .collect(Collectors.groupingBy(Person::getCity));
5. 自定义分组
-
北京与广州合并为一个北广分组
Map<Object, List<Person>> cityGroupSelf = persons.stream() .collect(Collectors.groupingBy(person -> { if ("北京".equals(person.getCity()) || "广州".equals(person.getCity())) { return "北广"; } else { return person.getCity(); } })); });
6 转map,属性:属性
Map<String, String> attrMap = persons.stream().collect(Collectors.toMap(Person::getName, Person::getCity));
7 转map, 属性:对象本身
Map<String, Person> mapByName = persons.stream().collect(Collectors.toMap(Person::getName, p -> p));
8 转map, 属性:对象本身,但属性有重复,想保留一个,写对比方法
Person person5 = new Person("d@", new Date(1268318348000L), "广州5");
persons.add(person5);
Map<String, Person> mapByName2 = persons.stream()
.collect(Collectors.toMap(Person::getName, p -> p, (k1, k2) -> k2));
9 如果想都保留,使用groupingby
Map<String, List<Test.Person>> dupNameMap = persons.stream().collect(Collectors.groupingBy(Person::getName));
10 获取属性的列表
List<String> names = persons.stream().map(p->p.getName()).collect(Collectors.toList());
System.out.println("list的属性列表"+names);
System.out.println("去重后的列表:"+names.stream().distinct().collect(Collectors.toList()));
三、完整源码
- 给的都是自己写的、可运行的良心代码
package lanxing.com.test;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lanxing.com.entity.MyClass;
/**
* @author <a href="mailto:lanxing@chances.com.cn">lanxing</a>
* @version 2019年3月7日
*
*/
public class Test {
public static void main(String[] args) {
List<Person> persons = new ArrayList<>();
Person person1 = new Person("a", new Date(1268318348000L), "上海");
Person person2 = new Person("b", new Date(952785548000L), "北京");
Person person3 = new Person("c", new Date(700324748000L), "上海");
Person person4 = new Person("d", new Date(1268318348000L), "广州");
persons.add(person1);
persons.add(person2);
persons.add(person3);
persons.add(person4);
// List遍历 (每个name后面都加一个@)
persons.forEach(person -> {
person.setName(person.getName() + "@");
});
persons.forEach(person -> {
System.out.println(person.getName());
});
// 过滤(把上海的person打印出来)
List<Person> personsFiltered = persons.stream().filter(person -> {
return "上海".equals(person.getCity());
}).collect(Collectors.toList());
personsFiltered.forEach(person -> {
System.out.println(person.getName() + ":" + person.getCity());
});
// 排序 (按生日,从小到大)
List<Person> personsSorted = persons.stream().sorted((a, b) -> {
return (int) (a.getBirthday().getTime() - b.getBirthday().getTime());
}).collect(Collectors.toList());
personsSorted.forEach(person -> {
System.out.println(person.getName() + ":" + person.getBirthday());
});
// 按属性分组(按城市分)
Map<String, List<Person>> cityGroup = persons.stream().collect(Collectors.groupingBy(Person::getCity));
cityGroup.forEach((key, value) -> {
System.out.println("============分组:" + key);
value.forEach(person -> {
System.out.println(person.getName() + "-" + person.getCity());
});
});
// 自定义分组(北京与广州作为一个北广分组)
Map<Object, List<Person>> cityGroupSelf = persons.stream().collect(Collectors.groupingBy(person -> {
if ("北京".equals(person.getCity()) || "广州".equals(person.getCity())) {
return "北广";
} else {
return person.getCity();
}
}));
cityGroupSelf.forEach((key, value) -> {
System.out.println("============分组:" + key);
value.forEach(person -> {
System.out.println(person.getName() + "-" + person.getCity());
});
});
// 转map,属性:属性
Map<String, String> attrMap = persons.stream().collect(Collectors.toMap(Person::getName, Person::getCity));
// 转map, 属性:对象本身
Map<String, Person> mapByName = persons.stream().collect(Collectors.toMap(Person::getName, p -> p));
// 转map, 属性:对象本身,但属性有重复,想保留一个,写对比方法
Person person5 = new Person("d@", new Date(1268318348000L), "广州5");
persons.add(person5);
Map<String, Person> mapByName2 = persons.stream()
.collect(Collectors.toMap(Person::getName, p -> p, (k1, k2) -> k2));
//(如果想都保留,使用groupingby)
Map<String, List<Test.Person>> dupNameMap = persons.stream().collect(Collectors.groupingBy(Person::getName));
//获取属性的列表
List<String> names = persons.stream().map(p->p.getName()).collect(Collectors.toList());
System.out.println("list的属性列表"+names);
System.out.println("去重后的列表:"+names.stream().distinct().collect(Collectors.toList()));
// set的遍历
mapByName.entrySet().forEach(m -> {
System.out.println(m.getKey());
});
Set<Entry<String, Test.Person>> entrys = mapByName.entrySet();
for(Entry<String, Test.Person> entry:entrys) {
System.out.println(entry.getValue().getName());
}
Iterator<Entry<String, Test.Person>> ite = entrys.iterator();
while(ite.hasNext()) {
System.out.println(ite.next().getValue().getName());
}
mapByName.keySet().forEach(k -> {
System.out.println(k + ":" + mapByName.get(k));
});
mapByName.forEach((k, v) -> {
System.out.println(v.getName() + ":" + v.getCity());
});
}
static class Person {
private String name;
private Date birthday;
private String city;
public Person() {
}
public Person(String name, Date birthday, String city) {
this.birthday = birthday;
this.name = name;
this.city = city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
}