How to Sort HashMap in java

This article covers some of the aspects of sorting Map in Java. Java Map comes in different flavours, like HashMap, TreeMap, LinkedHashMap etc. TreeMap for instance will sort the data based on the keys. We can also pass a custom Comparator to sort based on the keys as per the custom sort algorithm.
We can have two requirements in terms of sorting, first one is sorting by Keys and second is Sorting by Values. Following examples demonstrates few approaches for sorting by key and sorting by value. Following examples uses Java Lambda and Stream api to sort Java HashMap instance.

Sort by Value

Map sort by value in revere order
Following code snippet sorts the map based on value in reverse order and populates a new map reverseSortedMap from the data.
//LinkedHashMap preserve the ordering of elements in which they are inserted
Map reverseSortedMap =  new LinkedHashMap();
//Use Comparator.reverseOrder() for reverse ordering
map.entrySet()
    .stream()
    .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) 
    .forEachOrdered(x -> reverseSortedMap.put(x.getKey(), x.getValue()));

Sort By Key

Map sort by key in revere order
Following code snippet sorts the map based on the keys in reverse order.
//LinkedHashMap preserve the ordering of elements in which they are inserted
Map reverseSortedMapByKey =  new LinkedHashMap();;
//Use Comparator.reverseOrder() for reverse ordering
map.entrySet()
    .stream()
    .sorted(Map.Entry.comparingByKey(Comparator.reverseOrder())) 
    .forEachOrdered(x -> reverseSortedMapByKey.put(x.getKey(), x.getValue()));
 

System.out.println("Sorted by Value Map : " + reverseSortedMapByKey);

Full Example

MapSortJava
public class MapSortJava {

  public static void main(String[] args) {

    Map<Integer, String> map = new HashMap<Integer, String>();
    map.put(101, "Tokyo");
    map.put(3, "New York");
    map.put(2, "San Francisco");
    map.put(14, "Los Angels");
    map.put(5, "Austin");

    System.out.println("Unsorted Map : " + map);
    
   // LinkedHashMap preserve the ordering of elements in which they are inserted
    Map<Integer, String> reverseSortedMap = new LinkedHashMap<Integer, String>();;
    // Use Comparator.reverseOrder() for reverse ordering
    map.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
        .forEachOrdered(x -> reverseSortedMap.put(x.getKey(), x.getValue()));
    System.out.println("Sorted by Value Map : " + reverseSortedMap);


    // LinkedHashMap preserve the ordering of elements in which they are inserted
    Map<Integer, String> reverseSortedMapByKey = new LinkedHashMap<Integer, String>();;
    // Use Comparator.reverseOrder() for reverse ordering
    map.entrySet().stream().sorted(Map.Entry.comparingByKey(Comparator.reverseOrder()))
        .forEachOrdered(x -> reverseSortedMapByKey.put(x.getKey(), x.getValue()));
    System.out.println("Sorted by Value Map : " + reverseSortedMapByKey);

  }
}
Output
Unsorted Map : 2 ==> San Francisco 3 ==> New York 101 ==> Tokyo 5 ==> Austin 14 ==> Los Angels Sorted by Value Map (Reverse) : 2 ==> San Francisco 3 ==> New York 101 ==> Tokyo 5 ==> Austin 14 ==> Los Angels Sorted by Value Map (Reverse) : 101 ==> Tokyo 14 ==> Los Angels 5 ==> Austin 3 ==> New York 2 ==> San Francisco
Summary of steps
  • Get the entry set by calling the Map.entrySet().
  • Get the entry set stream by calling stream() method.
  • Use the Map.Entry.comparingByValue() or Map.Entry.comparingByKey().
  • Call the sorted method with a Comparator.
  • call the terminal operation forEachOrdered to store the each entries in the new Map.

How to use custom comparator for TreeMap

This article covers some of the aspects of sorting Map in Java. Java Map comes in different flavors, like HashMap, TreeMap, LinkedHashMap etc. TreeMap for instance will sort the data based on the keys. We can also pass a custom Comparator to sort based on the keys as per the custom sort algorithm.
TreeMap sorts based on natural ordering of the keys if no custom Comparator is provided. To use a custom Comparator we need to pass the Comparator in the TreeMap Constructor.

Simple TreeMap

TreeMap without any Comparator
Following code creates a TreeMap and adds some data to it. After that we print the TreeMap. As No comparator is specified, the data is sorted by natural ordering of the keys.
TreeMap map = new TreeMap();
    map.put(1, "A");
    map.put(3, "C");
    map.put(2, "B");
    map.put(4, "D");
    map.put(5, "E");


    System.out.println("\nPrint Map ");
    map.forEach((a, b) -> {
      printKeyVal(a,b);
    });;
