0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaのSteam APIを使ってみた

Last updated at Posted at 2023-10-25
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class HelloWorld {
    public static void main(String[] args) {

        List<Map<String, String>> msgList = new ArrayList<>();
        
        Map<String, String> msgMap = new HashMap<>();

        msgMap.put("1", "test[warning_726]");
        msgList.add(msgMap);

        msgMap = new HashMap<>();
        msgMap.put("1", "test3333[warning_727]");
        msgList.add(msgMap);

        msgMap = new HashMap<>();
        msgMap.put("1","test2[warning_736]");
        msgList.add(msgMap);

        msgMap = new HashMap<>();
        msgMap.put("1","test21[914_warning]");
        msgList.add(msgMap);

        msgMap = new HashMap<>();
        msgMap.put("1","test255[warning_737]");
        msgList.add(msgMap);

        msgMap = new HashMap<>();
        msgMap.put("2","error_728");
        msgList.add(msgMap);

        // msgList: [{1=test[warning_726]}, {1=test3333[warning_727]}, {1=test2[warning_736]}, {1=test21[914_warning]}, {1=test255[warning_737]}, {2=error_728}]
        System.out.println("msgList: " + msgList);

        /*
         * test3333 の値を含む物の数を取得
         */
        var a = msgList.stream()
            .flatMap(msg -> msg.entrySet().stream())
            .filter(entry -> entry.getValue().contains("test3333"))
            .count();

        // a : 1
        System.out.println("a: " + a);

        /*
         * test3333 の値を含む物を取得
         */
        var b = msgList.stream()
            .flatMap(msg -> msg.entrySet().stream())
            .filter(entry -> entry.getValue().contains("test3333"))
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

        // b: {1=test3333[warning_727]}
        System.out.println("b: " + b);

        /*
         * warning_726 の値が含まれない物を全て取得
         */
        msgList = msgList.stream()
            .filter(map -> map.entrySet().stream()
                .noneMatch(entry -> entry.getValue().contains("warning_726")))
            .collect(Collectors.toList());

        /*
         * warning_727 の値が含まれない物を全て取得
         */
        msgList = msgList.stream()
            .filter(map -> map.entrySet().stream()
                .noneMatch(entry -> entry.getValue().contains("warning_727")))
            .collect(Collectors.toList());

        // msgList: [{1=test2[warning_736]}, {1=test21[914_warning]}, {1=test255[warning_737]}, {2=error_728}]
        System.out.println("msgList: " + msgList);
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?