Java language LINQ expression implementation method based on chain call

By introducing LINQ expression implementation with chained calls into the Java language, the code verbosity and type safety issues of Java data querying are solved, enabling efficient and readable data querying, supporting unified querying of multiple data sources, and improving development efficiency and performance.

CN120723210BActive Publication Date: 2026-06-02GUANGZHOU JUNBO NETWORK TECH INC

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
GUANGZHOU JUNBO NETWORK TECH INC
Filing Date
2025-06-18
Publication Date
2026-06-02

AI Technical Summary

Technical Problem

The Java language suffers from verbose code, poor readability, lack of SQL-style query support, insufficient extensibility, and difficulty in meeting the needs of complex and ever-changing business scenarios in data query processing. Furthermore, its type safety is weak, which can easily lead to runtime errors.

Method used

This paper provides a Java LINQ expression implementation method based on chained calls, including a query context interface, an operator implementation module, a query expression parsing module, and an execution engine module. It provides type safety through generics, supports chained calls and multi-condition combinations, parses into an operator tree, and executes query operations.

Benefits of technology

The code length is reduced by approximately 40%, readability is significantly improved, type checking is performed at compile time, runtime errors are reduced, unified querying from multiple data sources is supported, and development efficiency and query performance are improved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120723210B_ABST
    Figure CN120723210B_ABST
Patent Text Reader

Abstract

The application is suitable for the field of computer programming and provides a Java language LINQ expression implementation method based on chain calling, which comprises the following steps: providing a LinqQuery interface, the LinqQuery interface defines a chain operator where operator and select, and type safety is provided through a generic T; a user generates an operator tree through calling the chain operator of the LinqQuery interface, the operator tree is used for recording query logic; the operator tree is parsed, query is executed in layers, and a result set is returned; the application has the beneficial effects that compared with traditional loop and conditional branch implementation, the number of code lines is reduced by about 40%, the readability is significantly improved, through an open operator interface, a developer can easily extend query functions, through generic constraints, type checking of query logic is completed at the compilation stage, runtime errors are reduced, and unified query of various scenes such as memory collection, database query and file reading is supported.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of computer programming, and in particular relates to a method for implementing Java LINQ expressions based on chained calls. Background Technology

[0002] With the advent of the big data era and the increasing prevalence of complex data manipulation scenarios, the demand for efficient querying of collections, streams, and databases in the programming field has exploded. Under this technological trend, C#'s LINQ technology has emerged as a core feature of the C# ecosystem. It provides developers with powerful and convenient in-memory query capabilities, greatly improving the efficiency and convenience of data processing, making it a powerful tool for C# developers to perform data queries.

[0003] In contrast, the Java language faces numerous challenges in data query processing. Currently, Java developers often have to rely on cumbersome loops, conditional branching statements, or external libraries to implement similar query logic. This traditional query approach has many drawbacks: on the one hand, the code is verbose and cumbersome, with numerous loops and conditional logic significantly reducing readability and increasing development and maintenance costs; on the other hand, while Java's built-in Stream API has some query capabilities, it lacks good support for SQL-style queries, and its functionality is fragmented and limited. Furthermore, its scalability is insufficient to meet the needs of complex and ever-changing business scenarios, and it is weak in type safety, with query conditions prone to runtime errors and lacking effective compile-time type checking support. To solve these technical problems, there is an urgent need to develop a Java LINQ expression implementation method based on chained calls. Summary of the Invention

[0004] The purpose of this invention is to provide a Java LINQ expression implementation method based on chained calls, aiming to solve the problems mentioned in the background art.

[0005] This invention is implemented as follows: It proposes a LINQ query expression parser and execution framework, with core modules including:

[0006] Query Context Interface: Defines the basic structure of LINQ queries and supports chained queries. Type safety is provided through generics. Specifically, it provides the `LinqQuery` interface, defining chained operators such as `where` and `select`.

[0007] Operator Implementation Module: Implements commonly used query operators, including Where, Select, GroupBy, and OrderBy. Query logic is defined using functional interfaces, supporting lambda expressions for developers to write query conditions in a concise manner.

[0008] The query expression parsing module parses user-defined chained calls into an operator tree, supporting complex nested queries and multi-condition combinations, thus enabling it to handle various complex query requirements.

[0009] Execution engine module: Traverses the operator tree and executes the corresponding query operations. Supports diverse data sources, enabling unified query operations across different scenarios.

[0010] A method for implementing Java LINQ expressions based on chained calls, the steps of which are as follows:

[0011] Define query context: Provide the LinqQuery interface, define chained operators such as where and select, and ensure type safety by using the generic type T to ensure type checking at compile time.

[0012] Constructing the operator tree: Users generate the operator tree through chained calls. The operator tree records the complete query logic, providing a foundation for subsequent parsing and execution.

[0013] Parse and execute queries: Parse the operator tree, execute queries hierarchically, and finally return a set of results.

[0014] This invention provides a Java LINQ expression implementation method based on chained calls. Compared with traditional loop and conditional branch implementations, it reduces the number of lines of code by about 40%, significantly improves readability, and allows developers to easily extend query functionality through open operator interfaces. Through generic constraints, the type checking of query logic is completed at compile time, reducing runtime errors and supporting unified queries for various scenarios such as memory collections, database queries, and file reading. Attached Figure Description

[0015] Figure 1 This is the main flowchart of a Java LINQ expression implementation method based on chained calls. Detailed Implementation

[0016] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the invention.

[0017] The specific implementation of the present invention will be described in detail below with reference to specific embodiments.

[0018] This invention provides a Java language LINQ expression implementation method based on chained calls, which solves the technical problems in the background art.

[0019] like Figure 1 The diagram shown is a main flowchart of a Java LINQ expression implementation method based on chained calls, according to an embodiment of the present invention. The method includes:

[0020] Step S100: Provide the LinqQuery interface, which defines the chained operators where and select, and provides type safety through generic T;

[0021] Step S200: The user generates an operator tree by calling the chained operators of the LinqQuery interface. The operator tree is used to record the query logic.

[0022] Step S300: Parse the operator tree, perform the query hierarchically, and return the result set;

[0023] In this embodiment, the query context is first defined by providing the LinqQuery interface and defining chained operators such as WHERE and SELECT within it. Using generics (T), data types are rigorously checked during the compilation phase, effectively avoiding runtime type errors and providing robust type safety for the entire query process. This design not only improves code reliability but also gives developers greater peace of mind when writing query logic. Next, an operator tree is constructed. When users call the chained operators of the LinqQuery interface, these operations are transformed into an operator tree. The operator tree acts as a "blueprint" for the entire query logic, recording the user-defined query logic. Finally, the query is parsed and executed. The constructed operator tree is parsed, and query operations are executed sequentially according to the hierarchy, ultimately returning the set of results that meet the conditions to the user.

[0024] In a preferred embodiment of the present invention, a query expression parsing module is also included. This module parses user-defined chained calls into an operator tree and supports complex nested queries and multi-condition combinations. An execution engine module is also included. This module traverses the operator tree and executes the corresponding query operations. The execution engine module supports diverse data sources, including memory collections, databases, and file systems. Query logic is dynamically constructed through generics and functional interfaces, ensuring type safety at compile time. Query conditions are bound to operators to reduce runtime error risks. The operator tree is used to optimize the query logic and avoid redundant calculations. Deferred evaluation is performed during the execution phase, executing the query only when the result is needed. An open query context interface is defined to support user-defined operators and extended query functionality. The operator tree parsing and execution logic can be replaced with a parallel computing framework to improve performance. The data source is extended to databases and files through the adapter pattern.

[0025] In this embodiment, the expression parsing module can accurately parse the query statements written by the user through chained calls into a structured operator tree. It supports complex nested queries and multi-condition combinations, easily handling data query needs in various complex business scenarios. The operator tree, as a visual "carrier" of the query logic, provides a solid foundation for subsequent query optimization and execution. At the optimization level, it can effectively identify and avoid redundant calculations, reducing unnecessary resource consumption. The execution engine module is responsible for traversing the operator tree and executing the corresponding query operations sequentially. It supports diverse data sources, covering in-memory collections, databases, and file systems.

[0026] During the execution phase, a deferred evaluation strategy is adopted, triggering the execution operation only when query results are needed, further improving query performance and resource utilization efficiency. Furthermore, the operator tree parsing and execution logic of the execution engine module are highly flexible and can be replaced with parallel computing frameworks, such as the Fork / Join framework or Java Stream's parallel streams, according to actual needs, thereby significantly improving data processing speed. In terms of extensibility, this invention provides developers with space to customize operators and extend query functionality by defining an open query context interface. Developers can flexibly add new query operators or extend existing functionality according to specific business needs. Simultaneously, through the adapter pattern, data sources can be seamlessly extended to databases, files, etc., greatly enhancing the applicability of this method in different application scenarios.

[0027] As a preferred embodiment of the present invention, a simple query of the memory set is performed:

[0028] Suppose there exists a collection of Person objects in memory, each with a name and age attribute.

[0029] First, define the query context interface LinqQuery and implement its WHERE and SELECT operators. For example:

[0030] public interface LinqQuery <t>{

[0031] LinqQuery <t>where(Predicate <t>predicate);

[0032] <r>LinqQuery <r>select(Function<T, R> function);

[0033] List <t>toList();

[0034] };

[0035] Users can then construct queries using chained queries. For example, to query a list of names of people older than 18:

[0036] List <person>personList = new ArrayList<>();

[0037] / / Initialize the personList data;

[0038] LinqQuery <person>query = new LinqQueryImpl<>(personList);

[0039] List <string>names = query.where(person -> person.getAge() > 18)

