Good afternoon! I am new to JAVA and JSON. I'm using Jackson. The program does the following from the incoming JSON file:
Gives out a list of people between the ages of 20 and 30;
Unique list of cities;
The number of people with an age interval of 0-10, 11-20, 21-30, etc.
The program consists of two classes
Main.java
package com.testsample;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
try {
List<Data> data = Arrays.asList(mapper.readValue(Paths.get("C:\\dataClients.json").toFile(), Data[].class));
List<Data> age20to30 = data
.stream()
.filter(p -> p.getAge() >= 20 && p.getAge() < 30)
.sorted(Comparator.comparing(p -> p.getLastName() + " " + p.getFirstName()))
.collect(Collectors.toList());
age20to30.forEach(System.out::println);
Set<String> cities = data
.stream()
.map(Data::getCity)
.collect(Collectors.toCollection(TreeSet::new));
cities.forEach(System.out::println);
Map<String, Long> byAges = data
.stream()
.collect(Collectors.groupingBy(Data::getAgeGroup, TreeMap::new, Collectors.counting()));
System.out.println(byAges);
} catch (JsonParseException jsonParseException) {
jsonParseException.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Data.java
package com.testsample;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.time.LocalDate;
import java.time.Period;
public class Data {
private int id;
private String firstName;
private String lastName;
private LocalDate dateOfBirth;
private String city;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
Data() {
}
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
public LocalDate getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(LocalDate dob) {
if (dob.isAfter(LocalDate.now())) {
dob = dob.minusYears(100);
}
this.dateOfBirth = dob;
}
public Data(int id, String firstName, String lastName, String city, LocalDate dateOfBirth) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.city = city;
this.dateOfBirth = dateOfBirth;
}
#JsonIgnore
public int getAge() {
return Period.between(dateOfBirth, LocalDate.now()).getYears();
}
#JsonIgnore
public String getAgeGroup() {
int age = getAge();
if (age < 11) {
return "0..10";
}
return (age / 10 * 10 + 1) + ".." + ((age / 10 + 1) * 10);
}
#Override
public String toString() {
return "[id = " + id + ", age=" + getAge() + ", firstName = " + firstName + ", lastName = " + lastName + ", dateOfBirth = " + dateOfBirth + ", city = " + city + "]";
}
}
Json file
[
{
"id": "1",
"firstName": "Lesley",
"lastName": "Bryan",
"dateOfBirth": "1961-11-28",
"city": "Southampton–Portsmouth"
},
{
"id": "2",
"firstName": "Edward",
"lastName": "Houston",
"dateOfBirth": "1992-10-05",
"city": "Southampton–Portsmouth"
},
{
"id": "3",
"firstName": "Donald",
"lastName": "Ross",
"dateOfBirth": "1979-12-10",
"city": "Glasgow"
},
{
"id": "4",
"firstName": "Peter",
"lastName": "Kelly",
"dateOfBirth": "2004-03-17",
"city": "Birmingham–Wolverhampton"
},
{
"id": "5",
"firstName": "Anthony",
"lastName": "McKinney",
"dateOfBirth": "1968-03-06",
"city": "Liverpool"
},
{
"id": "6",
"firstName": "David",
"lastName": "Stewart",
"dateOfBirth": "1973-04-11",
"city": "Leeds–Bradford"
},
{
"id": "7",
"firstName": "Christopher",
"lastName": "Austin",
"dateOfBirth": "1974-12-28",
"city": "Birmingham–Wolverhampton"
},
{
"id": "8",
"firstName": "Alvin",
"lastName": "Hodge",
"dateOfBirth": "1958-11-25",
"city": "Newcastle upon Tyne–Sunderland"
},
{
"id": "9",
"firstName": "Gerald",
"lastName": "Higgins",
"dateOfBirth": "1955-06-28",
"city": "Liverpool"
},
{
"id": "10",
"firstName": "Amos",
"lastName": "Owens",
"dateOfBirth": "2001-01-16",
"city": "Manchester-Salford"
},
{
"id": "11",
"firstName": "Christian",
"lastName": "Bishop",
"dateOfBirth": "1950-11-14",
"city": "Nottingham"
},
{
"id": "12",
"firstName": "Robert",
"lastName": "Caldwell",
"dateOfBirth": "1980-12-08",
"city": "Manchester-Salford"
},
{
"id": "13",
"firstName": "Brian",
"lastName": "Heath",
"dateOfBirth": "2002-09-23",
"city": "Newcastle upon Tyne–Sunderland"
},
{
"id": "14",
"firstName": "Mark",
"lastName": "Anthony",
"dateOfBirth": "1992-01-08",
"city": "London"
},
{
"id": "15",
"firstName": "Mark",
"lastName": "Watson",
"dateOfBirth": "1991-07-27",
"city": "Nottingham"
},
{
"id": "16",
"firstName": "Charles",
"lastName": "Stafford",
"dateOfBirth": "1990-01-26",
"city": "Birmingham–Wolverhampton"
},
{
"id": "17",
"firstName": "Steven",
"lastName": "Merritt",
"dateOfBirth": "1963-12-04",
"city": "Leeds–Bradford"
},
{
"id": "18",
"firstName": "John",
"lastName": "Holmes",
"dateOfBirth": "1952-04-22",
"city": "Southampton–Portsmouth"
},
{
"id": "19",
"firstName": "Mervin",
"lastName": "Lewis",
"dateOfBirth": "1995-10-27",
"city": "Birmingham–Wolverhampton"
},
{
"id": "20",
"firstName": "Peter",
"lastName": "Marsh",
"dateOfBirth": "1963-12-10",
"city": "Glasgow"
},
{
"id": "21",
"firstName": "Piers",
"lastName": "Harrington",
"dateOfBirth": "1985-04-27",
"city": "London"
},
{
"id": "22",
"firstName": "Matthew",
"lastName": "O’Brien’",
"dateOfBirth": "1959-01-19",
"city": "Manchester-Salford"
},
{
"id": "23",
"firstName": "Blaze",
"lastName": "Williamson",
"dateOfBirth": "1959-11-26",
"city": "Leeds–Bradford"
},
{
"id": "24",
"firstName": "Ethan",
"lastName": "Harrison",
"dateOfBirth": "1999-08-27",
"city": "Southampton–Portsmouth"
},
{
"id": "25",
"firstName": "Blake",
"lastName": "Dennis",
"dateOfBirth": "2003-09-12",
"city": "Manchester-Salford"
},
{
"id": "26",
"firstName": "Isaac",
"lastName": "Jordan",
"dateOfBirth": "1994-08-08",
"city": "London"
},
{
"id": "27",
"firstName": "Winfred",
"lastName": "Washington",
"dateOfBirth": "1977-04-24",
"city": "Sheffield"
},
{
"id": "28",
"firstName": "Brent",
"lastName": "Hoover",
"dateOfBirth": "1986-09-28",
"city": "Sheffield"
},
{
"id": "29",
"firstName": "Jacob",
"lastName": "Benson",
"dateOfBirth": "1954-12-04",
"city": "Sheffield"
},
{
"id": "30",
"firstName": "Thomas",
"lastName": "Martin",
"dateOfBirth": "1954-11-18",
"city": "London"
},
{
"id": "31",
"firstName": "Ferdinand",
"lastName": "Douglas",
"dateOfBirth": "1989-01-28",
"city": "Southampton–Portsmouth"
},
{
"id": "32",
"firstName": "Cecil",
"lastName": "Sutton",
"dateOfBirth": "1982-08-16",
"city": "Leeds–Bradford"
},
{
"id": "33",
"firstName": "Peter",
"lastName": "Warner",
"dateOfBirth": "1950-03-13",
"city": "Birmingham–Wolverhampton"
},
{
"id": "34",
"firstName": "Williamя",
"lastName": "Gallagher",
"dateOfBirth": "1997-01-04",
"city": "Nottingham"
},
{
"id": "35",
"firstName": "Frank",
"lastName": "Hensley",
"dateOfBirth": "1980-03-15",
"city": "Sheffield"
},
{
"id": "36",
"firstName": "David",
"lastName": "Summers",
"dateOfBirth": "1981-08-01",
"city": "Sheffield"
},
{
"id": "37",
"firstName": "Melvin",
"lastName": "Allison",
"dateOfBirth": "1982-10-08",
"city": "Newcastle upon Tyne–Sunderland"
},
{
"id": "38",
"firstName": "Robert",
"lastName": "Bell",
"dateOfBirth": "1966-06-27",
"city": "Glasgow"
},
{
"id": "39",
"firstName": "Edward",
"lastName": "Gordon",
"dateOfBirth": "1986-07-20",
"city": "Newcastle upon Tyne–Sunderland"
},
{
"id": "40",
"firstName": "Charles",
"lastName": "Hicks",
"dateOfBirth": "1960-06-12",
"city": "Newcastle upon Tyne–Sunderland"
},
{
"id": "41",
"firstName": "Joshua",
"lastName": "Wheeler",
"dateOfBirth": "1979-02-11",
"city": "Southampton–Portsmouth"
},
{
"id": "42",
"firstName": "Leonard",
"lastName": "Hall",
"dateOfBirth": "1974-09-16",
"city": "Southampton–Portsmouth"
},
{
"id": "43",
"firstName": "Jerome",
"lastName": "Hill",
"dateOfBirth": "2002-06-04",
"city": "Liverpool"
},
{
"id": "44",
"firstName": "Matthew",
"lastName": "Hawkins",
"dateOfBirth": "1968-02-22",
"city": "Birmingham–Wolverhampton"
},
{
"id": "45",
"firstName": "Archibald",
"lastName": "Russell",
"dateOfBirth": "1965-10-29",
"city": "Manchester-Salford"
},
{
"id": "46",
"firstName": "Charles",
"lastName": "Little",
"dateOfBirth": "1954-09-07",
"city": "Glasgow"
},
{
"id": "47",
"firstName": "Neil",
"lastName": "Dean",
"dateOfBirth": "1979-12-31",
"city": "Sheffield"
},
{
"id": "48",
"firstName": "Jeremy",
"lastName": "Norris",
"dateOfBirth": "1955-07-21",
"city": "Glasgow"
},
{
"id": "49",
"firstName": "Christopher",
"lastName": "Holland",
"dateOfBirth": "1994-10-25",
"city": "Sheffield"
},
{
"id": "50",
"firstName": "Myron",
"lastName": "Carroll",
"dateOfBirth": "1982-01-16",
"city": "Glasgow"
},
{
"id": "51",
"firstName": "Hugo",
"lastName": "Long",
"dateOfBirth": "1962-05-25",
"city": "Leeds–Bradford"
},
{
"id": "52",
"firstName": "David",
"lastName": "Thornton",
"dateOfBirth": "1969-02-02",
"city": "Birmingham–Wolverhampton"
},
{
"id": "53",
"firstName": "David",
"lastName": "Eaton",
"dateOfBirth": "1960-02-27",
"city": "Southampton–Portsmouth"
},
{
"id": "54",
"firstName": "Abner",
"lastName": "Joseph",
"dateOfBirth": "1960-01-29",
"city": "Liverpool"
},
{
"id": "55",
"firstName": "Ethan",
"lastName": "McDowell",
"dateOfBirth": "1983-07-11",
"city": "Sheffield"
},
{
"id": "56",
"firstName": "Anthony",
"lastName": "Barton",
"dateOfBirth": "1972-12-09",
"city": "Glasgow"
},
{
"id": "57",
"firstName": "Anthony",
"lastName": "Green",
"dateOfBirth": "2003-01-26",
"city": "Birmingham–Wolverhampton"
},
{
"id": "58",
"firstName": "Ronald",
"lastName": "Gilbert",
"dateOfBirth": "1988-05-28",
"city": "Leeds–Bradford"
},
{
"id": "59",
"firstName": "Brendan",
"lastName": "White",
"dateOfBirth": "1975-07-02",
"city": "Glasgow"
},
{
"id": "60",
"firstName": "Christopher",
"lastName": "Richards",
"dateOfBirth": "1979-09-23",
"city": "Manchester-Salford"
},
{
"id": "61",
"firstName": "Buck",
"lastName": "Sanders",
"dateOfBirth": "1956-05-03",
"city": "Newcastle upon Tyne–Sunderland"
},
{
"id": "62",
"firstName": "Griffin",
"lastName": "Cannon",
"dateOfBirth": "1962-01-05",
"city": "Leeds–Bradford"
},
{
"id": "63",
"firstName": "Evan",
"lastName": "Nichols",
"dateOfBirth": "1960-01-18",
"city": "Birmingham–Wolverhampton"
},
{
"id": "64",
"firstName": "Peregrine",
"lastName": "Horton",
"dateOfBirth": "1960-10-11",
"city": "Liverpool"
},
{
"id": "65",
"firstName": "Paul",
"lastName": "Hampton",
"dateOfBirth": "1991-08-06",
"city": "Sheffield"
},
{
"id": "66",
"firstName": "Jacob",
"lastName": "Fox",
"dateOfBirth": "1983-05-18",
"city": "Sheffield"
},
{
"id": "67",
"firstName": "John",
"lastName": "Griffith",
"dateOfBirth": "1971-01-03",
"city": "Birmingham–Wolverhampton"
},
{
"id": "68",
"firstName": "Jack",
"lastName": "May",
"dateOfBirth": "1984-09-28",
"city": "Nottingham"
},
{
"id": "69",
"firstName": "Harold",
"lastName": "Owen",
"dateOfBirth": "1965-08-17",
"city": "Southampton–Portsmouth"
},
{
"id": "70",
"firstName": "Abraham",
"lastName": "Hardy",
"dateOfBirth": "1981-10-26",
"city": "Manchester-Salford"
},
{
"id": "71",
"firstName": "Frederick",
"lastName": "Allen",
"dateOfBirth": "1987-08-19",
"city": "London"
},
{
"id": "72",
"firstName": "Peter",
"lastName": "Thompson",
"dateOfBirth": "1992-09-27",
"city": "Sheffield"
},
{
"id": "73",
"firstName": "Bertram",
"lastName": "Hopkins",
"dateOfBirth": "2001-02-23",
"city": "London"
},
{
"id": "74",
"firstName": "Christopher",
"lastName": "Burns",
"dateOfBirth": "1962-05-22",
"city": "Manchester-Salford"
},
{
"id": "75",
"firstName": "Vernon",
"lastName": "Perry",
"dateOfBirth": "1966-10-17",
"city": "Nottingham"
},
{
"id": "76",
"firstName": "Oliver",
"lastName": "Webster",
"dateOfBirth": "1955-03-13",
"city": "Sheffield"
},
{
"id": "77",
"firstName": "Joshua",
"lastName": "Ball",
"dateOfBirth": "1972-03-08",
"city": "Manchester-Salford"
},
{
"id": "78",
"firstName": "Christopher",
"lastName": "Griffin",
"dateOfBirth": "1994-09-26",
"city": "Nottingham"
},
{
"id": "79",
"firstName": "Sherman",
"lastName": "Pearson",
"dateOfBirth": "1959-01-31",
"city": "Liverpool"
},
{
"id": "80",
"firstName": "Gavin",
"lastName": "Barber",
"dateOfBirth": "2003-02-16",
"city": "Manchester-Salford"
},
{
"id": "81",
"firstName": "Brian",
"lastName": "Mills",
"dateOfBirth": "1980-10-17",
"city": "Newcastle upon Tyne–Sunderland"
},
{
"id": "82",
"firstName": "Herbert",
"lastName": "Grant",
"dateOfBirth": "1991-11-28",
"city": "Southampton–Portsmouth"
},
{
"id": "83",
"firstName": "Christopher",
"lastName": "Sherman",
"dateOfBirth": "2002-04-08",
"city": "Sheffield"
},
{
"id": "84",
"firstName": "Patrick",
"lastName": "Morrison",
"dateOfBirth": "1979-07-13",
"city": "Glasgow"
},
{
"id": "85",
"firstName": "Kristopher",
"lastName": "Nash",
"dateOfBirth": "1966-09-10",
"city": "Newcastle upon Tyne–Sunderland"
},
{
"id": "86",
"firstName": "Henry",
"lastName": "Hood",
"dateOfBirth": "1975-10-03",
"city": "Sheffield"
},
{
"id": "87",
"firstName": "Edward",
"lastName": "Walsh",
"dateOfBirth": "2004-04-16",
"city": "Sheffield"
},
{
"id": "88",
"firstName": "Williamя",
"lastName": "Francis",
"dateOfBirth": "1969-08-06",
"city": "Newcastle upon Tyne–Sunderland"
},
{
"id": "89",
"firstName": "Leo",
"lastName": "Richardson",
"dateOfBirth": "1989-12-19",
"city": "Liverpool"
},
{
"id": "90",
"firstName": "Mark",
"lastName": "Day",
"dateOfBirth": "1954-06-06",
"city": "Leeds–Bradford"
},
{
"id": "91",
"firstName": "Charles",
"lastName": "Cross",
"dateOfBirth": "1955-02-23",
"city": "Liverpool"
},
{
"id": "92",
"firstName": "Alban",
"lastName": "Fields",
"dateOfBirth": "1953-09-03",
"city": "Liverpool"
},
{
"id": "93",
"firstName": "Paul",
"lastName": "Rodgers",
"dateOfBirth": "1966-10-07",
"city": "Sheffield"
},
{
"id": "94",
"firstName": "James",
"lastName": "Gregory",
"dateOfBirth": "1965-11-23",
"city": "London"
},
{
"id": "95",
"firstName": "Harry",
"lastName": "Patrick",
"dateOfBirth": "1967-04-27",
"city": "Southampton–Portsmouth"
},
{
"id": "96",
"firstName": "Jacob",
"lastName": "Walters",
"dateOfBirth": "1952-12-09",
"city": "Manchester-Salford"
},
{
"id": "97",
"firstName": "Kenneth",
"lastName": "Thomas",
"dateOfBirth": "1973-05-16",
"city": "Leeds–Bradford"
},
{
"id": "98",
"firstName": "Ernest",
"lastName": "Cobb",
"dateOfBirth": "1985-09-05",
"city": "Glasgow"
},
{
"id": "99",
"firstName": "Joseph",
"lastName": "Small",
"dateOfBirth": "1965-03-06",
"city": "Nottingham"
},
{
"id": "100",
"firstName": "Basil",
"lastName": "Stephens",
"dateOfBirth": "1964-07-29",
"city": "London"
}
]
The result of the program is as follows:
[id = 14, age=28, firstName = Mark, lastName = Anthony, dateOfBirth = 1992-01-08, city = London]
[id = 34, age=23, firstName = Williamя, lastName = Gallagher, dateOfBirth = 1997-01-04, city = Nottingham]
[id = 82, age=28, firstName = Herbert, lastName = Grant, dateOfBirth = 1991-11-28, city = Southampton–Portsmouth]
[id = 78, age=26, firstName = Christopher, lastName = Griffin, dateOfBirth = 1994-09-26, city = Nottingham]
[id = 65, age=29, firstName = Paul, lastName = Hampton, dateOfBirth = 1991-08-06, city = Sheffield]
[id = 24, age=21, firstName = Ethan, lastName = Harrison, dateOfBirth = 1999-08-27, city = Southampton–Portsmouth]
[id = 49, age=25, firstName = Christopher, lastName = Holland, dateOfBirth = 1994-10-25, city = Sheffield]
[id = 2, age=28, firstName = Edward, lastName = Houston, dateOfBirth = 1992-10-05, city = Southampton–Portsmouth]
[id = 26, age=26, firstName = Isaac, lastName = Jordan, dateOfBirth = 1994-08-08, city = London]
[id = 19, age=24, firstName = Mervin, lastName = Lewis, dateOfBirth = 1995-10-27, city = Birmingham–Wolverhampton]
[id = 72, age=28, firstName = Peter, lastName = Thompson, dateOfBirth = 1992-09-27, city = Sheffield]
[id = 15, age=29, firstName = Mark, lastName = Watson, dateOfBirth = 1991-07-27, city = Nottingham]
Birmingham–Wolverhampton
Glasgow
Leeds–Bradford
Liverpool
London
Manchester-Salford
Newcastle upon Tyne–Sunderland
Nottingham
Sheffield
Southampton–Portsmouth
{11..20=10, 21..30=12, 31..40=19, 41..50=16, 51..60=21, 61..70=21, 71..80=1}
Now I need to serialize all received data into one file. They tell me that I need to create another class that will collect the results of the program, and then serialize it into a json file.
Please help in the implementation of this task.
P.S.: All names, surnames and dates from the json file were obtained at random.
You can write the obtained output to HashMap and that hashMap can be written to a file using ObjectMapper like this.
package com.testsample;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
try {
List<Data> data = Arrays.asList(mapper.readValue(Paths.get("C:\\dataClients.json").toFile(), Data[].class));
List<Data> age20to30 = data
.stream()
.filter(p -> p.getAge() >= 20 && p.getAge() < 30)
.sorted(Comparator.comparing(p -> p.getLastName() + " " + p.getFirstName()))
.collect(Collectors.toList());
age20to30.forEach(System.out::println);
Set<String> cities = data
.stream()
.map(Data::getCity)
.collect(Collectors.toCollection(TreeSet::new));
cities.forEach(System.out::println);
Map<String, Long> byAges = data
.stream()
.collect(Collectors.groupingBy(Data::getAgeGroup, TreeMap::new, Collectors.counting()));
System.out.println(byAges);
Map<String, Object> map = new HashMap<>();
map.put("age20To30", age20to30);
map.put("cities", cities);
map.put("byAges", byAges);
// convert map to JSON file
mapper.writeValue(Paths.get("C:\\dataOutput.json").toFile(), map);
} catch (JsonParseException jsonParseException) {
jsonParseException.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Now the content of the output file will be like below
{
"cities": [
"Birmingham–Wolverhampton",
"Glasgow",
"Leeds–Bradford",
"Liverpool",
"London",
"Manchester-Salford",
"Newcastle upon Tyne–Sunderland",
"Nottingham",
"Sheffield",
"Southampton–Portsmouth"
],
"byAges": {
"11..20": 10,
"21..30": 12,
"31..40": 19,
"41..50": 16,
"51..60": 21,
"61..70": 21,
"71..80": 1
},
"age20To30": [
{
"id": 14,
"firstName": "Mark",
"lastName": "Anthony",
"dateOfBirth": "1992-01-08",
"city": "London"
},
{
"id": 34,
"firstName": "Williamя",
"lastName": "Gallagher",
"dateOfBirth": "1997-01-04",
"city": "Nottingham"
}
]
}