0

If I have a HashSet<String>, how can I retrieve only the first word of each element of the Set?

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • `HashSet` doesn't guarantee any order so there's no reason to do this. – Matt Feb 25 '20 at 00:07
  • 1
    You could use the iterator returned by the Hashset, but it might be more efficient to use a TreeSet if you must use a set. – NomadMaker Feb 25 '20 at 00:12
  • 1
    What do you mean exactly by "first" word? Is it A) the first word added to the Set, B) the alphabetically first word, C) the first word encountered when iterating over the Set?, D) something else? – Bohemian Feb 25 '20 at 00:16
  • So if I were to have Strings, and look at the first word of each String, which data structure should I use? – user12956826 Feb 25 '20 at 00:17
  • 1
    @user12956826 "first word of each String" is an entirely different thing from first element of a set. You're changing the question. It would be better to ask a new question. Are you asking [how to iterate the set](https://www.google.com/search?q=java+how+to+iterate+a+set)? Or are you asking [how to get first word of a string](https://www.google.com/search?q=how+to+get+first+word+of+a+string), which has **nothing to do with sets**? – Andreas Feb 25 '20 at 00:19
  • @Andreas to be fair, I suspected OP wanted this by the wording, which did not say first *element*, but first *word*, and OP did not say the *Strings* only had one word. – Bohemian Feb 25 '20 at 00:20
  • @Bohemian Sorry, I can't be fair about that. If OP wanted to know how to get first word of a string, why even mention `HashSet`? And I can't be fair about it, given how easy the answer is to find with a web search. Down-voting question for lack of research. – Andreas Feb 25 '20 at 00:24
  • I have a bunch of strings in a set, and I want to find a specific word- but that word can only be the first word of a string – user12956826 Feb 25 '20 at 00:31
  • 2
    @user12956826 that's actualy a different question, because you don't need to know the first words, only that there's a match. ie something like `boolean found = set.stream().anyMatch(s -> s.startsWith(searchWord)).isPresent();` would do. – Bohemian Feb 25 '20 at 00:37

2 Answers2

3

To retrieve the first word of each String in your set, try this:

Collection<String> firstWords = set.stream()
    .map(s -> s.split(" ")[0]) // split on spaces, take first element of the split
    .collect(Collectors.toSet());

If you want to retain duplicates, change Collectors.toSet() to Collectors.toList()

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

You can get the first element of a set like this:

if (! set.isEmpty()) {
    first = set.iterator().next();
}

However, the "first" element is an arbitrary thing for a HashSet, since they are unordered, or as the javadoc says it:

It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.

Instead of HashSet, you could use a LinkedHashSet, which keeps insertion order, or a TreeSet, with is sorted, so in both cases "first" is well-defined.

Andreas
  • 154,647
  • 11
  • 152
  • 247