java 1.8, stream api migration: collect (to be continued)

This commit is contained in:
Anna Kozlova
2014-03-03 11:00:05 +01:00
parent 578ac0e1f1
commit 0cab1e2e81
16 changed files with 376 additions and 7 deletions
@@ -0,0 +1,15 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
List<String> names = persons.stream().map(person -> person.getName()).collect(Collectors.toList());
}
}
@@ -0,0 +1,15 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
List<String> names = persons.stream().filter(person -> person != null).map(person -> person.getName()).collect(Collectors.toList());
}
}
@@ -0,0 +1,15 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
Set<String> names = persons.stream().map(person -> person.getName()).collect(Collectors.toSet());
}
}
@@ -0,0 +1,16 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
public class Collect {
class Person {
String getName() {
return "";
}
}
Set<String> names = new HashSet<>();
void collectNames(List<Person> persons){
names.addAll(persons.stream().map(person -> person.getName()).collect(Collectors.toList()));
}
}
@@ -0,0 +1,15 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
Set<String> names = persons.stream().map(person -> person.getName()).collect(Collectors.toCollection(() -> new LinkedHashSet<>()));
}
}
@@ -0,0 +1,15 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
public abstract class Collect implements Collection<String>{
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
addAll(persons.stream().map(person -> person.getName()).collect(Collectors.toList()));
}
}
@@ -0,0 +1,15 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons, Set<String> names){
names.addAll(persons.stream().map(person -> person.getName()).collect(Collectors.toList()));
}
}
@@ -0,0 +1,18 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
List<String> names = new ArrayList<>();
for (Person person : pers<caret>ons) {
names.add(person.getName());
}
}
}
@@ -0,0 +1,20 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
List<String> names = new ArrayList<>();
for (Person person : pers<caret>ons) {
if (person != null) {
names.add(person.getName());
}
}
}
}
@@ -0,0 +1,18 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
Set<String> names = new HashSet<>();
for (Person person : pers<caret>ons) {
names.add(person.getName());
}
}
}
@@ -0,0 +1,18 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
public class Collect {
class Person {
String getName() {
return "";
}
}
Set<String> names = new HashSet<>();
void collectNames(List<Person> persons){
for (Person person : pers<caret>ons) {
names.add(person.getName());
}
}
}
@@ -0,0 +1,18 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
Set<String> names = new LinkedHashSet<>();
for (Person person : pers<caret>ons) {
names.add(person.getName());
}
}
}
@@ -0,0 +1,17 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
public abstract class Collect implements Collection<String>{
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
for (Person person : pers<caret>ons) {
add(person.getName());
}
}
}
@@ -0,0 +1,17 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons, Set<String> names){
for (Person person : pers<caret>ons) {
names.add(person.getName());
}
}
}