Is it possible that TreeSet equals HashSet but not HashSet equals TreeSet

I had a interview today and the person taking my interview puzzled me with his statement asking if it possible that TreeSet equals HashSet but not HashSet equals TreeSet. I said “no” but …

Sort List of Strings by Characters In Java

I am trying to write a program to order a list of strings by most least frequent characters within the list. For example, if the list was [apple, orange, banana] the letter frequency within the list …

How to create a Queue which accepts any Datatype?

How to insert both String’s and Integers in the same Queue ? Please look at the below program, I have created two objects (q, q1). Can we insert strings and integers in one Queue? import java.util.*;…

Java Array List – Facing issue while taking values from user, but unknowingly program terminating

I am trying to take values from user in ArrayList, I can take input for the size of ArrayList, But, when I am trying to add elements, the program is being terminating. Here is my code import java.io.*;…

understanding comparator in Collections.max

Can somebody please explain what the following lines of code mean?. I had a hard time understanding the Comparator part.I tried googling but all of them were too complex for me to understand. Could …

Transform List into Map using only two keys and odd or even list indexes as values – Java 8 Stream

I would like to transform list into map using as key values only two string values. Then as values just list of strings containing elements from odd or even index positions from input list. Here is …

List.of() or Collections.emptyList()

As an special case of List.of(…) or Collections.unmodifiableList() – what is the preferred Java 9 way of pointing to an empty and immutable list? Keep writing Collections.emptyList(); or switch …

How do I keep the iteration order of a List when using Collections.toMap() on a stream?

I am creating a Map from a List as follows: List strings = Arrays.asList(“a”, “bb”, “ccc”); Map map = strings.stream() .collect(Collectors.toMap(Function….

What is difference between Collection.stream().forEach() and Collection.forEach()?

I understand that with .stream(), I can use chain operations like .filter() or use parallel stream. But what is difference between them if I need to execute small operations (for example, printing the …

How to copy a java.util.List into another java.util.List

I have a List that is populated from a Web Service. I want to copy/clone the contents of that list into an empty list of the same type. A Google search for copying a list suggested me …