Java8 practical Lambda and Stram API learning

Source: Internet
Author: User

Public class trader{
    
private String name;
private String City;

Public Trader (string n, string c) {
this.name = n;
this.city = c;
    }

Public String GetName () {
return this.name;
    }

Public String getcity () {
return this.city;
    }

Public void Setcity (String newcity) {
this.city = newcity;
    }

Public String toString () {
return "Trader:" +this.name + "in" + this.city;
    }
}

public class transaction{

Private Trader Trader;
private int year;
private int value;

Public Transaction (Trader Trader, int year, int value)
{
This.trader = Trader;
This.year = year;
This.value = value;
}

Public Trader Gettrader () {
return this.trader;
}

public int getYear () {
return this.year;
}

public int GetValue () {
return this.value;
}

Public String toString () {
Return "{" + This.trader + "," +
"Year:" +this.year+ "," +
"Value:" + This.value + "}";
}
}

public class Puttingintopractice {
public static void Main (String ... args) {
Trader Raoul = new Trader ("Raoul", "Cambridge");
Trader Mario = new Trader ("Mario", "Milan");
Trader Alan = new Trader ("Alan", "Cambridge");
Trader brian = new Trader ("Brian", "Cambridge");

list<transaction> transactions = arrays.aslist (New Transaction (Brian,), New Transaction (Raoul, 2012, (+), new Transaction (Raoul, +), new Transaction (Mario, 710), New Transaction (Mario, +), new Transa Ction (Alan, 2012, 950));

Query 1:find All transactions from year, and sort them by value (small to high).
list<transaction> tr2011 = Transactions.stream (). Filter (Transaction, transaction.getyear () = =). Sorted (Comparing (Transaction::getvalue)). Collect (ToList ());
System.out.println (tr2011);

Query 2:what is all the unique cities where the traders work?
List<string> cities = Transactions.stream (). Map (transaction-Transaction.gettrader (). getcity ()). Distinct (). Collect (ToList ());
SYSTEM.OUT.PRINTLN (cities);

Query 3:find all traders from Cambridge and sort them by name.
List<trader> traders = Transactions.stream (). Map (Transaction::gettrader). Filter (Trader, trader.getcity () . Equals ("Cambridge")). Distinct (). Sorted (Comparing (Trader::getname)). Collect (ToList ());
SYSTEM.OUT.PRINTLN (traders);

Query 4:return A string of all traders ' names sorted alphabetically.
String traderstr = Transactions.stream (). Map (transaction-Transaction.gettrader (). GetName ()). Distinct (). Sorted (). Reduce ("", (n1, N2), N1 + N2);
System.out.println (TRADERSTR);

Query 5:are there any trader based in Milan?
Boolean milanbased = Transactions.stream (). AnyMatch (Transaction-Transaction.gettrader (). Getcity (). Equals (" Milan "));
System.out.println (milanbased);

Query 6:update All transactions So, the traders from Milan is set to Cambridge.
Transactions.stream (). Map (Transaction::gettrader). Filter (Trader-trader.getcity (). Equals ("Milan")). ForEach ( Trader-Trader.setcity ("Cambridge"));
System.out.println (transactions);

Query 7:what ' s the highest value in all the transactions?
int highestvalue = Transactions.stream (). Map (Transaction::getvalue). Reduce (0, Integer::max);
System.out.println (Highestvalue);
}
}

Java8 practical Lambda and Stram API learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.