Algo.Java.BFS in BinaryTree

https://leetcode.com/problems/maximum-depth-of-n-ary-tree

Example (just add nodes on each level and then handle them)

https://leetcode.com/problems/average-of-levels-in-binary-tree/description/
    public List<Double> averageOfLevels(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<Double> result = new ArrayList<>();

        queue.add(root);
        queue.add(null);

        while (queue.peek() != null) {
            double sum = 0;
            int nodes = 0;

            while (queue.peek() != null) {
                TreeNode node = queue.poll();
                sum += node.val;
                nodes++;
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }

            queue.add(queue.poll());
            result.add(sum / nodes);
        }

        return result;
    }
Posted in Без рубрики | Leave a comment

Java.Jackson.Serialize object to json

ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
mapper.writeValueAsString(obj);
Posted in Без рубрики | Leave a comment

Algo. Traverse linked list

// while

Node curr = head;
while (curr != null) {
  
  curr = curr.next;
}

// recursion

    public static Node traverseWirhRecursion(Node curr) {
        if (curr == null) {
            return null;
        }
        
        System.out.println("diving: " + curr.val);

        Node nodeBeforeFromRight = traverseWirhRecursion(curr.next);

        if (curr != null) {
            System.out.println("returning: " + curr.val);
        }

        return curr;
    }
Posted in Без рубрики | Comments Off on Algo. Traverse linked list

Algo. Java. Remove-duplicates-from-sorted-array

https://github.com/psGringo/algoJava/blob/main/remove-duplicates-from-sorted-array/remove-duplicates-from-sorted-array/src/main/java/org/example/Main.java

Posted in Без рубрики | Comments Off on Algo. Java. Remove-duplicates-from-sorted-array

Java.Hibernate.SimpleExample

/resources/hibernate.properties

hibernate.connection.url = jdbc:mysql://localhost:3307/library
    public static void main(String[] args) {
        SessionFactory sessionFactory = new Configuration()
                .addAnnotatedClass(org.example.entity.AuthorEntity.class)
                .buildSessionFactory();

        try (Session session = sessionFactory.openSession()) {
         List<AuthorEntity> list = session.createQuery("from AuthorEntity", AuthorEntity.class).list();
            AuthorEntity authorEntity = list.get(0);
            System.out.println(authorEntity.getFirstName());

            var transaction = session.beginTransaction();
            authorEntity.setFirstName("Robert");
            session.save(authorEntity);
            transaction.commit();
        }
    }
Posted in Без рубрики | Comments Off on Java.Hibernate.SimpleExample

Java.Jdbc.SimpleExample

build.gradle.kts

dependencies {
    implementation("mysql:mysql-connector-java:8.0.33")
    testImplementation(platform("org.junit:junit-bom:5.10.0"))
    testImplementation("org.junit.jupiter:junit-jupiter")
}

Main.java

    public static void main(String[] args) throws SQLException {
        System.out.printf("Hello and welcome!");

public class Main {

    public static void main(String[] args) throws SQLException {
        System.out.printf("Hello and welcome!");

        try (Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3307", "root", "root"
        )) {
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from library.author");
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                System.out.printf("ID: %d, Name: %s\n", id, name);
            }

            jdbcMetaDataExample(resultSet);

        }
    }

    private static void jdbcMetaDataExample(ResultSet resultSet) throws SQLException {
        var metaData = resultSet.getMetaData();
        for (int i = 1; i < metaData.getColumnCount(); i++) {
            System.out.println(metaData.getColumnName(i));
        }
    }
}
Posted in Без рубрики | Comments Off on Java.Jdbc.SimpleExample

SQL. Calc the sum with case, example

select 
  sum(case town_to
    when 'LosAngeles' then 1 else 0
  end)
from trip;
Posted in Без рубрики | Comments Off on SQL. Calc the sum with case, example

Mullvad – free the internet :)

no more freedom downtime

https://mullvad.net/en

Posted in Без рубрики | Comments Off on Mullvad – free the internet :)

Junit. Jupiter

test order example

https://github.com/junit-team/junit4/wiki/aggregating-tests-in-suites

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
  TestFeatureLogin.class,
  TestFeatureLogout.class,
  TestFeatureNavigate.class,
  TestFeatureUpdate.class
})

public class FeatureTestSuite {
  // the class remains empty,
  // used only as a holder for the above annotations
}
Posted in Без рубрики | Comments Off on Junit. Jupiter

Java. SpringBoot Example how to work with dateTime in Specification

Working example

    public static <T, V> Specification<T> findByWithTypeCasting5(String functionName, String paramName, V param, Class<T> entityType) {

        checkParam(paramName, entityType);

        return ((root, query, criteriaBuilder) -> param == null ? criteriaBuilder.conjunction()
                : criteriaBuilder.equal(criteriaBuilder.function(
                        "TO_CHAR",
                        String.class,
                        root.get(paramName),
                        criteriaBuilder.literal("YYYY-MM-DD HH24:MI:SS.000")
                ),
                param
        ));

    }

just with date

        return ((root, query, criteriaBuilder) -> param == null ? criteriaBuilder.conjunction()
                : criteriaBuilder.equal(
                root.<Date>get(paramName),

                param
        ));

or like that

    public static <T, V> Specification<T> findByWithTypeCasting2(String functionName, String paramName, V param, Class<T> entityType) {

        checkParam(paramName, entityType);

        return ((root, query, criteriaBuilder) -> param == null ? criteriaBuilder.conjunction()
                : criteriaBuilder.equal(criteriaBuilder.function("date", LocalDate.class, root.get(paramName)), param));
    }
Posted in Без рубрики | Comments Off on Java. SpringBoot Example how to work with dateTime in Specification