The simplest realization of Chinese character sorting

public static void main(String[] args) {
        List<String> strings = Arrays.asList("张三", "王五", "安娜", "李四", "张志东", "杨天华");

        List<String> sorted = strings.stream().sorted(Collator.getInstance(Locale.CHINESE)).collect(Collectors.toList());
        System.out.println("自然正序排序"+JSON.toJSON(sorted));

        List<String> sorted1 = strings.stream().sorted(Collections.reverseOrder(Collator.getInstance(Locale.CHINESE))).collect(Collectors.toList());
        System.out.println("汉子倒序排序"+JSON.toJSON(sorted1));
    }


输出结果:
自然正序排序["安娜","李四","王五","杨天华","张三","张志东"]
汉子倒序排序["张志东","张三","杨天华","王五","李四","安娜"]

Lambda expression 4: MapReduce development case of java8

Introduction

Implementing the MapReduce tool through the Stream interface in Lambda, a simple understanding is similar to the grouping statistics tool in SQL, except that MapReduce is a final statistical operation that can be processed for each data + set.

Specific content

No matter how the collection changes, it must be a dynamic array, so the entire MapReduce operation is done around objects.
Example: Define a shopping cart class, there will be multiple Car class objects stored in the collection class

public class Car {

    private String pname;
    private Integer amouter;
    private Double price;

    public Car(String pname, Integer amouter, Double price) {
        super();
        this.pname = pname;
        this.amouter = amouter;
        this.price = price;
    }
    public Car() {
        super();
    }
    public String getPname() {
        return pname;
    }
    public void setPname(String pname) {
        this.pname = pname;
    }
    public Integer getAmouter() {
        return amouter;
    }
    public void setAmouter(Integer amouter) {
        this.amouter = amouter;
    }
    public Double getPrice() {
        return price;
    }
    public void setPrice(Double price) {
        this.price = price;
    }
}

Use Map() to process data separately

Example: Separate processing of data

public class TestDemo {
    public static void main(String[] args) {
        List<Car> all=new ArrayList<Car>();
        all.add(new Car("java",200,20.8));
        all.add(new Car("ios",200,10.8));
        all.add(new Car("c",200,2.8));
        all.add(new Car("c++",200,10.8));
        all.add(new Car("mongo",200,10.8));
        all.add(new Car("android",200,12.8));
        all.add(new Car("oracle",20,8.8));
        all.stream().map(car->{
            System.out.print("书名:"+car.getPname()+" 总价:");
            return car.getAmouter()*car.getPrice();
        }).forEachOrdered(System.out::println);
    }
}

The output result is as follows:
Title: c++ Total price: 2160.0
Title: mongo Total price: 2160.0
Book title: android Total price: 2560.0
Book title: Oracle Total price: 176.0

As can be seen from the above code, the function of the map() method is to process each data in the collection.

Use Reduce() to turn all the data in the collection into one result

If you use the map() method to recombine the data, then reduce() will turn all the data in the collection into one result, just like the functions of sum(), avg(), count() in SQL.
reduce() method: public final Optional<P_OUT> reduce(BinaryOperator<P_OUT> accumulator)
example, to realize the comprehensive operation of purchasing goods

public class TestDemo {
    public static void main(String[] args) {
        List<Car> all=new ArrayList<Car>();
        all.add(new Car("java",200,20.8));
        all.add(new Car("ios",200,10.8));
        all.add(new Car("c",200,2.8));
        all.add(new Car("c++",200,10.8));
        all.add(new Car("mongo",200,10.8));
        all.add(new Car("android",200,12.8));
        all.add(new Car("oracle",20,8.8));
        double result =all.stream().map(car->{
            return car.getAmouter()*car.getPrice();
        }).reduce((sum,carPrice)->sum+carPrice).get();
        System.out.println("购买总金额"+result);
    }
}

The code running results are as follows: the
total purchase amount is 13936.0

Map and Reduce operate together

If you want to perform statistics, it may include: sum, maximum, minimum, average, quantity
. Corresponding operations are provided in the Stream interface:

  • Processing double data:DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper)
  • Processing int operations:IntStream mapToInt(ToIntFunction<? super T> mapper)
  • Processing long operations:LongStream mapToLong(ToLongFunction<? super T> mapper)

    The following statistical operation methods are provided in each returned interface:

  • Double data statistics (DoubleStream): public DoubleSummaryStatistics summaryStatistics()

  • Int data statistics (IntStream): public IntSummaryStatistics summaryStatistics()
  • Long data statistics (LongStream): public LongSummaryStatistics summaryStatistics()
    These classes provide a series of getXxx() methods for statistics related information.
    Example: Implement the reduce function
public class TestDemo {
    public static void main(String[] args) {
        List<Car> all=new ArrayList<Car>();
        all.add(new Car("java",200,20.8));
        all.add(new Car("ios",200,10.8));
        all.add(new Car("c",200,2.8));
        all.add(new Car("c++",200,10.8));
        all.add(new Car("mongo",200,10.8));
        all.add(new Car("android",200,12.8));
        all.add(new Car("oracle",20,8.8));
        DoubleSummaryStatistics result=all.stream().mapToDouble(myCar->{
            return myCar.getAmount()*myCar.getPrice();
        }).summaryStatistics();
        System.out.println("统计量: "+result.getCount());
        System.out.println("最大值: "+result.getMax());
        System.out.println("最小值: "+result.getMin());
        System.out.println("总和: "+result.getSum());
        System.out.println("平均值: "+result.getAverage());
    }
}

Output value:
Statistics: 7
Maximum: 4160.0
Minimum: 176.0
Sum: 13936.0
Average: 1990.857142857143

The whole process is the MapReduce analysis method used in Mongodb.

 

 

Guess you like

Origin blog.csdn.net/yangguoqi/article/details/106742343