0% found this document useful (0 votes)
14 views1 page

Default Method

The document discusses several topics related to Java interfaces and streams: 1. It defines functional interfaces Function and Predicate that allow default methods like andThen() and or(). 2. It shows a generic interface I with a type parameter method foo() and how default and static methods can access type parameters. 3. It defines a Range class to track min and max double values with methods like add() and join(). 4. A RangeTest class demonstrates using streams to find the min, max, and range of double values from a file. 5. A TemperatureStatistics class uses streams to calculate statistics from a list of daily temperatures, such as average, max value, counts above a threshold,

Uploaded by

nino nakashidze
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

Default Method

The document discusses several topics related to Java interfaces and streams: 1. It defines functional interfaces Function and Predicate that allow default methods like andThen() and or(). 2. It shows a generic interface I with a type parameter method foo() and how default and static methods can access type parameters. 3. It defines a Range class to track min and max double values with methods like add() and join(). 4. A RangeTest class demonstrates using streams to find the min, max, and range of double values from a file. 5. A TemperatureStatistics class uses streams to calculate statistics from a list of daily temperatures, such as average, max value, counts above a threshold,

Uploaded by

nino nakashidze
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

,

1. functional interface Default method:

 public interface Function {


 R apply(T t);
 default Function andThen(Function after) {
 return t -> after.apply(apply(t));
 }
 default Function compose(Function before) {
 return v -> apply(before.apply(v));
 }
 static Function identity() {
 return t -> t;
 }}
 public interface Predicate {
boolean test(T t);
 default Predicate and(Predicate other) {
 return t -> test(t) && other.test(t);
 }
 default Predicate or(Predicate other) {
 return t -> test(t) || other.test(t);
 }
 static Predicate not(Predicate target) {
 return t -> !target.test(t);
 }}

2. generic interface I with the method T foo()

 interface I {
 T foo();
 static boolean batz(I x) {
 return x.foo() != null;
 }
 default String name() { if (batz(this))
 return foo().toString();
 return "Sorry!";
 }}
 public class B implements I {
 private String story; public B(String s) {
 story = s;
 }
 public String foo() {
 return story;
 }}

3. private attributes max and min

 class Range {
 private double max;
 private double min;
 public double diff() {
 return max-min;
 }
 public Range() {
 max = -Double.MAX_VALUE;
 min = Double.MAX_VALUE;
 } public void add(double x) {
 if (x > max) max = x;
 if (x < min) min = x;
 }
 public void join(Range r) {
 if (max < r.max) max = r.max;
 if (min > r.min) min = r.min; } }

4. class RangeTest
 class RangeTest {
 public static void main(String[] a) throws IOException {
 BiConsumer acc = (Range r, Double d) -> r.add(d);
 BiConsumer comb = (Range r, Range r1) -> r.join(r1);
 Stream ss = Files.lines(Path.of(a[0]));
 double diff = 0.0;
 double max = ss.mapToDouble(p ->
Double.parseDouble(p)).filter(number -> number > -42.0 || number <
42.0).max().getAsDouble();
 double min = ss.mapToDouble(p ->
Double.parseDouble(p)).filter(number -> number > -42.0 || number <
42.0).min().getAsDouble();
 diff = max - min; System.out.println("The range of values is \t"+diff);
 }}

5. methods using Streams

 public class TemperatureStatistics {


 private List dailyTemperatures;
 public TemperatureStatistics(List dailyTemperatures) {
 this.dailyTemperatures = dailyTemperatures;
 }
 public double calculateAverageTemperature() {
 return dailyTemperatures.stream()
 .mapToDouble(Integer::doubleValue)
 .average() .orElse(0.0);
 }
 public int findMaxTemperature() {
 return dailyTemperatures.stream()
 .mapToInt(Integer::intValue)
 .max()
 .orElse(0);
 }
 public long countDaysAboveThreshold(int threshold) {
 return dailyTemperatures.stream()
 .filter(temperature -> temperature > threshold)
 .count();
 }
 public boolean areAllTemperaturesBelowThreshold(int threshold) {
 return dailyTemperatures.stream()
 .allMatch(temperature -> temperature < threshold);
 }
 public boolean containsTemperature(int value) {
 return dailyTemperatures.stream()
 .anyMatch(temperature -> temperature == value);
 }}

You might also like