SHARE
TWEET

Untitled

a guest Aug 21st, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.ashishsrivastav.java.streams;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class MinFinder {
  7.  
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.         List<Person> people = getMockData();
  12.         Person younger = people.stream().min((p1, p2) -> p1.getAge() > p2.getAge() ? 1 : -1).get();
  13.  
  14.         System.out.println("Younger Person ->  " + younger.toString());
  15.     }
  16.  
  17.     private static List<Person> getMockData() {
  18.         List<Person> people = new ArrayList<>();
  19.  
  20.         Person person = new Person();
  21.         person.setAge(26);
  22.         person.setName("Neo");
  23.  
  24.         Person person1 = new Person();
  25.         person1.setAge(11);
  26.         person1.setName("Trinity");
  27.  
  28.         Person person2 = new Person();
  29.         person2.setAge(44);
  30.         person2.setName("Morpheus");
  31.  
  32.         Person person3 = new Person();
  33.         person3.setAge(65);
  34.         person3.setName("Agent Smith");
  35.  
  36.         Person person4 = new Person();
  37.         person4.setAge(44);
  38.         person4.setName("Oracle");
  39.  
  40.         Person person5 = new Person();
  41.         person5.setAge(33);
  42.         person5.setName("Cypher");
  43.  
  44.         Person person6 = new Person();
  45.         person6.setAge(22);
  46.         person6.setName("Tank");
  47.  
  48.  
  49.         people.add(person);
  50.         people.add(person1);
  51.         people.add(person2);
  52.         people.add(person3);
  53.         people.add(person4);
  54.         people.add(person5);
  55.         people.add(person6);
  56.  
  57.  
  58.         return people;
  59.     }
  60. }
  61.  
  62. class Person {
  63.     private String name;
  64.     private int age;
  65.  
  66.     public String getName() {
  67.         return name;
  68.     }
  69.  
  70.     public void setName(String name) {
  71.         this.name = name;
  72.     }
  73.  
  74.     public int getAge() {
  75.         return age;
  76.     }
  77.  
  78.     public void setAge(int age) {
  79.         this.age = age;
  80.     }
  81.  
  82.     @Override
  83.     public String toString() {
  84.         return "Person{" +
  85.                 "name='" + name + '\'' +
  86.                 ", age=" + age +
  87.                 '}';
  88.     }
  89. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top