Java – How to declare and initialize an Array

Few Java examples to declare, initialize and manipulate Array in Java 1. Declares Array 1.1 For primitive types. ArrayExample1.java package com.mkyong; import java.util.Arrays; public class ArrayExample1 { public static void main(String[] args) { //declares an array of integers int[] num1 = new int[5]; int[] num2 = {1, 2, 3, 4, 5}; int[] num3 = new …

Read more

Java 8 – How to sort list with stream.sorted()

Few examples to show you how to sort a List with stream.sorted() 1. List 1.1 Sort a List with Comparator.naturalOrder() package com.mkyong.sorted; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class StreamApplication { public static void main(String[] args) { List<String> list = Arrays.asList("9", "A", "Z", "1", "B", "Y", "4", "a", "c"); /* List<String> sortedList = list.stream() …

Read more

Java Selection sort example

Selection sort is an in-place comparison sort. It loops and find the first smallest value, swaps it with the first element; loop and find the second smallest value again, swaps it with the second element, repeats third, fourth, fifth smallest values and swaps it, until everything is in correct order. P.S Selection sort is inefficient …

Read more

Java Bubble sort example

Bubble sort is the simplest sorting algorithm, it compares the first two elements, if the first is greater than the second, swaps them, continues doing (compares and swaps) for the next pair of adjacent elements. It then starts again with the first two elements, compares, swaps until no more swaps are required. 1. Explanation #unsorted …

Read more

Java 8 – How to sort a Map

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

MongoDB – group, count and sort example

Some MongoDB examples to show you how to perform group by, count and sort query. 1. Test Data A whois_range collection, containing many records. > db.whois_range.find(); { "_id" : 1, "country" : "us", "source" : "ARIN", "status" : "NEW", "createdDate" : ISODate("2016-05-03T08:52:32.434Z") }, { "_id" : 2, "country" : "us", "source" : "ARIN", "status" : …

Read more

Java 8 – Convert List to Map

Few Java 8 examples to show you how to convert a List of objects into a Map, and how to handle the duplicated keys. Hosting.java package com.mkyong.java8 public class Hosting { private int Id; private String name; private long websites; public Hosting(int id, String name, long websites) { Id = id; this.name = name; this.websites …

Read more

How to get the environment variables in Java

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

MongoDB – Aggregate and Group example

In this tutorial, we will show you how to use MongoDB aggregate function to group documents (data). 1. Test Data Data in JSON format, shows the hosting provider for website. website.json { “_id” : 1, “domainName” : “test1.com”, “hosting” : “hostgator.com” } { “_id” : 2, “domainName” : “test2.com”, “hosting” : “aws.amazon.com”} { “_id” : …

Read more

How to sort a Map in Java

Few Java examples to sort a Map by its keys or values. Note If you are using Java 8, refer to this article – How to use Stream APIs to sort a Map 1. Sort by Key 1.1 Uses java.util.TreeMap, it will sort the Map by keys automatically. SortByKeyExample1.java package com.mkyong.test; import java.util.HashMap; import java.util.Map; …

Read more