Deepstack
Deepstack

Reputation: 106

How can i make linked CharBag (Eclipse Collections)?

I know about CharBag bag = CharAdapter.adapt("hello world!").toBag();it's nice, but it's not linked. I need bag with linked input string and how can i get keys and values from this collection to make output like:

h 1
e 1
l 3
o 2
  1
w 1
r 1
d 1
! 1

Upvotes: 1

Views: 100

Answers (1)

Donald Raab
Donald Raab

Reputation: 6706

There is currently no LinkedCharBag in Eclipse Collections as you point out. You can accomplish your goal by using the following solution leveraging distinct on CharAdapter:

CharAdapter helloWorld = Strings.asChars("hello world!");
CharBag bag = helloWorld.toBag();
helloWorld.distinct().forEach(c -> System.out.println(c + " " + bag.occurrencesOf(c)));

Upvotes: 1

Related Questions