Quantcast
Channel: User ZhekaKozlov - Stack Overflow
Browsing index pages (52 articles)

Understanding union types

In Pascal it is possible to declare union types:AnimalType = (Dog, Cat);Animal = record name: string; case myType: AnimalType of Dog: (weight: Integer); Cat: (age: Integer);end;However, it is easy to...

View Article


Answer by ZhekaKozlov for Why _.pick(object, _.identity) in lodash returns...

_.pick(obj, _.identity) removes fields with undefined/null values in Underscore.js.If you use Lodash, pick should be replaced with pickBy:_.pickBy(obj, _.identity)Example:> _.pickBy({a:1,...

View Article


Answer by ZhekaKozlov for When was the JRE discontinued as a separate offering?

First of all, the question is incorrect. JRE was not discontinued. It was only discontinued by Oracle. For example, on Linux, JRE is still available*:$ apt-cache search openjdk-11-jreopenjdk-11-jre -...

View Article

Comment by ZhekaKozlov on Converting Array to List

Java 16+: IntStream.of(ints).boxed().toList()

View Article

Answer by ZhekaKozlov for How to zip two Java Lists

The operation you want is called zipping.You need to implement a method zip:public static <A, B> List<Map.Entry<A, B>> zip(List<A> as, List<B> bs) { Iterator<A> it1...

View Article


Answer by ZhekaKozlov for What is the difference between List.of and...

Arrays.asList returns a mutable list while the list returned by List.of is structurally immutable:List<Integer> list = Arrays.asList(1, 2, null);list.set(1, 10); // OKList<Integer> list =...

View Article

Answer by ZhekaKozlov for How to split a string, but also keep the delimiters?

There is a new method String.splitWithDelimiters() in Java 21:/** * Splits this string around matches of the given regular expression * and returns both the strings and the matching delimiters....

View Article

Comment by ZhekaKozlov on Initialization of an ArrayList in one line

This is not one-line

View Article


Answer by ZhekaKozlov for Add item to top of LinkedHashMap

There is a new method putFirst() in Java 21:conversationsMap.putFirst("test", Conversation);

View Article


How to properly close MappedByteBuffer?

This is the code I'm running:import java.io.RandomAccessFile;import java.nio.MappedByteBuffer;import java.nio.channels.FileChannel;public class Main { public static void main(String[] args) throws...

View Article

Answer by ZhekaKozlov for How can a Java program get its own process ID?

Since Java 9 there is a method Process.pid() which returns the native ID of a process:public abstract class Process { ... public long pid();}To get the process ID of the current Java process one can...

View Article

Answer by ZhekaKozlov for Retrieving a List from a java.util.stream.Stream in...

There is a new method Stream.toList() in Java 16, which accumulates the stream elements into an unmodifiable list:List<Long> targetLongList = sourceLongList .stream() .filter(l -> l > 100)...

View Article

Answer by ZhekaKozlov for Is there an easy way to convert String to...

Since Java 22:jshell> InetAddress addr = InetAddress.ofLiteral("127.0.0.1")addr ==> /127.0.0.1jshell> InetAddress addr = InetAddress.ofLiteral("::0")addr ==> /0:0:0:0:0:0:0:0jshell>...

View Article


Answer by ZhekaKozlov for Differences of Java 16's Stream.toList() and...

Here is a small table that summarizes the differences between Stream.collect(Collectors.toList()), Stream.collect(Collectors.toUnmodifiableList()) and Stream.toList():MethodGuarantees...

View Article

Answer by ZhekaKozlov for How to asynchronously call a method in Java

Java 21+:Thread.ofVirtual().start(() -> { // Some work});Or (if you don't want a virtual thread for some reason):Thread.ofPlatform().start(() -> { // Some work});

View Article


What is the difference between Cabal and Stack?

Yesterday I learnt about a new Haskell tool called Stack. At the first blush, it looks like it does much the same job as Cabal. So, what is the difference between them? Is stack a replacement for...

View Article

What is an automatic module?

Automatic modules are mentioned many times on stackoverflow but I couldn't find a complete, succinct and self-sufficient definition of an automatic module.So, what is an automatic module? Does it...

View Article


Answer by ZhekaKozlov for Getting last of LinkedHashSet

Since Java 21 LinkedHashSet has getLast() method:jshell> var set = new LinkedHashSet<>(List.of(1,2,3,4,5));set ==> [1, 2, 3, 4, 5]jshell> set.getLast();$2 ==> 5

View Article

Answer by ZhekaKozlov for How to get a reversed list view on a list in Java?

Since Java 21 there is a new interface SequencedCollection (which is a superinteface for List). It has the method reversed() which provides a reverse-ordered view of a collection:jshell> List.of(1,...

View Article

Answer by ZhekaKozlov for Switch statement with boolean is not working?

¯\(ツ)/¯switch (Boolean.hashCode(isOn)) { case 1231 -> System.out.println("its on"); case 1237 -> System.out.println("its off");}

View Article
Browsing index pages (52 articles)


Latest Images