Blog

In this short Swift code example, we will use the UIAlertController to create an alert dialog message with only one “OK” button. The code example below will demonstrate how to: Create UIAlertController with a Title and a Message to display, Add a UIAlertAction with the OK label to UIAlertController, Handle UIAlertAction to know when the user taps…

Read More Create UIAlertController with OK Button in Swift

private static void sumWithRegularStream() { for (int i = 0; i < 10; i++) { IntStream.rangeClosed(1, 100000).sum(); } } private static void sumWithParallelStream() { for (int i = 0; i < 10; i++) { IntStream.rangeClosed(1, 100000).parallel().sum(); } } class Test { public static void main(String[] args) { long regularStreamStartTime = System.currentTimeMillis(); sumWithRegularStream(); System.out.println(“Regular Stream execution…

Read More Parallel Streams Performance Testing

class Test { public static void main(String[] args) { System.out.println(“Normal Stream…”); IntStream integers = IntStream.rangeClosed(1, 10); integers.forEach(num -> System.out.print(num + ” “)); System.out.println(); System.out.println(“Parallel Stream…”); IntStream integers2 = IntStream.rangeClosed(1, 10); integers2.parallel().forEach(num -> System.out.print(num + ” “)); } } Output: Normal Stream… 1 2 3 4 5 6 7 8 9 10 Parallel Stream… 7 6…

Read More Introduction to Parallel Streams API in Java

The following Swift code example demonstrates how to create a UIWebView programmatically and how to make it render HTML code. To break it into smaller steps, the below code snippet demonstrates how to: Create UIWebView programmatically, Implement UIWebViewDelegate functions webViewDidStartLoad and webViewDidFinishLoad, Make UIWebView render HTML Code. Render HTML in UIWebView import UIKit import WebKit class…

Read More Create UIWebView Programmatically and Render HTML Code

This tutorial will teach you how to create your own Spring Cloud Config Server and how to configure a Spring Boot Application to be a Spring Cloud Config Client. Spring Cloud Config Server Spring Cloud Config Server is a Spring Boot application. So to create our own Spring Cloud Config Server, we must first create…

Read More Spring Cloud Config Server and Config Client

class Test { public static void main(String[] args) { Optional<String> stringOptional = Optional.ofNullable(“value1”); stringOptional.map(value -> value.concat(“234”)) .ifPresent(System.out::println); } } Output: value1234 2. Using the flatMap() method class Student { private String firstName; private String lastName; private int grade; private Optional<String> address; public Student(String firstName, String lastName, int grade) { this.firstName = firstName; this.lastName = lastName;…

Read More Optional – map() and flatMap() operations

public Optional<T> filter(Predicale<T> predicate) class Test { public static void main(String[] args) { Optional<String> stringOptional = Optional.ofNullable(“alegru coding”); stringOptional.filter(str -> str.length() > 10) .map(String::toUpperCase) .ifPresent(System.out::println); } } Output: ALEGRU CODING class Test { public static void main(String[] args) { Optional<User> userOptional = Optional.ofNullable(new User(“John”, “john123”, “premium”, “5th Avenue”)); userOptional.filter(user -> user.getMembershipType().equals(“premium”)) .ifPresent(System.out::println); } } class…

Read More Optional – filter() operation

In this lesson, we will cover the three useful methods from the Optional class: public T orElse(T other) – It returns the value if present, otherwise returns the other. public T orElseGet(Supplier<? extends T> other) – It returns the value if present. Otherwise, invoke other and return the result of that invocation. public <X extends…

Read More Optional – orElse(), orElseGet() and orElseThrow() methods

We have the isPresent() and ifPresent() methods to help us detect if Optional contains some value or not. Optional isPresent() method The isPresent() method returns true if there is a value present in the Optional, otherwise false.Using it, we can avoid getting an exception when trying to get the value from an empty Optional. So…

Read More Optional – ifPresent() and isPresent() methods

class Test { public static void main(String[] args) { Optional optional = Optional.empty(); // creates an empty Optional if (optional.isEmpty()) { System.out.println(“It is an empty Optional.”); } else { System.out.println(“Optional is not empty.”); } } } Output: It is an empty Optional. 2. Optional.of(T value) method class Test { public static void main(String[] args) {…

Read More Introduction to Optional class in Java

public static <T, K> Collector<T, ?, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier) class Test { public static void main(String[] args) { List<String> cities = new ArrayList<>(Arrays.asList(“Paris”, “Bern”, “London”, “Tokyo”, “Boston”)); Map<Integer, List<String>> resultMap = cities.stream() .collect(Collectors.groupingBy(String::length)); System.out.println(resultMap); } } Output: {4=[Bern], 5=[Paris, Tokyo], 6=[London, Boston]} class Test { public static void main(String[]…

Read More Streams – groupingBy() operation

class Test { public static void main(String[] args) { List<String> words = new ArrayList<>(Arrays.asList(“Hello”, “World”, “of”, “Java!”)); String result = words.stream().collect(Collectors.joining()); System.out.println(result); String resultWithDelimiters = words.stream().collect(Collectors.joining(“-“)); // delimiter System.out.println(resultWithDelimiters); String resultWithDelimitersAndPrefixAndSuffix = words.stream().collect(Collectors.joining(“-“, “(“, “)”)); // delimiter, prefix, suffix System.out.println(resultWithDelimitersAndPrefixAndSuffix); } } Output: HelloWorldofJava! Hello-World-of-Java! (Hello-World-of-Java!) class Test { public static void main(String[] args) {…

Read More Streams – collect() operation

DoubleStream.of(2.4); DoubleStream.of(7.1, 10.5, 12.7); DoubleStream.iterate(0, i -> i + 2).limit(10); DoubleStream.generate(() -> Math.random() * 5).limit(10); class Test { public static void main(String[] args) { DoubleStream doubleStream = DoubleStream.of(1.2, 3.1, 7.2, 5.3, 9.8); doubleStream.forEach(item -> System.out.print(item + ” “)); } } Output: 1.2 3.1 7.2 5.3 9.8 class Test { public static void main(String[] args) {…

Read More Streams – DoubleStream class