How to select duplicate values from a list in java?

后端 未结 13 916
傲寒
傲寒 2021-01-18 00:38

For example my list contains {4, 6, 6, 7, 7, 8} and I want final result = {6, 6, 7, 7}

One way is to loop through the list and eliminate unique values (4, 8 in this

13条回答
  •  孤独总比滥情好
    2021-01-18 01:32

    The following will work with Eclipse Collections:

    IntBag bag = IntLists.mutable.with(4, 6, 6, 7, 7, 8).toBag().selectDuplicates();
    

    If you want boxed values instead of primitives, the following will work:

    Bag bag = Lists.mutable.with(4, 6, 6, 7, 7, 8).toBag().selectDuplicates();
    

    Note: I am a committer for Eclipse Collections.

提交回复
热议问题