This article shows how to do Spring Data JPA paging and sorting using the PagingAndSortingRepository interface. Technologies used: Spring Boot 3.1.2 Spring Data JPA H2 in-memory database Java 17 Maven 3 JUnit 5 Spring Integration Tests with TestRestTemplate Unit Tests with Mocking (Mockito) Table of contents: 1. Extends PagingAndSortingRepository 2. Extends JpaRepository 3. Pageable and […]

Read more Spring Data JPA Paging and Sorting example

The Java String compareTo() method compares two strings lexicographically (alphabetical order). It returns a positive number, negative number, or zero. s1.compareTo(s2) if s1 < s2, it returns negative number. if s1 > s2, it returns positive number. if s1 == s2, it returns 0. Table of contents 1. "a".compareTo("c"), negative integer 2. "c".compareTo("a"), positive integer […]

Read more Java String compareTo() examples

Java 8 Stream examples to sort a Map, by keys or by values. 1. Quick Explanation Steps to sort a Map in Java 8. Convert a Map into a Stream Sort it Collect and return a new LinkedHashMap (keep the order) Map result = map.entrySet().stream() .sorted(Map.Entry.comparingByKey()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new)); P.S By […]

Read more Java 8 – How to sort a Map

In Java, the System.getenv() returns an unmodifiable string Map view of the current system environment. Map<String, String> env = System.getenv(); // get PATH environment variable String path = System.getenv("PATH"); Table of contents. 1. Get a specified environment variable 2. UnsupportedOperationException 3. Display all environment variables 4. Sort the environment variables 5. Download Source Code 6. […]

Read more How to get the environment variables in Java

Performs a large sort operation (Aggregation) in a collection, and hits the following error message : MongoDB Console > db.bigdata.aggregate( {$group : {_id : "$range", total : { $sum : 1 }}}, {$sort : {total : -1}} ); #… aggregate failed at Error (<anonymous>) at doassert (src/mongo/shell/assert.js:11:14) #… Error: command failed: { "errmsg" : "exception: […]

Read more MongoDB : Sort exceeded memory limit of 104857600 bytes

In this example, we will show you how to use Java 8 Lambda expression to write a Comparator to sort a List. 1. Classic Comparator example. Comparator<Developer> byName = new Comparator<Developer>() { @Override public int compare(Developer o1, Developer o2) { return o1.getName().compareTo(o2.getName()); } }; 2. Lambda expression equivalent. Comparator<Developer> byName = (Developer o1, Developer o2)->o1.getName().compareTo(o2.getName()); […]

Read more Java 8 Lambda : Comparator example

In Mongodb, you can use sort()to do the date sorting. 1. MongoDB Sorting Examples In Mongodb, to sort a “date” field, issues : //The ‘1’ = sort ascending (oldest to newest) db.domain.find().sort({lastModifiedDate:1}) { "_id" : 1, "lastModifiedDate" : ISODate("2013-08-13T07:18:04.774Z") } { "_id" : 2, "lastModifiedDate" : ISODate("2013-09-03T08:12:16.309Z") } { "_id" : 3, "lastModifiedDate" : ISODate("2013-10-03T08:12:16.309Z") […]

Read more Spring Data MongoDB : get last modified records (date sorting)

Here’s the idea to sort a JSF dataTable list : 1. Column Header Put a commandLink in the column header, if this link is clicked, sort the dataTable list. <h:column> <f:facet name="header"> <h:commandLink action="#{order.sortByOrderNo}"> Order No </h:commandLink> </f:facet> #{o.orderNo} </h:column> 2. Implementation In the managed bean, uses Collections.sort() and custom comparator to sort the list. […]

Read more JSF 2 dataTable sorting example

In this tutorial, it shows the use of java.lang.Comparable and java.util.Comparator to sort a Java object based on its property value. 1. Sort an Array To sort an Array, use the Arrays.sort(). String[] fruits = new String[] {"Pineapple","Apple", "Orange", "Banana"}; Arrays.sort(fruits); int i=0; for(String temp: fruits){ System.out.println("fruits " + ++i + " : " + […]

Read more Java object sorting example (Comparable and Comparator)

Java example to show the use of Arrays.sort() to sort an Array. The code should be self-explanatory. import java.util.Arrays; import java.util.Collections; public class ArraySorting{ public static void main(String args[]){ String[] unsortStringArray = new String[] {"c", "b", "a", "3", "2", "1"}; int[] unsortIntArray = new int[] {7,5,4,6,1,2,3}; System.out.println("Before sort"); System.out.println("— unsortStringArray —"); for(String temp: unsortStringArray){ System.out.println(temp); […]

Read more How to sort an Array in java

By default, the ArrayList’s elements are display according to the sequence it is put inside. Often times, you may need to sort the ArrayList to make it alphabetically order. In this example, it shows the use of Collections.sort(‘List’) to sort an ArrayList. import java.util.ArrayList; import java.util.Collections; import java.util.List; public class SortArrayList{ public static void main(String […]

Read more How to sort an ArrayList in java

In Java, you can use System.getProperties() to get all the system properties. Properties properties = System.getProperties(); properties.forEach((k, v) -> System.out.println(k + ":" + v)); // Java 8 1. Example DisplayApp.java package com.mkyong.display; import java.util.Properties; public class DisplayApp { public static void main(String[] args) { Properties properties = System.getProperties(); // Java 8 properties.forEach((k, v) -> System.out.println(k […]

Read more Java – How to display all System properties