DEV Community

Mustafa  Çam
Mustafa Çam

Posted on

1

Stream API

Java 8 Stream API, işlerimizi kolaylaştıran bir araçtır ve verimli ve temiz uygulamalar oluşturmak için birçok yeni model getirir. Stream API, liste ve dizileri işlemek için işlevsel operasyonlar sağlar. İşte bazı önemli Stream API operasyonları:

  1. Filter (Filtreleme):

    • filter metodu, bir listeyi veya diziyi belirli bir koşula göre kısıtlamamıza olanak tanır. Örneğin, kullanıcı listesindeki bazı kriterlere göre listeyi indirgeyebiliriz:
     List<User> userList = Arrays.asList(user1, user2, user3);
     userList.stream()
             .filter(user -> user.getSurname().contains("a") && user.getAge().equals(65))
             .collect(Collectors.toList())
             .forEach(System.out::println);
    

    Yukarıdaki örnekte, isminde 'a' harfi geçenleri ve yaşı 65 olan kullanıcıları filtreleyerek ekrana yazdırmak istedik.

  2. Sorted (Sıralama):

    • sorted metodu, veri setini belirli parametrelere göre sıralamamıza olanak tanır. Örneğin, kitapların sayfa sayısına göre küçükten büyüğe sıralanışını aşağıdaki gibi yapabiliriz:
     List<Book> bookList = Arrays.asList(book1, book2, book3);
     List<Book> bookSortedListByPage = bookList.stream()
                                               .sorted(Comparator.comparingInt(Book::getPage))
                                               .collect(Collectors.toList());
     System.out.println(bookSortedListByPage);
    

    Burada Comparator.comparingInt(Book::getPage) ile kitapların sayfa sayısına göre sıralama yapacağımızı belirttik.

  3. Map (Dönüşüm):

    • map metodu, stream içerisindeki her bir nesneye özgü işlemler yapmamıza olanak sağlar. Örneğin, rakamların karesini almak için kullanabiliriz:
     List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
     List<Integer> squares = numbers.stream()
                                    .map(n -> n * n)
                                    .collect(Collectors.toList());
     System.out.println(squares);
    

    Yukarıdaki örnekte, her rakamın karesini alarak yeni bir liste oluşturduk.

Java Stream API, veri işlemlerini daha anlaşılır ve temiz bir şekilde ifade etmemize yardımcı olur. Bu operasyonlar, kodunuzu daha okunaklı ve etkili hale getirir. 🚀

Kaynaklar:

Kaynak: Bing ile konuşma, 17.05.2024
(1) Java 8 Stream Api Nedir ? Nasıl Kullanılır ? ( filter, sorted, map .... https://medium.com/@muhammetenesakcayir/java-8-stream-api-32dd3e9aa30f.
(2) Java 8 – Stream API (Filter, Sorted, Map, Match, Reduce). https://www.mobilhanem.com/stream-api-filter-sorted-map-match-reduce/.
(3) java - Stream API - How does sorted () operation works if a filter .... https://stackoverflow.com/questions/72118729/stream-api-how-does-sorted-operation-works-if-a-filter-is-placed-right-aft.
(4) Java 8 - Stream API – KodEdu. https://kodedu.com/2014/10/java-8-stream-api/.
(5) Difference between map and filter on a java collection stream. https://stackoverflow.com/questions/37154380/difference-between-map-and-filter-on-a-java-collection-stream.
(6) github.com. https://github.com/LinZiYU1996/Learning-Java/tree/a0d9f538c9d373c3a93ccd890006ce0e5e1f2d5d/MyNotes%2FJDK_8%2Fchapter_5%2Fc_5_2%2Fc_5_2_2%2FTest_5_2_1.java.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay