Test code:
MapTest maptest = new MapTest();
List<Country> testdata = maptest.getCountry();
Map<String, Country> result1 = maptest.getCountryDataMap(testdata);
Map<String, Country> result2 = maptest.getCountryDataMap2(testdata);
Stream<Country> temp = testdata.stream();
Predicate<Country> match = country -> country.getCode().equals("US");
Stream<Country> result = temp.filter(match);
System.out.println("Size 2: " + result.count());
List<Country> filterResult = testdata.stream().filter((country) -> country.getCode().equals("US")).collect(Collectors.toList());
System.out.println("size: " + filterResult.size());
filterResult.forEach(System.out::println);
System.out.println(maptest.assertEqual2(result1, result2));
Copy the code
Source code for Predicate:
/**
* Represents a predicate (boolean-valued function) of one argument.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #test(Object)}.
*
* @param <T> the type of the input to the predicate
*
* @since1.8 * /
Copy the code
Predicate takes a type as an argument and returns a Boolean value.
So I can use this arrow Function on line 113, where country is the arrow Function or the formal argument (input argument) in Lambda Function, and because the Predicate takes the generic argument t-country, the compiler can tell, The type of the formal parameter must be Country.
Finally, collect is passed in. Collectors. ToList ().
The collect method applies the incoming Collector to the element of the stream,
Action for Collectors. ToList: Add elements from the stream to a List container and return.
Returns a Collector that accumulates the input elements into a new List. There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned
Complete test code:
package java8.forHybrisCodeReview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Country{
private String mCountryCode;
private int mRank;
public Country(String mCode, int rank){
this.mCountryCode = mCode;
this.mRank = rank;
}
public String getCode(a){
return this.mCountryCode;
}
@Override
public boolean equals(Object other) {
if( other == null)
return false;
if ( !(other instanceof Country) )
return false;
Country country = (Country)other;
return this.mCountryCode.equals(country.mCountryCode) && this.mRank == country.mRank;
}
@Override
public int hashCode(a) {
return this.mCountryCode.hashCode()*37 + this.mRank; }}public class MapTest{
public List<Country> getCountry(a){
List<Country> countries = new ArrayList<Country>();
Country country = new Country("ZH".1);
countries.add(country);
country = new Country("US".2);
countries.add(country);
country = new Country("JP".3);
countries.add(country);
return countries;
}
public Map<String, Country> getCountryDataMap(List<Country> countryList)
{
final Map<String, Country> countryDataMap = new HashMap<>();
for (final Country countryData : countryList){
countryDataMap.put(countryData.getCode(), countryData);
}
return countryDataMap;
}
public Map<String, Country> getCountryDataMap2(List<Country> countryList)
{
final Map<String, Country> countryDataMap = new HashMap<>();
Consumer<Country> consumer = c -> countryDataMap.put(c.getCode(), c);
countryList.forEach(consumer);
return countryDataMap;
}
// forEach is called an internal traverser because once it has started, the traversal cannot be easily interrupted.
public boolean assertEqual(Map<String, Country> map1, Map<String, Country> map2){
if( map1.size() ! = map2.size())return false;
final boolean equal = true;
map1.forEach((key, value)->
{
System.out.println("key of map1:" + key);
Country country = map2.get(key);
if( !value.equals(country)){
}
// Void methods cannot return a value
// return false;
// equal = false; // cannot change final
});
return equal;
}
public boolean assertEqual2(Map<String, Country> map1, Map<String, Country> map2){
if( map1.size() ! = map2.size())return false;
for (Map.Entry<String,Country> entry : map1.entrySet()) {
String key = entry.getKey();
Country country = entry.getValue();
Country country2 = map2.get(key);
if( !country.equals(country2))
return false;
}
return true;
}
public static void main(String[] arg){
MapTest maptest = new MapTest();
List<Country> testdata = maptest.getCountry();
Map<String, Country> result1 = maptest.getCountryDataMap(testdata);
Map<String, Country> result2 = maptest.getCountryDataMap2(testdata);
Stream<Country> temp = testdata.stream();
Predicate<Country> match = country -> country.getCode().equals("US");
Stream<Country> result = temp.filter(match);
System.out.println("Size 2: " + result.count());
List<Country> filterResult = testdata.stream().filter((country) -> country.getCode().equals("US")).collect(Collectors.toList());
System.out.println("size: "+ filterResult.size()); filterResult.forEach(System.out::println); System.out.println(maptest.assertEqual2(result1, result2)); }}Copy the code
For more of Jerry’s original articles, please follow the public account “Wang Zixi “: