LinqArraylist

General

Category
Free
Tag
Data Structures
License
MIT License
Min SDK
1 (Android 1.0)
Registered
Jul 25, 2018
Favorites
1
Link
https://github.com/loukaspd/LinqArraylist
See also
Marray
SparseBitSet
Solid
Agrona
SimpleHashSet

Additional

Language
Java
Version
N/A
Created
Jul 20, 2018
Updated
Jul 25, 2018
Owner
loukaspd
Contributor
loukaspd
1
Activity
Badge
Generate
Download
Source code

Show card

LinqArraylist

use Linq-like functions to Java ArrayLists

    implementation 'gr.loukaspd:LinqArraylist:1.0.0'

Usage

  1. Instantiate a LinqArrayList object by passing it your original ArrayList
  2. Apply linq methods (even chaining) like select, where etc..
  3. Get the resulting ArrayList by calling getArrayList() method

Examples

public static void main(String[] args) {
        ArrayList<SampleClass> myList = new ArrayList<>();
        myList.add(new SampleClass(1, "red"));
        myList.add(new SampleClass(2, "green"));
        myList.add(new SampleClass(3, "blue"));


        ArrayList<SampleClass> result = new LinqArrayList<>(myList)
                .where(new LinqArrayList.BooleanFunc<SampleClass>() {
                    @Override
                    public boolean lambda(SampleClass item) {
                        return item.id > 1;
                    }
                })
                .getArrayList();
        System.out.println(result);   //[2: green, 3: blue]

        System.out.println(new LinqArrayList<>(myList)
                .firstOrDefault(new LinqArrayList.BooleanFunc<SampleClass>() {
                    @Override
                    public boolean lambda(SampleClass item) {
                        return item.color.equals("red");
                    }
                }));    //1: red


        // chaining
        System.out.println(
                new LinqArrayList<>(myList)
                .where(new LinqArrayList.BooleanFunc<SampleClass>() {
                    @Override
                    public boolean lambda(SampleClass item) {
                        return item.id > 1;
                    }
                })
                .select(new LinqArrayList.SelectFunc<SampleClass, String>() {
                    @Override
                    public String lambda(SampleClass item) {
                        return item.color;
                    }
                })
                .getArrayList()
        );          //[green, blue]
    }