我知道CharBag bag = CharAdapter.adapt("hello world!").toBag();
,它很好,但它没有链接。我需要带有链接输入字符串的包,以及我如何从这个集合中获取键和值来进行输出,如:
h 1
e 1
l 3
o 2
1
w 1
r 1
d 1
! 1
正如您所指出的,Eclipse集合中目前没有LinkedCharBag
。您可以通过在CharAdapter
上使用以下利用distinct
的解决方案来实现您的目标:
CharAdapter helloWorld = Strings.asChars("hello world!");
CharBag bag = helloWorld.toBag();
helloWorld.distinct().forEach(c -> System.out.println(c + " " + bag.occurrencesOf(c)));