Java 8 Streams:获得非重复计数

Ora*_*key 6 java java-8 java-stream

这是输入和输出的SQL版本:

     with tab1 as (

        select 1 as id from dual union all
        select 1 as id from dual union all
        select 2 as id from dual union all
        select 2 as id from dual union all
        select 5 as id from dual 
        )

    select id from tab1 group by id having count(id)=1;

Output is Id=5 and count is 1
Run Code Online (Sandbox Code Playgroud)

因为5不重复.我如何使用JAVA 8流实现它?

我试过下面,但很明显它给出了错误的结果

List<Integer> myList = new ArrayList<Integer>();
                myList.add(1);
                myList.add(1);
                myList.add(2);
                myList.add(2);
                myList.add(5);

                Long f =  myList.stream()
                          .distinct().count();

                System.out.println(f);
Run Code Online (Sandbox Code Playgroud)

Eug*_*ene 4

long result = myList.stream()
         .collect(Collectors.groupingBy(
                   Function.identity(),
                  Collectors.counting()))
         .entrySet()
         .stream()
         .filter(entry -> entry.getValue() == 1)
         .map(Entry::getKey)
         .count();
Run Code Online (Sandbox Code Playgroud)

好吧,您将所有元素收集到 a Map<Integer, Long>,其中键是值本身,值是它重复的次数。稍后我们从结果映射中流式传输条目集,并且filter仅流式传输那些具有计数的条目1(意味着它们不重复),稍后我们从列表中获取该值并进行计数mapEntry::getKey