[0040] .select(Person::getName)

[0041] .toList();

[0042] In the code above, a `LinqQuery` instance is first created and passed a list containing `Person` objects. Then, the query conditions are set using chained calls to the `where` operator, filtering out `Person` objects whose age is greater than 18. Next, the `select` operator is used to convert the filtered `Person` objects into their names. Finally, the `toList` method is called to execute the query and retrieve the result list. In actual execution, the query expression parsing module parses the user's chained calls into an operator tree. The execution engine module traverses the operator tree, first executing the `where` operation for filtering, then executing the `select` operation for conversion, and finally returning the result set.

[0043] As a preferred embodiment of the present invention, data query in the order management system:

[0044] In e-commerce order management systems, complex queries on order data are often required, such as filtering orders by conditions, calculating statistics, and grouping by user. In this case, the data source is a List. <order>The Order object contains key information such as the user ID, amount, and status of the order.

[0045] Using the Java LINQ expression implementation method based on chained calls of this invention, specific query requirements can be achieved through the following core code:

[0046] List <order>orders = getOrderList();

[0047] / / Query chaining

[0048] List <userordersummary>result = LinqQuery.from(orders)

[0049] .where(order->order.getAmount()>100&& "completed".equals(order.getStatus()))

[0050] .groupBy(Order::getUserId, Collectors.summingDouble(Order::getAmount))

[0051] .select((userId, totalAmount) -> new UserOrderSummary(userId,totalAmount))

[0052] .toList();

[0053] The code above implements two core query operations: First, it filters out orders with an amount greater than 100 and a status of "completed"; then, it groups the filtered orders by user ID and calculates the total order amount for each user. Finally, it converts the results into a list of UserOrderSummary objects, which record the user ID and the corresponding total order amount.

[0054] The above embodiments of the present invention provide a Java LINQ expression implementation method based on chained calls, providing a LinqQuery interface. The LinqQuery interface defines chained operators, the where operator and the select operator, and provides type safety through generic T. Users generate an operator tree by calling the chained operators of the LinqQuery interface. The operator tree is used to record query logic. The operator tree is parsed, the query is executed hierarchically, and a result set is returned. Compared with traditional loop and conditional branch implementations, the number of lines of code is reduced by about 40%, and readability is significantly improved. Through the open operator interface, developers can easily extend the query function. Through generic constraints, the type checking of the query logic is completed at compile time, reducing runtime errors, and supporting unified queries in various scenarios such as memory collections, database queries, and file reading.

[0055] In order for the above methods and systems to operate smoothly, the system may include more or fewer components than those described above, or combine certain components, or different components, in addition to the various modules mentioned above. For example, it may include input / output devices, network access devices, buses, processors, and memory.

[0056] The processor can be a central processing unit, or other general-purpose processors, digital signal processors, application-specific integrated circuits (ASICs), off-the-shelf programmable gate arrays (OPGs) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor can be a microprocessor or any conventional processor. This processor is the control center of the system, connecting various parts via various interfaces and lines.

[0057] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0058] The embodiments described above are merely illustrative of several implementations of the present invention, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of the present invention. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of the present invention, and these modifications and improvements all fall within the scope of protection of the present invention. Therefore, the scope of protection of this patent should be determined by the appended claims.

[0059] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention should be included within the protection scope of the present invention.< / userordersummary> < / order> < / order> < / string> < / person> < / person> < / t> < / r> < / r> < / t> < / t> < / t>

Claims

1. A Java LINQ expression implementation method based on chained calls, characterized in that, The method includes: Provides the LinqQuery interface, which defines the chained operators where and select, and provides type safety through the generic type T; Users generate an operator tree by calling the chained operators of the LinqQuery interface, and the operator tree is used to record the query logic; Parse the operator tree, execute the query hierarchically, and return the result set; Provide a query context interface, through which the basic structure of LINQ queries is defined, and support for chained calls; Implement common query operators, including where, select, groupBy, and orderBy, and define query logic using a functional interface to support lambda expressions; The query expression is parsed, and the user-defined chained calls are parsed into an operator tree. Complex nested queries and multiple condition combinations are supported during the parsing process. The execution engine traverses the operator tree and executes the corresponding query operations. The execution operations support diverse data sources, including memory collections, databases, and file systems. Query logic is dynamically constructed using generics and functional interfaces, ensuring type safety at compile time and binding query conditions to operators to reduce runtime error risks; Operator trees are used to optimize query logic and avoid redundant calculations; evaluation is deferred during the execution phase, and queries are executed only when the result is needed. By defining an open query context interface, user-defined operators and extended query functionality are supported.

2. The Java LINQ expression implementation method based on chained calls according to claim 1, characterized in that, The operator tree parsing and execution logic can be replaced with a parallel computing framework to improve performance.

3. The Java LINQ expression implementation method based on chained calls according to claim 1, characterized in that, The adapter pattern extends the data source to databases and file systems.