GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - aNNiMON/Android-Java-8-Stream-Example: Demo app of using Java 8 features with Retrolambda and Lightweight-Stream-API

BranchesTags
Open more actions menu

Latest commit

 

History

34 Commits
34 Commits

Folders and files

NameName
Last commit message
Last commit date
app
app
 
 
gradle/wrapper
gradle/wrapper
 
 
.gitignore
.gitignore
 
 
Java8StreamExample.iml
Java8StreamExample.iml
 
 
README.md
README.md
 
 
build.gradle
build.gradle
 
 
gradle.properties
gradle.properties
 
 
gradlew
gradlew
 
 
gradlew.bat
gradlew.bat
 
 
settings.gradle
settings.gradle
 
 

Repository files navigation

Android Java 8 Stream API Example

Demo app of using Java 8 features with Retrolambda and Lightweight-Stream-API.

Features:

  • () -> lambda expression

    findViewById(R.id.go).setOnClickListener(v -> {
       final int index = mActionSpinner.getSelectedItemPosition();
       if (index != Spinner.INVALID_POSITION) {
           action(actions[index]);
       }
    });
  • Method::references

    int cmp = Objects.compare(word, other.word, String::compareToIgnoreCase);
  • Stream.API()

    return Stream.of(lines)
         .map(str -> str.split("\t"))
         .filter(arr -> arr.length == 2)
         .map(arr -> new Word(arr[0], arr[1]))
         .collect(Collectors.toList());
  • switch for "string"

    switch (action) {
        case "filter 1":
            // Filter one word
            stream = stream.filter(p -> p.getWord().split(" ").length == 1);
            break;
        case "filter 2+":
            // Filter two and more words
            stream = stream.filter(p -> p.getWord().split(" ").length >= 2);
            break;
        // ...
    }
  • try(with-resources) {}

    final List<String> lines = new ArrayList<>();
    try (final InputStream is = context.getAssets().open("words.txt");
         final InputStreamReader isr = new InputStreamReader(is, "UTF-8");
         final BufferedReader reader = new BufferedReader(isr)) {
        String line;
        while ( (line = reader.readLine()) != null ) {
            lines.add(line);
        }
    }
  • Objects (from Java 7)

    @Override
    public boolean equals(Object o) {
        // ...
        final Word other = (Word) o;
        return Objects.equals(translate, other.translate) &&
               Objects.equals(word, other.word);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(word, translate);
    }
  • try { Exceptional } catch (functional try/catch)

    return Exceptional.of(() -> {
        final InputStream is = context.getAssets().open("words.txt");
        // ... operations which throws Exception ...
        return lines;
    }).ifException(e -> Log.e("Java 8 Example", "Utils.readWords", e))
      .getOrElse(new ArrayList<>());

Links

Demo app: Java8StreamExample.apk

Blog (Russian): Java 8 в Android со Stream API и лямбдами

Retrolambda: https://github.com/orfjackal/retrolambda

Lightweight-Stream-API: https://github.com/aNNiMON/Lightweight-Stream-API

Screenshots

screen_1 screen_2

About

Demo app of using Java 8 features with Retrolambda and Lightweight-Stream-API

Topics

androiddemojava-8retrolambdastream-api

Resources

Readme
Activity

Stars

111 stars

Watchers

6 watching

Forks

23 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.