Click here to watch in Youtube : https://www.youtube.com/watch?v=8Wqtd9xz7sk&list=UUhwKlOVR041tngjerWxVccwPerson.javaenum Gender
{
MALE, FEMALE
}
public class Person
{
private String name;
private String email;
private Gender gender;
private int age;
public Person(String name, String email, Gender gender, int age)
{
super();
this.name = name;
this.email = email;
this.gender = gender;
this.age = age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public Gender getGender()
{
return gender;
}
public void setGender(Gender gender)
{
this.gender = gender;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
@Override
public String toString()
{
return "Person [name=" + name + ", gender=" + gender + ", age=" + age
+ "]";
}
}
StreamMapDemo.javaimport java.util.ArrayList;
import java.util.List;
public class StreamMapDemo
{
public static void main(String[] args)
{
List<Person> personList = new ArrayList<>();
personList.add(new Person("Alice", "alice@gmail.com", Gender.FEMALE, 16));
personList.add(new Person("Bob", "bob@gmail.com", Gender.MALE, 15));
personList.add(new Person("Carol", "carol@gmail.com", Gender.FEMALE, 23));
personList.add(new Person("David", "david@gmail.com", Gender.MALE, 19));
personList.add(new Person("Eric", "eric@gmail.com", Gender.MALE, 26));
/*
* The map operation returns a new stream consisting of elements which
* are the results of applying a given function to the elements of the
* current stream. For example, converting a stream of Objects to a
* stream of String or a stream of primitive numbers.
*
* The Stream API provides 4 methods for the map operation:
*
* map(): transforms a stream of objects of type T to a stream of
* objects of type R.
*
* mapToInt(): transforms a stream of objects to a stream of int
* primitives.
*
* mapToLong(): transforms a stream of objects to a stream of long
* primitives.
*
* mapToDouble(): transforms a stream of objects to a stream of double
* primitives.
*
*/
personList.stream() // Stream<Person>
.map(p -> p.getEmail()) // Stream<String>
.forEach(System.out::println);
System.out.println("\n----------------------\n");
personList.stream() // Stream<Person>
.map(p -> p.getName().toUpperCase()) // Stream<String>
.forEach(System.out::println);
System.out.println("\n----------------------\n");
personList.stream() // Stream<Person>
.mapToInt(p -> p.getAge()) //IntStream
.forEach(age -> System.out.println(age));
}
}
Outputalice@gmail.com
bob@gmail.com
carol@gmail.com
david@gmail.com
eric@gmail.com
----------------------
ALICE
BOB
CAROL
DAVID
ERIC
----------------------
16
15
23
19
26
Click the below link to download the code:https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_map_person_name_age.zip?attredirects=0&d=1Github Link:https://github.com/ramram43210/Java/tree/master/BasicJava/StreamDemo_map_person_name_ageBitbucket Link:https://bitbucket.org/ramram43210/java/src/4c6dd26ede26896338d54f8923608ecad05bf799/BasicJava/StreamDemo_map_person_name_age/?at=masterSee also: All JavaEE Viedos PlaylistAll JavaEE ViedosAll JAVA EE LinksServlets TutorialAll Design Patterns LinksJDBC TutorialJava Collection Framework TutorialJAVA TutorialKids Tutorial