Java Coding Problems
Improve your Java Programming skills by solving real-world coding challenges
Anghel Leonard
- 816 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
Java Coding Problems
Improve your Java Programming skills by solving real-world coding challenges
Anghel Leonard
About This Book
Develop your coding skills by exploring Java concepts and techniques such as Strings, Objects and Types, Data Structures and Algorithms, Concurrency, and Functional programming
Key Features
- Solve Java programming challenges and get interview-ready by using the power of modern Java 11
- Test your Java skills using language features, algorithms, data structures, and design patterns
- Explore areas such as web development, mobile development, and GUI programming
Book Description
The super-fast evolution of the JDK between versions 8 and 12 has increased the learning curve of modern Java, therefore has increased the time needed for placing developers in the Plateau of Productivity. Its new features and concepts can be adopted to solve a variety of modern-day problems. This book enables you to adopt an objective approach to common problems by explaining the correct practices and decisions with respect to complexity, performance, readability, and more.
Java Coding Problems will help you complete your daily tasks and meet deadlines. You can count on the 300+ applications containing 1, 000+ examples in this book to cover the common and fundamental areas of interest: strings, numbers, arrays, collections, data structures, date and time, immutability, type inference, Optional, Java I/O, Java Reflection, functional programming, concurrency and the HTTP Client API. Put your skills on steroids with problems that have been carefully crafted to highlight and cover the core knowledge that is accessed in daily work. In other words (no matter if your task is easy, medium or complex) having this knowledge under your tool belt is a must, not an option.
By the end of this book, you will have gained a strong understanding of Java concepts and have the confidence to develop and choose the right solutions to your problems.
What you will learn
- Adopt the latest JDK 11 and JDK 12 features in your applications
- Solve cutting-edge problems relating to collections and data structures
- Get to grips with functional-style programming using lambdas
- Perform asynchronous communication and parallel data processing
- Solve strings and number problems using the latest Java APIs
- Become familiar with different aspects of object immutability in Java
- Implement the correct practices and clean code techniques
Who this book is for
If you are a Java developer who wants to level-up by solving real-world problems, then this book is for you. Working knowledge of Java is required to get the most out of this book.
Frequently asked questions
Information
Arrays, Collections, and Data Structures
Problems
- Sorting an array: Write several programs that exemplify different sorting algorithms for arrays. Also, write a program for shuffling arrays.
- Finding an element in an array: Write several programs that exemplify how to find the given element (primitive and object) in a given array. Find the index and/or simply check whether the value is in the array.
- Checking whether two arrays are equal or mismatches: Write a program that checks whether the two given arrays are equals or whether there is a mismatch.
- Comparing two arrays lexicographically: Write a program that compares the given arrays lexicographically.
- Creating a stream from an array: Write a program that creates a stream from the given array.
- Minimum, maximum, and average of an array: Write a program that computes the maximum, minimum, and average of the given array.
- Reversing an array: Write a program that reverses the given array.
- Filling and setting an array: Write several examples for filling up an array and setting all elements based on a generator function to compute each element.
- Next Greater Element (NGE): Write a program that returns the NGE for each element of an array.
- Changing array size: Write a program that adds an element to an array by increasing its size by one. In addition, write a program that increases the size of an array with the given length.
- Creating unmodifiable/immutable collections: Write several examples that create unmodifiable and immutable collections.
- Mapping a default value: Write a program that gets a value from Map or a default value.
- Computing whether absent/present in a Map: Write a program that computes the value of an absent key or a new value of a present key.
- Removal from a Map: Write a program that removes from a Map by means of the given key.
- Replacing entries from a Map: Write a program that replaces the given entries from a Map.
- Comparing two maps: Write a program that compares two maps.
- Merging two maps: Write a program that merges two given maps.
- Copying HashMap: Write a program that performs a shallow and deep copy of HashMap.
- Sorting a Map: Write a program that sorts a Map.
- Removing all elements of a collection that match a predicate: Write a program that removes all elements of a collection that match the given predicate.
- Converting a collection into an array: Write a program that converts a collection into an array.
- Filtering a collection by List: Write several solutions for filtering a collection by a List. Reveal the best way of doing this.
- Replacing elements of a List: Write a program that replaces each element of a List with the result of applying a given operator to it.
- Thread-safe collections, stacks, and queues: Write several programs that exemplify the usage of Java thread-safe collections.
- Breadth-first search (BFS): Write a program that implements the BFS algorithm.
- Trie: Write a program that implements a Trie data structure.
- Tuple: Write a program that implements a Tuple data structure.
- Union Find: Write a program that implements the Union Find algorithm.
- Fenwick Tree or Binary Indexed Tree: Write a program that implements the Fenwick Tree algorithm.
- Bloom filter: Write a program that implements the Bloom filter algorithm.
Solutions
99. Sorting an array
JDK built-in solutions
int[] integers = new int[]{...};
Arrays.sort(integers);
public class Melon {
private final String type;
private final int weight;
public Melon(String type, int weight) {
this.type = type;
this.weight = weight;
}
// getters omitted for brevity
}
Melon[] melons = new Melon[] { ... };
Arrays.sort(melons, new Comparator<Melon>() {
@Override
public int compare(Melon melon1, Melon melon2) {
return Integer.compare(melon1.getWeight(), melon2.getWeight());
}
});
Arrays.sort(melons, (Melon melon1, Melon melon2)
-> Integer.compare(melon1.getWeight(), melon2.getWeight()));