GithubHelp home page GithubHelp logo

timandy / linq Goto Github PK

View Code? Open in Web Editor NEW
213.0 15.0 45.0 3.6 MB

LINQ to Objects for Java.

License: Apache License 2.0

Java 100.00%
java android linq linq4j lambda stream-api functional-programming tuple

linq's Introduction

LINQ to Objects (Java)

Build Status Codecov Maven Central GitHub release License

中文文档

The term "LINQ to Objects" refers to the use of LINQ queries with any IEnumerable<T>. You can use LINQ to query any enumerable collections such as Primitive Array, Object Array, List, Collection or Iterable and so on. The collection may be user-defined or may be returned by a JDK API.

In a basic sense, LINQ to Objects represents a new approach to collections. In the old way, you had to write complex foreach loops that specified how to retrieve data from a collection. In the LINQ approach, you write declarative code that describes what you want to retrieve.

In addition, LINQ queries offer two main advantages over traditional foreach loops:

  1. They are more concise and readable, especially when filtering multiple conditions.
  2. They provide powerful filtering, ordering, and grouping capabilities with a minimum of application code.

LINQ queries also have some advantages over stream API:

  1. Support foreach loops therefore you can break loop at any time.
  2. IEnumerable can be traversed repeatedly.
  3. LINQ is very easy to use, like ToCollection, LeftJoin and so on.
  4. LINQ is faster than stream API in most complicated cases.

In general, the more complex the operation you want to perform on the data, the more benefit you will realize by using LINQ instead of traditional iteration techniques.

Features

  • Implemented all API of LINQ to Objects.
  • More API and Tuple supported.
  • Support for converting between IEnumerable and Stream.
  • Android supported.

bestvike

Navigation

linq

API of Linq

  • empty
  • singleton
  • ofNullable
  • of
  • as
  • chars
  • words
  • lines
  • split
  • infinite
  • loop
  • enumerate
  • iterate
  • range
  • repeat

API of IEnumerable

  • forEach
  • stream
  • parallelStream
  • aggregate
  • all
  • any
  • append
  • asEnumerable
  • average
  • cast
  • chunk
  • concat
  • contains
  • count
  • crossJoin
  • defaultIfEmpty
  • distinct
  • distinctBy
  • elementAt
  • elementAtOrDefault
  • except
  • exceptBy
  • findIndex
  • findLastIndex
  • first
  • firstOrDefault
  • format
  • fullJoin
  • groupBy
  • groupJoin
  • indexOf
  • intersect
  • intersectBy
  • join
  • joining
  • last
  • lastIndexOf
  • lastOrDefault
  • leftJoin
  • longCount
  • max
  • maxBy
  • min
  • minBy
  • ofType
  • orderBy
  • orderByDescending
  • prepend
  • reverse
  • rightJoin
  • runOnce
  • select
  • selectMany
  • sequenceEqual
  • shuffle
  • single
  • singleOrDefault
  • skip
  • skipLast
  • skipWhile
  • sum
  • take
  • takeLast
  • takeWhile
  • toArray
  • toCollection
  • toEnumeration
  • toLinkedList
  • toLinkedMap
  • toLinkedSet
  • toList
  • toLookup
  • toMap
  • toSet
  • union
  • unionBy
  • where
  • zip

API of IGrouping extends IEnumerable

  • getKey

API of ILookup extends IEnumerable

  • getCount
  • get
  • containsKey

API of IOrderedEnumerable extends IEnumerable

  • thenBy
  • thenByDescending

API of Index

  • fromStart
  • fromEnd
  • getValue
  • isFromEnd
  • getOffset

API of Range

  • startAt
  • endAt
  • getStart
  • getEnd
  • getOffsetAndLength

Tuple classes

  • Tuple1
  • Tuple2
  • Tuple3
  • Tuple4
  • Tuple5
  • Tuple6
  • Tuple7
  • TupleN

Debug View (IntelliJ IDEA)

  1. Open the settings dialog File | Settings | Build, Execution, Deployment | Debugger | Data Views | Java Type Renderers.
  2. Click the Add button.
  3. Type IterableView in Renderer name.
  4. Type java.lang.Iterable in Apply renderer to objects of type (fully-qualified name):.
  5. In the When rendering a node section, check Use following expression: and type DebugView.getDebuggerDisplayText(this). If you get an error, press Alt + Enter to import the class.
  6. Check Use list of expression: and type **RESULT VIEW** in the Name column. Type DebugView.getDebuggerProxyObject(this) in the Expression column. If you get an error, press Alt + Enter to import the class. It is recommended to check On-demand to enumerate the sequence if necessary.
  7. If you want to see the default field of the Iterable instance, check Append default children.
  8. Save your settings and try it out!

Result view should be used with caution because of possible side-effects.

Maven

<dependency>
    <groupId>com.bestvike</groupId>
    <artifactId>linq</artifactId>
    <version>6.0.0</version>
</dependency>

Gradle

implementation 'com.bestvike:linq:6.0.0'

Usage

If you use java 8 or java 9, it is recommended to replace the complex return type with lombok.var or lombok.val. If you use java 10 or later, it is recommended to replace the complex return type with var.

  • Join not empty strings.
String result = Linq.of("!@#$%^", "C", "AAA", "", "Calling Twice", "SoS", Empty)
        .where(x -> x != null && x.length() > 0)
        .aggregate((x, y) -> x + ", " + y);

System.out.println(result);
----
!@#$%^, C, AAA, Calling Twice, SoS
  • Determine all positive numbers is even or not.
boolean result = Linq.of(9999, 0, 888, -1, 66, -777, 1, 2, -12345)
        .where(x -> x > 0)
        .all(x -> x % 2 == 0);

System.out.println(result);
----
false
  • Determine any positive number is even or not.
boolean result = Linq.of(9999, 0, 888, -1, 66, -777, 1, 2, -12345)
        .where(x -> x > 0)
        .any(x -> x % 2 == 0);

System.out.println(result);
----
true
  • Append a number at the end and insert two numbers in the header.
String result = Linq.range(3, 2).append(5).prepend(2).prepend(1).format();

System.out.println(result);
----
[1, 2, 3, 4, 5]
  • Compute average of integer sequence.
double result = Linq.of(5, -10, 15, 40, 28).averageInt();

System.out.println(result);
----
15.6
  • Concat two integer sequence.
String result = Linq.of(1, 2).concat(Linq.of(3, 4)).format();

System.out.println(result);
----
[1, 2, 3, 4]

License

LINQ to Objects (Java) is released under the Apache License 2.0.

Copyright 2017-2024 济南百思为科信息工程有限公司

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

linq's People

Contributors

dependabot[bot] avatar timandy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

linq's Issues

常试List泛型对象进行linq查询无效

List<StockExcelInitMaterial_Entity> list = Linq.asEnumerable(ss)
.where(w -> w.getMaterialName().contains(sName))
.toList();

list为0,为什么?ss也是List<StockExcelInitMaterial_Entity>对象

linq library .jar

Hi, how are you? I hope that you are good, I am interested about this repository and I have a question:

Do your have a .jar library about this project? it would be very useful to community and if you talk with oracle developers, you repository will be an official java module in the next jdk versions.

I wait your answer.

Greetings.

为什么会有一定断言失败的场景代码

code

请问这个断言的目的是什么?

因为我在单元测试发现断言异常,而普通运行时却没有问题。最后查看您的代码,发现是单元测试的开启了断言导致的。
但是想不明白,这个断言是一定会失败的,想不出它的目的究竟是什么?

DebugView Not Working in Intellij

I have followed the instructions mentioned here for setting up the debug view in Intellij.

But I am getting the below error

Screenshot 2023-07-03 at 10 57 16 PM

My debugger settings are like below

Screenshot 2023-07-03 at 10 57 34 PM

Can someone suggest what is going wrong?

  • Intellij IDEA 2022.1.4
  • linq - 5.0.0

前辈你好

前辈你好,我从C#转java1年多,一直陆续在写一些utils,初衷也是希望写java代码时能更简洁优雅,特别是对集合数组的操作。最近一周刚开始写java linq,底层操作api基本都实现了。昨晚刚准备提供1个包装层,也想好命名Linq,暴露1个asEnumber的路口。。。
想着java轮子多特地一搜,结果搜到前辈的大作。
粗略看了前辈的源码,简直太棒了。甚至连对Action012、Function012的封装命名都惊人的一致。。
前辈的代码真规范,结构也非常清晰,让我非常喜欢和敬仰。
这么好的开源项目,居然只有10个star!太不科学了!
非常想认识前辈,不知是否有幸。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.