Print Map Key: 1 Value: A Key: 2 Value: B Key: 3 Value: C Key: 4 Value: D Key: 5 Value: E

TreeMap with Comparator

TreeMap with reverse order Comparator
In this case we are passing a built in Comparator to sort the keys reversely. From the result we can see that the data is sorted in reverse order of the keys.
System.out.println("\nPrint Map with Reverse Comparator");
TreeMap map2 = new TreeMap(Collections.reverseOrder());
    map2.putAll(map);
    map2.forEach((a, b) -> {
      printKeyVal(a, b);
 });
Print Map with Reverse Comparator Key: 5 Value: E Key: 4 Value: D Key: 3 Value: C Key: 2 Value: B Key: 1 Value: A

Custom Comparator

Following examples shows implementation of two custom Comparator, one with inner class second one with java 8 Lambda. Both the Comparator works exactly same, that is it sorts the data in reverse order.
With Inner Classes
Custom Comparator with Inner Class to sort the keys in reverse order.
Comparator orderByKeyDesc = new Comparator() {
@Override
 public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);
      }
};
Lambda Comparator.
Java 8 Lambda to implement Comparator
Comparator orderByKeyDesc2 = (Integer o1, Integer o2) -> o2.compareTo(o1);

Full Example

TreeMapCustomComparator
Full example with Custom Comparator that is passed to the TreeMap.
public class TreeMapCustomComparator {

  public static void main(String[] args) {

    TreeMap map = new TreeMap();
    map.put(1, "A");
    map.put(3, "C");
    map.put(2, "B");
    map.put(4, "D");
    map.put(5, "E");


    System.out.println("\nPrint Map ");
    map.forEach((a, b) -> {
      printKeyVal(a, b);
    });;

   
    System.out.println("\nPrint Map with Reverse Comparator");
    TreeMap map2 = new TreeMap(Collections.reverseOrder());
    map2.putAll(map);
    map2.forEach((a, b) -> {
      printKeyVal(a, b);
    });


    //Custom Comparator with Inner Class
    Comparator orderByKeyDesc = new Comparator() {
      @Override
      public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);
      }
    };

    //Custom Comparator with Lambda
    Comparator orderByKeyDesc2 = (Integer o1, Integer o2) -> o2.compareTo(o1);

    System.out.println("\nPrint Map with Custom Reverse Comparator Java Lambda");
    TreeMap map3 = new TreeMap(orderByKeyDesc2);
    map3.putAll(map);

    map3.forEach((a, b) -> {
      printKeyVal(a, b);
    });

  }

  /* Utility method to print key value pairs nicely */
  private static void printKeyVal(Object a, Object b) {
    System.out.print("Key: " + a + "  Value: " + b + "     ");
  }


}
Print Map Key: 1 Value: A Key: 2 Value: B Key: 3 Value: C Key: 4 Value: D Key: 5 Value: E Print Map with Reverse Comparator Key: 5 Value: E Key: 4 Value: D Key: 3 Value: C Key: 2 Value: B Key: 1 Value: A Print Map with Custom Reverse Comparator Java Lambda Key: 5 Value: E Key: 4 Value: D Key: 3 Value: C Key: 2 Value: B Key: 1 Value: A

Convert ByteBuffer to String and viceversa

ByteBuffer Class is part of Java NIO. It extends java.nio.Buffer and used to handle data read and write to NIO Channels. This article demonstrates few ways to handle conversion between ByteBuffer to String and converting String to ByteBuffer object.
Convert String to ByteBuffer
Following code converts the String data to ByteBuffer object using Charset.forName. We need to specify the appropriate character set .
String data = "this is my string";
ByteBuffer buff = Charset.forName("UTF-8").encode(data);
Convert ByteBuffer to String
Following code sample shows two methods, both can be used to convert ByteBuffer object back to a String. We need to specify the same character set, that was used to encode, otherwise we might get a different String.
public static String getData (ByteBuffer buff) {
    return new String(buff.array(), StandardCharsets.UTF_8 );
  }
  
public static String getData2 (ByteBuffer buff) {
    return StandardCharsets.UTF_8.decode(buff).toString();
}

Java File Channel

FileChannel is available as a part of Java NIO (New IO).Java NIO (New IO) is an alternative to the standard Java IO API's. Java NIO offers a different way of working with IO than the standard IO API's. File channels are safe for use by multiple concurrent threads. This article demonstrate with working example of how to write and read file using FileChannel.
Advantages of File Channels
  • File channels are safe for use by multiple concurrent threads.
  • Reading and writing at a specific position in a file.
  • transfer file data from one channel to another at a faster rate.
  • lock a section of a file to restrict access by other threads.

