Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package AV5;
- public class Person implements Comparable<Person>{
- private String name;
- private int age;
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public Person(String line)
- {
- String[] parts=line.split("\\s+");
- this.name=parts[0];
- this.age=Integer.parseInt(parts[1]);
- }
- @Override
- public int compareTo(Person other) {
- return Integer.compare(this.age , other.age);
- }
- @Override
- public String toString() {
- return "Person{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- }
- package AV5;
- import java.io.*;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- import java.util.stream.Collectors;
- public class OldestPersonTest {
- public static List<Person> readData(InputStream inputStream) {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
- return bufferedReader.lines()
- .map(line -> new Person(line))
- .collect(Collectors.toList());
- }
- public static void main(String[] args) {
- File file = new File("C:\\Users\\User\\Desktop\\Napredno Sept\\src\\AV5\\people");
- try {
- List<Person> people = readData(new FileInputStream(file));
- Collections.sort(people);
- System.out.println(people.get(people.size() - 1));
- if(people.stream().max(Comparator.naturalOrder()).isPresent())
- {
- System.out.println(people.stream().max(Comparator.naturalOrder()).get());
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment