Skip to content

Instantly share code, notes, and snippets.

@ucguy4u
Last active September 19, 2020 13:07
Show Gist options
  • Save ucguy4u/502cd4db3692c507d6926b07be606857 to your computer and use it in GitHub Desktop.
Save ucguy4u/502cd4db3692c507d6926b07be606857 to your computer and use it in GitHub Desktop.
Collections Api using Lambda expressions
package com.ucguy4u.functional.java;
import java.util.*;
import java.util.stream.Collectors;
import static java.util.Arrays.asList;
/**
* @author chauhanuday
*/
public class Lambdas_04 {
public static void main(String[] args) {
List<String> names = Arrays.asList("Paul", "Jane", "Michaela", "Sam");
// way to sort prior to Java 8 lambdas
Collections.sort(names, new Comparator<String>() {
@Override
public int compare(String a, String b) {
return b.compareTo(a);
}
});
// first iteration with lambda
Collections.sort(names, (String a, String b) -> {
return b.compareTo(a);
});
// now remove the return statement
Collections.sort(names, (String a, String b) -> b.compareTo(a));
// now remove the data types and allow the compile to infer the type
Collections.sort(names, (a, b) -> b.compareTo(a));
// total pages in your book collection
Book book1 = new Book("Miss Peregrine's Home for Peculiar Children", "Ranson", "Riggs", 382);
Book book2 = new Book("Harry Potter and The Sorcerers Stone", "JK", "Rowling", 411);
Book book3 = new Book("The Cat in the Hat", "Dr", "Seuss", 45);
List<Book> books = Arrays.asList(book1, book2, book3);
int total = books.stream().collect(Collectors.summingInt(Book::getPages));
System.out.println("\n" + total);
// create a list with duplicates
List<Book> dupBooks = Arrays.asList(book1, book2, book3, book1, book2);
System.out.println("\n" + "Before removing duplicates: ");
System.out.println(dupBooks.toString());
Collection<Book> noDups = new HashSet<>(dupBooks);
System.out.println("\n" + "After removing duplicates: ");
System.out.println(noDups.toString());
// aggregate author first names into a list
List<String> list = books.stream().map(Book::getAuthorLName).collect(Collectors.toList());
System.out.println("\n" + list);
// example of using Set to eliminate dups and sort automatically
Set<Integer> numbers = new HashSet<>(asList(4, 3, 3, 3, 2, 1, 1, 1));
System.out.println("\n" + numbers.toString());
}
}
class Book {
private String title;
private String authorFName;
private String authorLName;
private int pages;
public Book(String title, String authorFName, String authorLName, int pages) {
this.title = title;
this.authorFName = authorFName;
this.authorLName = authorLName;
this.pages = pages;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthorFName() {
return authorFName;
}
public void setAuthorFName(String authorFName) {
this.authorFName = authorFName;
}
public String getAuthorLName() {
return authorLName;
}
public void setAuthorLName(String authorLName) {
this.authorLName = authorLName;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public String toString() {
return getTitle() + " Written By: " + getAuthorFName() + " " + getAuthorLName() + "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment