Java: Aggregation and java Aggregation
What is the purpose of using a set? Of course, it is not for simple storage, but for data extraction and operation from a set.
Consider the backpack class mentioned in the previous article again,
To print the weight of all backpacks,
for(Package p : packages) System.out.println(p.getWeight());
Traversal, you can use the "forEach" aggregation operation,
packages.stream() .forEach(e -> System.out.println(p.getWeigh());
It doesn't matter if you don't understand the syntax. Let's make it a little more complex,
We only print "the weight of a backpack is greater than 20 ",
packages.stream() .filter(e -> e.getWeight() > 20) .forEach(e - > System.out.println(e.getWeight());
Analyze its structure,
Packages is a set. The stream () method is to get its "stream". It can generate not only a set, an array, but also an I/O channel. The following is the pipeline operation (similar to the pipeline operation in MongoDB or Linux. There are zero or several intermediate operations, such as the filter here to generate a new stream, and then there is an end operation, which can return values (no longer a stream ), or no return value.
The filter function is prototype:
Stream<T> filter<Predicate<? super T> p)
Accept a predicate parameter (condition ).
Check the APIS under java. util. strem. There are many operations, as shown in the following section,
The specific usage is not described in detail.
Let's look at another requirement and find "average weight of a backpack with a weight greater than 20 ".
double average = packages .stream() .filter(e -> e.getWeight() > 20) .mapToInt(e -> e.getWeigh()) .average() .getAsDouble();
In addition to filters, maptoInt is used to filter data, and an IntStream is returned.
The function prototype is,
IntStream maptoInt(toIntFunction<? super T> mapper)
The parameter is a lambda expression that returns an Int.
Average () is the IntStream method. Obviously, it is the average value. An OptionalDouble is returned. What is OptionalDouble? Why not return Double directly !! OptionalDouble is newly added to Java 8 and has many similar APIs. It features that this object may or may not contain a value. The function is that the traditional method returns a Null value, and the NullException is returned for the continue operation. If OptionalXX has no value, the operation can continue (of course, The NoSuchElement exception will be reported if there is no value for getAsDouble.
In addition, the average termination operation is also a callback operation.
In JDK, operations such as average, sum, and min return a value through the content of the combined stream. These operations are called Operation operations. Of course, a certain ction operation returns a set instead of a value. It is worth mentioning that JDK also provides two general operations: reduce and collect.
Suppose we need To sum the size of the backpack,
Integer total = packages .stream() .mapToInt(e -> e,getWeight()) .sum();
If you want to use reduce to transform it,
Integer total = packages .stream() .mapToInt(e -> e,getWeight()) .reduce(0,(a,b) -> a + b);
If function reference is used,
Integer total = packages .stream() .mapToInt(e -> e,getWeight()) .reduce(0,Integer::sum);
View the reduce function prototype:
T reduce(T indentidy,BinaryOperator<T> accumulator)
This may be confusing,
The following describes the API: using the provided identity value and an associative accumulation function.
First look at associative, "associicity", that is, to meet,
a op b op c = a op (b op c)
Such as addition, minimum value, maximum value, and character concatenation all have this property.
The API also says that the reduce function is equivalent,
T result = identity; for (T element : this stream) result = accumulator.apply(result, element) return result;
What is identy? For all t of the set of operations, there should be
accumulator.apply(identity, t) = t
Yes, it is the 1 yuan mentioned in discrete mathematics. For addition, 1 RMB is 0.
The reduce operation always returns a new value. However, the accumulation function returns a new value each time it processes an element in the stream. If you want to make the stream reduce into a set, each time you add an element to process it, a new combination will be generated. This is obviously a defect in personal performance and is very inefficient. In this case, you can consider updating an existing set. This is what the collect method does.
Next, we will introduce the Usage Details of collect.
Java, bit operations?
Minus one ~
Reverse operation ~
Refer to the questions I have answered before
Reference: z.baidu.com/question/75765111.html
JAVA text operations
Not very detailed ...... This rule means that a Chinese character can be counted as one, and a non-Chinese character can be counted as 0.5. According to the requirements of a maximum of 19 Chinese characters per line, in fact, there is also a possibility to wrap 18.5 characters (18 Chinese characters plus one punctuation plus one Chinese character will exceed, so 18.5 Chinese characters will have to be wrapped). According to this understanding, the Code is as follows:
Import java. io. BufferedWriter;
Import java. io. FileInputStream;
Import java. io. FileWriter;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. io. Reader;
Import java. util. regex. Matcher;
Import java. util. regex. Pattern;
Public class t1 {
Public static void main (String args []) throws IOException {
String path1 = "F :\\ test1.txt ";
String path2 = "F: \ test2.txt ";
Pattern p1 = Pattern. compile ("[\ u4e00-\ u9fa5]");
Reader r1 = new InputStreamReader (new FileInputStream (path1 ));
BufferedWriter bw = new BufferedWriter (new FileWriter (path2 ));
Int c = 0;
Float count = 0;
StringBuffer bfStr = new StringBuffer ();
While (c = r1.read ())! =-1 ){
String str = (char) c + "";
Matcher m = p1.matcher (str );
If (m. matches ()){
Count ++;
} Else {
Count + = 0.5;
}
If (char) c = '\ R '){
Bw. write (bfStr. toString ());
Bw. newLine ();
BfStr = new StringBuffer ();
Count = 0;
} Else {
If (count> 19 ){
Bw. write (bfStr. toString ());
Bw. newLine ();
BfStr = new StringBuffer ();
BfStr. append (str );
Count = 0;
} Else {
BfStr. append (str );
}
}
}
If (bfStr. toString ()! = ""){
Bw. write (bfStr. toString ());
}
Bw. close ();
}
}
The original text line feed format is reserved for you. Generally, this will not let you change it. You have not made it clear ...... Remaining full text>