Example of Reading and Writing File

Following example demonstrate how to read and write file using FileChannel.
JavaFileChannelHelper
package bootng.com.files;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

public class JavaFileChannelHelper {
  
  public static void main(String[] args) {
    JavaFileChannelHelper example = new JavaFileChannelHelper();
    try {
      example.write("samplefile.txt","This is it");
      String content = example.read("samplefile.txt");
      System.out.println(content + "  " );
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  public String read(String source) throws IOException {
    StringBuilder content = new StringBuilder("");
    RandomAccessFile file = new RandomAccessFile(source, "r");
    FileChannel channel = file.getChannel();
    ByteBuffer buf = ByteBuffer.allocate(48);
    while (channel.read(buf) > 0) {
      content.append(new String(buf.array(), StandardCharsets.UTF_8 ));
    }
     System.out.println("Read success!");
    return content.toString();
  }
  
  public void write(String source, String content) throws IOException {
    RandomAccessFile file = new RandomAccessFile(source, "rw");
    FileChannel channel = file.getChannel();
    ByteBuffer buff = ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8));
    channel.write(buff);
    System.out.println("Write success!");
      }
}
Notes
  • Channels read and write buffers containing the content.
  • Channels are bi directional, that is we can both write and read in a channel.

Photo by Viktor Talashuk on Unsplash

How to list all files in a directory in java

Say we want get names of all the files under a directory and sub directories. Basically we need to able to look under each sub directory and if it has any files add to the result. This sounds like a problem that can be solved recursively.
Get Files
List the file names by calling listFiles on the root path, and then recursively calling it if we get any directory.
public List getFiles (String path) {
     List result = new ArrayList();
     File root = new File (path);
     File[] filesNDirectories = root.listFiles();
     
     for (var file: filesNDirectories) {
       if (file.isFile()) {
         result.add(file.getName());
       } 
       else if (file.isDirectory()) {
         List subResult = getFiles(file.getPath());
         result.addAll(subResult);
       }
     }
     return result; 
  }
Files.walk
Files.walk returns a stream containing
public List getFiles(String path) throws IOException {
    Stream walk = Files.walk(Paths.get(path));
    List result = walk.filter(Files::isRegularFile).map(x -> x.getFileName().toString())
        .collect(Collectors.toList());

    result.forEach(System.out::println);
    walk.close();

    return result;
  }

Example


Java Files.walk examples

Java File.walk method is available in java 8. It is very useful to traverse the the content of a file tree. Walks goes in a depth first fashion and return a Stream object. We can then work with the stream to get the required result. Like we can filter only directories, find files matching some name etc. This article covers three example of using File.walk to list all the directories, files and both.
List all the folders
List all the directories inside the location specified by path.
public List getFolders(String path) throws IOException {
    Stream walk = Files.walk(Paths.get(path));
    List result = walk.filter(Files::isDirectory).map(x -> x.getFileName().toString())
        .collect(Collectors.toList());
    result.forEach(System.out::println);
    walk.close();
    return result;
  }
List all files
Similar to above method, but the filter condition is different. Here we are filtering only regular fiels with Files::isRegularFile.
public List getFiles(String path) throws IOException {
    Stream walk = Files.walk(Paths.get(path));
    List result = walk.filter(Files::isRegularFile).map(x -> x.getFileName().toString())
        .collect(Collectors.toList());

    result.forEach(System.out::println);
    walk.close();

    return result;
  }
Files and Directories
Following method will list all the directories: with files inside it. It is similar to linux command ls *.
public List getFoldersAndFiles(String path) throws IOException {
    Stream walk = Files.walk(Paths.get(path));
    List result = walk.map(x -> {
      if (Files.isDirectory(x)) {
        return x.getFileName().toString() + ":";
      } else
        return x.getFileName().toString();
    }).collect(Collectors.toList());

    result.forEach(System.out::println);
    walk.close();

    return result;
  }

References


Table of Content

Iterating Java Map

Java Map is an object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. If we want to get the object stored at a particular key we can do so using the the get method.
But if we want to traverse all the objects, then we have different ways of doing the same.

Different Options

In Java we have the following four options to iterate over java map.
Map Iterator
Classic way of using the Iterator of the keySet.
//Iterator
Map map = new TreeMap();
System.out.println("Print using Iterator");
Iterator it = map.keySet().iterator();

while (it.hasNext()) {
  Integer k = it.next();
  String  v = map.get(k);
  System.out.print( "Key=" + k + " Value=" + v + "  ");
}
Java 8 forEach
We can use the forEach introduced in Java 8 to iterate over key and value.
//forFeach
Map map = new TreeMap();
System.out.println("\nPrint using Java 8 forEach");
map.forEach((k,v) -> System.out.print("Key=" + k + " Value=" + v + "  "));
For loop over the keyset
Using for loop over the key set, and then getting the respective value from the map.
//For loop over the keySet
Map map = new TreeMap();
System.out.println("\nPrint using for loop");
for (Integer k : map.keySet()) {
  String  v = map.get(k);
  System.out.print("Key=" + k + " Value=" + v + "  ");
}

Full Example

Full Java Class with all the above options to iterate over map.
MapIterationExample
package bootng.com.learn.collections;

public class MapIterationExample {
  public static void main(String[] args) {
    Map<Integer, String> map = new TreeMap<Integer, String>();
    map.put(1, "A");
    map.put(3, "C");
    map.put(2, "B");
    map.put(4, "D");
    map.put(5, "E");

    // Iterator
    System.out.println("Print using Iterator");
    Iterator<Integer> it = map.keySet().iterator();

    while (it.hasNext()) {
      Integer k = it.next();
      String v = map.get(k);
      System.out.print("Key=" + k + " Value=" + v + "  ");
    }

    // forFeach
    System.out.println("\nPrint using Java 8 forEach");
    map.forEach((k, v) -> System.out.print("Key=" + k + " Value=" + v + "  "));

    // For loop over the keySet
    System.out.println("\nPrint using for loop");
    for (Integer k : map.keySet()) {
      String v = map.get(k);
      System.out.print("Key=" + k + " Value=" + v + "  ");
    }

    // Stream
    System.out.println("\nPrint using stream");
    map.keySet().stream().forEach(key -> {
      System.out.print("Key=" + key + " Value=" + map.get(key) + "  ");
    });    
  }
}
Output
Print using Iterator
Key=1 Value=A  Key=2 Value=B  Key=3 Value=C  Key=4 Value=D  Key=5 Value=E  
Print using Java 8 forEach
Key=1 Value=A  Key=2 Value=B  Key=3 Value=C  Key=4 Value=D  Key=5 Value=E  
Print using for loop
Key=1 Value=A  Key=2 Value=B  Key=3 Value=C  Key=4 Value=D  Key=5 Value=E  
Print using stream
Key=1 Value=A  Key=2 Value=B  Key=3 Value=C  Key=4 Value=D  Key=5 Value=E  

BigQuery and Public Dataset

BigQuery is Serverless, highly scalable cloud data warehouse solution. BigQuery is extremely fast, and can process terabytes of data in seconds and petabytes of data in minutes. BigQuery also comes with some really useful public data sets which can be used to learn about the BigQuery or associated tools.

SandBox

BigQuery Sandbox is the quickest way to start with BigQuery without any payment. See the official Google Cloud blog post “Query without a credit card: introducing BigQuery sandbox” for more information. Here we are going to focus on you getting started and querying as quickly as possible.

BigQuery UI

BigQuery UI is the main Web interface to interact with BigQuery. If we are already signed up for Google Cloud Account then BigQuery should be already available. If not then we can take advantage of the BigQuery Sandbox. BigQuery sandbox is a BigQuery-specific initiative to provide easy access to BigQuery without needing to grab your credit card.

COVID-19 Dataset Fee Usages

As a special case, this BigQuery dataset is free to query even outside the free tier (until Sep 2020). If you join the COVID-19 data against any other datasets, the first 1 TB of querying per month of those other datasets is free and included in the sandbox program

Public Dataset

Google pays for the storage of these datasets and provides public access to the data via Google cloud project. We need to pay only for the queries that you perform on the data and that exceeds free quota. Moreover, there’s a 1TB per month free tier, making getting started super easy. Following are some of the popular public datasets to explore.

Exploring the Datasets



Preview and Query data


First Query
Query to Get cases from all regions of the world
SELECT country_region,  SUM(confirmed) as confirmed, SUM(deaths) as deaths, SUM(recovered) as recovered, SUM(active) as active FROM `bigquery-public-data.covid19_jhu_csse.summary` group by country_region order by confirmed desc LIMIT 1000
Query Result

References


Cloud SQL

Creating Cloud SQL Instance

