0
static void main(String[] args) {
     List<Foo> fooList = new LinkedList<>();
     Set<Foo> fooSet = new HashSet<>();

     Foo element1 = new Foo("Element1");
     Foo element2 = new Foo("Element2");
     Foo element3 = new Foo("Element3");

     fooSet.add(element1);
     fooSet.add(element2);
     fooSet.add(element3);

     CollectionUtils.addAll(fooList, fooSet);
}

Is there a way to convert from a Set to a List and guaranteeing element1 is the first element in the List? I do not care about the ordering for the rest of the elements in the List only the very first element. I could remove element1 from the Set then add it to the List then add the rest of the element. I'm just wondering what is the cleanest way to do this.

Holger
  • 285,553
  • 42
  • 434
  • 765
Muhamad Gafar
  • 409
  • 3
  • 12
  • 1
    What’s the point of `CollectionUtils.addAll`? Why don’t you use a straight-forward `fooList.addAll(fooSet);`? And if you don’t care about the remaining order, you can simply do a `if(fooList.get(0) != element1) Collections.swap(fooList, 0, fooList.indexOf(element1));` afterwards. Also worth reading [When to use LinkedList over ArrayList in Java?](https://stackoverflow.com/q/322715/2711488) – Holger Jul 04 '22 at 10:25
  • You're right there's no need for CollectionUtils.addAll – Muhamad Gafar Jul 04 '22 at 12:21

1 Answers1

3

convert from a Set to a List and guaranteeing element1 is the first element in the List?

For that, you need to use a LinkedHashSet which is capable of maintaining the order of elements instead of HashSet.

Set<Foo> fooSet = new LinkedHashSet<>();

fooSet.add(element1);
fooSet.add(element2);
fooSet.add(element3);

List<Foo> fooList = new LinkedList<>(fooSet);

And since you've added the tag it seems like use you were thinking of stream-based solution, I would point out that there's no need to create a stream and utilize a collector just in order to dump the same data without any changes into a list.

Sure, no one has a power to prohibit you to right such line:

List<Foo> fooList = fooSet.stream().collect(Collectors.toList());

But it would be a misuse of streams.

Holger
  • 285,553
  • 42
  • 434
  • 765
Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46
  • Thanks, the LinkedHashSet hadn't crossed my mind. As for the use of Streams they were on my mind because of findFirst so I thought when collecting to a list there may be a way to setFirst. – Muhamad Gafar Jul 04 '22 at 14:22
  • @MuhamadGafar Stream is nothing more than a **mean of iteration** over the source of data. And the order of stream elements entirely depends on the source of data. Stream generating from a `HashSet` would is not guaranteed to have a particular order. – Alexander Ivanchenko Jul 04 '22 at 17:24
  • 1
    @MuhamadGafar There's **no** anything close to `setFirst` whatsoever in Stream API. It's possible to create an intricate *custom collector* which would be able to intercept a particular element (or elements) from the stream and treat them differently than others... **But** the main weapon of the *functional programming* in Java is a **simplicity**, ideal stream is simple and straightforward as a grocery list. When stream gets more complicated than other solutions - that means that streams are not the right way to go (unless you're simply exercising). – Alexander Ivanchenko Jul 04 '22 at 17:25