Skip to main content

Questions tagged [java]

Java (not to be confused with JavaScript) is a class-based, object-oriented, strongly typed, reflective language and run-time environment (JRE). Java programs are compiled to bytecode and run in a virtual machine (JVM) enabling a "write once, run anywhere" (WORA) methodology.

5 votes
4 answers
2k views

Duplicate words in a text

Here is a simplified implementation to obtain the duplicate words in a text using lambda expressions. ...
2 votes
2 answers
4k views

Convert string to mixed case

This method converts a string to mixed case, id est mixedCase("hello, world!") -> "HeLlO, wOrLd!". ...
3 votes
1 answer
218 views

Leetcode: Number of Islands - BFS (Queue vs Recursion)

I was playing around with leetcode's Number of Islands. As per the challenge's description: Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the ...
4 votes
4 answers
1k views

Stack - implementation in Java

I implemented a stack data structure without using any libraries or Java API code. ...
6 votes
3 answers
9k views

Printing longest sequence of zeroes

I am doing a coding exercise in codility and I came across this question: A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ...
5 votes
3 answers
25k views

Finding the longest string and its length using Java streams

Java 8 streams are my new golden hammer that I try to use as often as possible with often enormous gains in brevity and readability. Getting multiple return values, such as a maximum value and its ...
5 votes
2 answers
3k views

Java 8 Stream to produce all permutations of an array using recursion

I want to write a class that returns a Stream of permutations of an int[]. ...
3 votes
3 answers
5k views

Running multiple producer and single consumer in a multithread environment

I have a below class in which add method will be called by multiple threads to populate messageByChannelReference concurrent ...
3 votes
1 answer
95 views

In Java, replace for loop with condition with lambdas [closed]

I want to replace a for loop with a break/return condition inside with a lambda. I think I have a good replacement, but I want ...
3 votes
4 answers
7k views

Remove adjacent duplicate characters

Introduction This problem is essentially equivalent to this HackerRank problem. The task is to remove all duplicate characters that are adjacent to each other in a given ...
63 votes
5 answers
285k views

Removing elements on a List while iterating through it

I needed a way to remove elements on a List while iterating through it. Supposedly something like this (illegal code): ...
-2 votes
1 answer
104 views

How can I reduce my code in time complexity?

My friends in the same project told me that I needed to improve this code's complexty, so I tried but couldn't figure out how to do more.. They also said that I needed to change my coding style and ...
5 votes
0 answers
95 views

Dial's heap in Java for integer priority queues

(The entire project is here.) Intro I have this priority queue data structure for non-negative integer priority keys. I recall that it is called Dial's heap. Code Implementation ...
45 votes
6 answers
175k views

Simple Example of an Iterable and an Iterator in Java

I was a TA for a first-year Java programming course this year. As a very simple example of iterables/iterators, I wrote the following code for the students. I am curious if any styling or other ...
21 votes
4 answers
18k views

Filtering out empty Optionals

I want to convert my list of optional strings to a list of strings by getting rid of the empty Optionals. Is there a shorter version for achieving this than the ...

15 30 50 per page