Skip to content
Advertisement

Tag: set

How to process object with map for CSV output

I have a set of below Objects, which i need to write to CSV: Above set can have a map with two, three or four values. Output of CSV required: I started with below snippet to print out: But above is creating a two column csv with userId and behaviours printing all map object behaviours. How to achieve above type

TreeSet throws nullpointer exception when using custom comparator

I got an error msg when I trying to remove a specific element in treeset (not null). I’m not sure why this happened, and I tried to use contains to check if there has the same element in the set already which would work fine. The exception throws when calling reset method. Here is my code: Answer As @VGR mentioned:

Power set of an input set as custom collection

I have been reading the Effective Java book and I have stuck with this code I am unable to understand how this code is generating power set. Code: Output: Can someone explain how this code is generating powerset of a given set. Answer The code uses the binary representation of the index number as a map of which element of

Merge sets when two elements in common

This is the follow up of compare sets I have I want to merge the sets when there are two elements in common. For example 0,1,2 and 0,2,6 has two elements in common so merging them to form [0,1,2,6]. Again [0,1,2,6] and [2,6,7] has 2 and 6 common. so merging them and getting [0,1,2,6,7]. The final output should be :

Good way to get *any* value from a Java Set?

Given a simple Set<T>, what is a good way (fast, few lines of code) to get any value from the Set? With a List, it’s easy: But, with a Set, there is no .get(…) method because Sets are not ordered. Answer A Set<T> is an Iterable<T>, so iterating to the first element works: Guava has a method to do this,

Why doesn’t java.util.Set have get(int index)?

I’m sure there’s a good reason, but could someone please explain why the java.util.Set interface lacks get(int Index), or any similar get() method? It seems that sets are great for putting things into, but I can’t find an elegant way of retrieving a single item from it. If I know I want the first item, I can use set.iterator().next(), but

Advertisement