Cloud SQL is a fully-managed database service that makes it easy to set up and use relational databases on Google Cloud Platform. This article covers the steps that will help in creating a MySQL 5 instance on Google Cloud Platform. It walks through the creation of a MySQL Instance, setting root password creating schema and a table.
We will create an MySQL instance named currencyproj and once it is created we will create a database and one table.
Prepare Instance
Create environment variables that will be used later in the lab for your project ID and the storage bucket that will contain your data
export PROJECT_ID=$(gcloud info --format='value(config.project)')
export BUCKET=${PROJECT_ID}-ml
Create a Cloud SQL instance
Create currencyproj MySQL instance : currencyproj
gcloud sql instances create currencyproj \
    --tier=db-n1-standard-1 --activation-policy=ALWAYS
Set Root Passord
following sets the password for currencyproj instance.
gcloud sql users set-password root --host % --instance currencyproj \
 --password Passw0rd
Create an environment variable with the IP address of the Cloud Shell
export ADDRESS=$(wget -qO - http://ipecho.net/plain)/32
White list the Cloud Shell
Whitelist the Cloud Shell instance for management access to your SQL instance
gcloud sql instances patch currencyproj --authorized-networks $ADDRESS
Get Cloud SQL IP Address
Get the IP address of your Cloud SQL instance by running
MYSQLIP=$(gcloud sql instances describe \
currencyproj --format="value(ipAddresses.ipAddress)")
Create the currency name table by logging into the mysql command line interface
mysql --host=$MYSQLIP --user=root \
      --password --verbose
Create DB and Table
After logging into the MySQL console, create database and table
create database if not exists currency_db;
use currency_db;


CREATE TABLE IF NOT EXISTS CURRENCY_NAME
(
  id varchar(255),
  title varchar(255),
  symbol varchar(255),
  PRIMARY KEY (ID)
);


Summary

We are able to create an instance of MySQL and then created a database and table.
Docker is very popular for developing applications and sharing them faster. Docker has number of commands to achieve tasks.
In this article we will follow some common docker commands that we need everyday. For reference
we are using Docker 19.03.5

Docker arranges the commands in two groups Management Commands and sub commands. This is true for docker 1.13 and later. With this kind of organization it is easy to navigate the commands based on the functional area of docker.






  1. docker - version
  2. docker pull
  3. docker run
  4. docker ps
  5. docker exec
  6. docker stop
  7. docker kill
  8. docker 

Docker Image

To manage docker image these image related commands are used.

List Image (image ls)

To see list of docker images run the following command
docker image ls 

Build Image (image build)

To build an image run the following command 
docker image ls

Save Image (image save)

Save image is used to save an Image not a container. For example to save the docker image bootng-sample to test.tar run the following command
docker image save bootng-sample > test.tar

Tag Image (Image tag)

Docker tag is an alias for an image which is more readable and meaningful than the ID of the image.
To tag an image we can run the following command
docker image tag 0e4d2ffd5f62 "docker_tag_example"

Remove Image

To remove a docker image execute the rm command and specify the id or tag of the image.
For example to remove the image with tag docker_tag_example run the following commad
docker image rm docker_tag_example

Docker Container

Docker container is the running instance of an image. Following commands are commonly used to work with docker containers.


List Containers

It supports many flags like showing all containers, listing only the container ids, filtering containers, showing latest containers etc.
To see containers running or stopped use the following command.
docker container ls

By default it shows only the running containers to see all the containers use the flag --all
docker container ls --all

To list only the container ids use flag --quiet or -q
docker container ls -q

To list only latest container use the flag --latest or -l
docker container ls --latest

Stop Containers

To stop one of more containers use the following command
docker container stop CONTAINERID

Start Containers

To stop one of more containers use the following command
docker container stop CONTAINERID

Rename Container

To rename container use the following command
docker container rename OLD_NAME NEW_NAME

See Logs 

To see logs from the container use the following command

docker container logs CONTAINERID




What is user agent stylesheet

Every browser comes with some default style information. Which is used in case a particular webpage does not contains any style defined. For example if a web page contains standard HTML tags like H1, Button, Input etc. but does not contains any css along side the HTML. In that case the browser still needs to render the HTML tags to the end user. To facilitate this each browser like Chrome, Safari, Firefox etc comes with a default style sheet.
The default style sheet provided by the browser it self is called user agent stylesheet. Following screenshot shows an example of css for H1 provided by user agent style sheet on Chrome.


To override the browser specific defaults we can employ multiple techniques such as Reseting the user agent CSS or Normalize the user agent CSS. CSS Reset tries to remove all the default browser supplied styles where as Normalize CSS tries to standardize the different defaults across all browsers
Key points
  • Different browsers set different default CSS rules.
  • Default CSS links for some of the browsers are given bellow.
  • We can reset the default browser specific style.
  • We can also normalize the browser specif style to have standardize looks across all browsers.

User Agent Style References