Java – Java 8 Comparator to replace Guava compound comparing

Guava example

Collection.sort(list, 
    Ordering.from((Comparator<Item>) Comparator.comparingInt(item -> item.getId()))
    .compound(Comparator.comparing(Item::getName)))

Java 8 example

Comparator<Item> compoundComparator
      = Comparator.comparingInt(Item::getId)
        .thenComparing(Item::getName);

Collection.sort(list, compoundComparator)
Advertisements
Posted in Programming, Java | Leave a comment

Java – Guava API to implement compareTo with considering sorting ordering

Example of a chained comparison statement.

   public int compareTo(Foo that) {
     return ComparisonChain.start()
         .compare(this.aString, that.aString)
         .compare(this.anInt, that.anInt)
         .compare(this.anEnum, that.anEnum, Ordering.natural().nullsLast())
         .result();
   }
Posted in Programming, Java | Leave a comment

Java – The article of Stream API under the hood

The following article is explaining the under the hood Java 8 Stream API.

https://www.ibm.com/developerworks/library/j-java-streams-3-brian-goetz/index.html

Posted in Java, Programming | Leave a comment

Java – Converting Cassandra ResultSet to Java 8 Stream

Statement stmt = QueryBuilder().select().all().from('table');
ResultSet rs = session.execute(stmt);

StreamSupport.stream(
    Spliterators.spliteratorUnknownSize(
                rs.iterator(), Spliterator.ORDERED), false)
.forEach(row -> {
   ...
});

As input stream is sorted by partition key and input order is important, the following example can be used.

Statement stmt = QueryBuilder().select().all().from('table');
ResultSet rs = session.execute(stmt);

StreamSupport.stream(
    Spliterators.spliteratorUnknownSize(
                rs.iterator(), Spliterator.ORDERED | Spliterator.SORTED), false)
.forEach(row -> {
   ...
});
Posted in cassandra, Java, Programming | Leave a comment

Angular – Proxy settings to avoid CORS

Create a proxy.config.json

{
  "/api": {
    "target": "http://localhost:8080",
    "secure": false
  }
}

run ng serve with a configuration file.

ng serve --proxy-config proxy.config.json
Posted in angular, Programming | Leave a comment

Java – Spring Boot 2.0 WebFlux configuration for static resources

    @Value("classpath:/static/index.html")
    private Resource indexHtml;

    @Bean
    RouterFunction<?> routerFunction() {
        RouterFunction router = RouterFunctions.resources("/**", new ClassPathResource("static/"))
                // workaround solution for forwarding / to /index.html
                .andRoute(GET("/"), request -> {
                    return ServerResponse.ok().contentType(TEXT_HTML).syncBody(indexHtml);
                });
        return router;
    }
Posted in Java, Programming | Leave a comment

Java – Read CSV file from a stream

try (final CSVReader reader = new CSVReader(new BufferedReader(new InputStreamReader(inputStream)))) {
    String[] line = null;

    while ((line = reader.readNext()) != null) {
              
    }

} catch (Exception e) {
...
}

Posted in Java, Programming | Leave a comment