Introduction to Java Stream Programming

What is a Stream? A Stream is a sequence of elements supporting sequential and parallel aggregate operations. It focuses on the “what” (declarative) rather than the “how” (imperative). Key Characteristics: No Storage: Streams don’t store data; they move it from a source (collection, array, I/O) through a pipeline. Functional: Operations don’t modify the source; they return new streams. Lazy Evaluation: Intermediate operations are only performed when a terminal operation is called. Possibly Infinite: Unlike collections, streams can be unbounded (e.g., a stream of random numbers). Stream Pipeline Stages Creation: list.stream(), Stream.of(a, b), IntStream.range(1, 10). Intermediate Operations: (Lazy) filter, map, sorted, distinct. Terminal Operations: (Triggers execution) collect, forEach, reduce, count, anyMatch. Common Operations Creation Stream.generate(Math::random).limit(10) Stream.iterate(0, n -> n + 2).limit(5) (Generates 0, 2, 4, 6, 8) Intermediate filter(Predicate): Selects elements. map(Function): Transforms elements. flatMap(Function): Flattens a stream of streams into a single stream. peek(Consumer): Performs an action for each element (mainly for debugging). Terminal collect(Collectors.toList()): Accumulates elements into a collection. reduce(BinaryOperator): Combines elements into a single result (e.g., sum). allMatch/anyMatch/noneMatch: Returns a boolean based on a condition. The Optional Class Many terminal operations return an Optional<T> to avoid NullPointerException if the stream is empty. ...

April 6, 2023 · 2 min

Introduction to Java Lambda Functional Programming

Overview Functional programming is based on Lambda Calculus, a formal system for studying functions developed by Alonzo Church in the 1930s. Languages like Haskell and Erlang brought these concepts into practical use. Java 8 introduced these features to handle growing program complexity through a more concise and robust style. OO is about abstracting data; FP is about abstracting behavior. Traditional vs. Lambda Style Before Java 8, we used anonymous inner classes for strategies or callbacks. This was verbose. ...

April 2, 2023 · 2 min

Microservice Architecture in the Cloud-Native Era

This sharing mainly focuses on the following four topics: What is Cloud Native? Why use Cloud-Native architecture? The concept of Microservices Technical selection for Microservices What is Cloud Native? Cloud Computing and Cloud Native Cloud computing is different from traditional self-built computer rooms. Cloud computing abstracts computing into infrastructure and distributes it through the network. Thanks to the infinite scaling capability of cloud computing, “cloud computing” is just like a water plant; we can get water at any time, unlimited, and pay according to our water consumption. Here are the five basic characteristics of cloud computing. ...

November 30, 2021 · 11 min

Microservice Design Principles Based on Spring Cloud

1. The Core Philosophy The shift from monolith to microservices is like moving from a “battleship” to a “fleet.” Breaking down a large application into small, independent services provides: Agility: Rapid deployment cycles. Resilience: Fault isolation (one service crash doesn’t kill the whole app). Scalability: Scaling only the bottleneck services. 2. The Cost of Decentralization Microservices are not a solution for everyone. They introduce: Network Latency: Inter-service calls are slower than in-memory calls. Data Consistency: Distributed transactions are complex (CAP Theorem). Operational Complexity: You need robust monitoring and logging. 3. Spring Cloud Ecosystem Spring Cloud provides a suite of tools for the common “pain points” of microservices: ...

September 14, 2020 · 2 min

Several CodeReview Experiences Summarized from Practice

Experienced programmers know that Code Review can greatly improve code quality, enforce coding standards, and enhance team capabilities. As the famous technical expert “Haoel” (Left-Eared Mouse) once said: I believe there is no need to stay in a company that doesn’t do Code Review (because a company that doesn’t do Code Review definitely doesn’t respect technology). — From “The Programmer’s Level-Up Guide - Cultivation” Many tech companies abroad, such as Google and Amazon, value Code Review highly and do it exceptionally well. However, many domestic companies struggle with it, often leading to counterproductive results. Here are some common situations I’ve seen: ...

August 25, 2020 · 3 min

Practical Lessons from a Technical Leader

Introduction Moving from a senior engineer to a Tech Lead is a major shift. Currently, I lead a team of about a dozen people, following Amazon’s “Two Pizza Rule.” I remember initially resisting this role, fearing that management would rob me of my coding time. This post is for those who are just stepping into their first leadership position. Key Questions to Explore: What are the core competencies of a Tech Lead? How does the mindset change from engineer to leader? How can we boost team efficiency and morale? How do we handle communication and self-growth? 1. Leadership and Competencies Developers are an “intelligent and proud” group. To earn their respect, a Tech Lead needs: ...

August 3, 2020 · 3 min

Basic Design Principles for Relational Databases

Preface Over the years, I’ve noticed a strange phenomenon: whether it’s an experienced veteran or a junior programmer, people love to talk about AI, Big Data, Blockchain, and various frameworks, but very few focus on databases. I’m not sure if it’s because databases seem too “low-end” or if they are just too low-profile. Technology is like that: the areas you don’t focus on are the ones where you’re most likely to fall into a pit. So, I want to talk about how to avoid “pits” when using databases. ...

February 27, 2020 · 4 min