From 3fe81a3e075ae05044038788b23fa8729115f734 Mon Sep 17 00:00:00 2001 From: Niket Agrawal Date: Fri, 3 Nov 2023 03:49:06 +0530 Subject: [PATCH] Formatting changes --- .../core-java-persistence-3/pom.xml | 6 --- .../ResultSetToMapUnitTest.java | 44 ++++++++----------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/persistence-modules/core-java-persistence-3/pom.xml b/persistence-modules/core-java-persistence-3/pom.xml index 6868da27be..26ac2a5218 100644 --- a/persistence-modules/core-java-persistence-3/pom.xml +++ b/persistence-modules/core-java-persistence-3/pom.xml @@ -16,11 +16,6 @@ h2 ${h2.version} - - org.springframework - spring-jdbc - ${spring-jdbc.version} - commons-dbutils commons-dbutils @@ -29,7 +24,6 @@ 2.1.214 - 5.3.29 1.8.1 \ No newline at end of file diff --git a/persistence-modules/core-java-persistence-3/src/test/java/com/baeldung/resultsettomap/ResultSetToMapUnitTest.java b/persistence-modules/core-java-persistence-3/src/test/java/com/baeldung/resultsettomap/ResultSetToMapUnitTest.java index b483cc04c9..c8a6351804 100644 --- a/persistence-modules/core-java-persistence-3/src/test/java/com/baeldung/resultsettomap/ResultSetToMapUnitTest.java +++ b/persistence-modules/core-java-persistence-3/src/test/java/com/baeldung/resultsettomap/ResultSetToMapUnitTest.java @@ -33,48 +33,43 @@ public class ResultSetToMapUnitTest { private void initialDataSetup() throws SQLException { Statement statement = connection.createStatement(); - String sql = "CREATE TABLE EMPLOYEE (empId INTEGER not null, empName VARCHAR(50), empCity VARCHAR(50), PRIMARY KEY (empId))"; - statement.executeUpdate(sql); - List sqlQueryList = Arrays.asList("INSERT INTO EMPLOYEE VALUES (1, 'Steve','London')", "INSERT INTO EMPLOYEE VALUES (2, 'John','London')", "INSERT INTO EMPLOYEE VALUES (3, 'David', 'Sydney')", - "INSERT INTO EMPLOYEE VALUES (4, 'Kevin','London')", "INSERT INTO EMPLOYEE VALUES (5, 'Jade', 'Sydney')"); + String ddlQuery = "CREATE TABLE employee (empId INTEGER not null, empName VARCHAR(50), empCity VARCHAR(50), PRIMARY KEY (empId))"; + statement.execute(ddlQuery); + List sqlQueryList = Arrays.asList("INSERT INTO employee VALUES (1, 'Steve','London')", "INSERT INTO employee VALUES (2, 'John','London')", "INSERT INTO employee VALUES (3, 'David', 'Sydney')", + "INSERT INTO employee VALUES (4, 'Kevin','London')", "INSERT INTO employee VALUES (5, 'Jade', 'Sydney')"); + for (String query : sqlQueryList) { - statement.executeUpdate(query); + statement.execute(query); } } @Test - public void whenUsingNativeJava_thenConvertResultSetToMap() throws SQLException { - ResultSet resultSet = connection.prepareStatement("SELECT * FROM EMPLOYEE") - .executeQuery(); + public void whenUsingContainsKey_thenConvertResultSetToMap() throws SQLException { + ResultSet resultSet = connection.prepareStatement("SELECT * FROM employee").executeQuery(); Map> valueMap = new HashMap<>(); while (resultSet.next()) { String empCity = resultSet.getString("empCity"); String empName = resultSet.getString("empName"); if (!valueMap.containsKey(empCity)) { - valueMap.putIfAbsent(empCity, new ArrayList<>()); + valueMap.put(empCity, new ArrayList<>()); } - valueMap.get(empCity) - .add(empName); + valueMap.get(empCity).add(empName); } - assertEquals(3, valueMap.get("London") - .size()); + assertEquals(3, valueMap.get("London").size()); } @Test - public void whenUsingLambda_thenConvertResultSetToMap() throws SQLException { - ResultSet resultSet = connection.prepareStatement("SELECT * FROM EMPLOYEE") - .executeQuery(); + public void whenUsingComputeIfAbsent_thenConvertResultSetToMap() throws SQLException { + ResultSet resultSet = connection.prepareStatement("SELECT * FROM employee").executeQuery(); Map> valueMap = new HashMap<>(); while (resultSet.next()) { String empCity = resultSet.getString("empCity"); String empName = resultSet.getString("empName"); - valueMap.computeIfAbsent(empCity, data -> new ArrayList<>()) - .add(empName); + valueMap.computeIfAbsent(empCity, data -> new ArrayList<>()).add(empName); } - assertEquals(3, valueMap.get("London") - .size()); + assertEquals(3, valueMap.get("London").size()); } @Test @@ -86,16 +81,15 @@ public class ResultSetToMapUnitTest { while (resultSet.next()) { String empCity = resultSet.getString("empCity"); String empName = resultSet.getString("empName"); - result.computeIfAbsent(empCity, data -> new ArrayList<>()) - .add(empName); + result.computeIfAbsent(empCity, data -> new ArrayList<>()).add(empName); } return result; } }; + QueryRunner run = new QueryRunner(); - Map> valueMap = run.query(connection, "SELECT * FROM EMPLOYEE", handler); - assertEquals(3, valueMap.get("London") - .size()); + Map> valueMap = run.query(connection, "SELECT * FROM employee", handler); + assertEquals(3, valueMap.get("London").size()); } @After