improvements 7

generic-observer 7
Lisa 7 years ago
parent a5f57c0d4e
commit 67280ac2cd

@ -5,25 +5,24 @@ public class LambdaTest {
public static void main(String[] args) { public static void main(String[] args) {
Function<Double, Double> id = (x) -> x; Function<Double, Double> id = (x) -> x;
Function<Double,Double> inverse = (x) -> x*-1; Function<Double, Double> inverse = (x) -> -x;
Function<Double,Double> timesTen = (x) -> x*10; Function<Double, Double> timesTen = (x) -> 10*x;
Function<Double, Double> divideByPi = (x) -> x/Math.PI; Function<Double, Double> divideByPi = (x) -> x/Math.PI;
Function<Double, Long> round = Math::round; Function<Double, Long> round = Math::round;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Function<Double, Double> chain = makeChain(new Function[]{ inverse, id, timesTen, divideByPi }); Function<Double, Double> chain = makeChain(new Function[]{ inverse, id, timesTen, divideByPi });
System.out.println(round.calculate((chain.calculate(5.5)))); System.out.println(round.calculate(chain.calculate(5.5)));
} }
private static Function<Double, Double> makeChain(final Function<Double, Double>[] functions) { private static Function<Double, Double> makeChain(final Function<Double, Double>[] functions) {
return (x) -> { return (x) -> {
for (Function<Double, Double> i : functions) { for (Function<Double, Double> f : functions) {
x = i.calculate(x); x = f.calculate(x);
} }
return x; return x;
}; };

@ -5,16 +5,7 @@ import java.util.stream.Stream;
public class StreamTest { public class StreamTest {
public static void main(String[] args) { public static void main(String[] args) {
Stream<Integer> naturals = Stream.iterate(1, i -> i + 1); Stream<Integer> naturals = Stream.iterate(1, i -> i + 1);
Stream<Integer> integers = Stream.iterate(0, i -> i > 0 ? -i : -i + 1);
Stream<Integer> integers = Stream.iterate(0, i -> {
int next;
if (i>0) {
next = i*(-1);
} else {
next = i*(-1)+1;
}
return next;
});
System.out.println(filterAndSum(naturals)); System.out.println(filterAndSum(naturals));
System.out.println(filterAndSum(integers)); System.out.println(filterAndSum(integers));
@ -23,7 +14,7 @@ public class StreamTest {
private static Integer filterAndSum(Stream<Integer> stream) { private static Integer filterAndSum(Stream<Integer> stream) {
return stream.filter((i) -> i % 2 == 0) return stream.filter((i) -> i % 2 == 0)
.limit(10) .limit(10)
.reduce((a,b) -> a+b) .reduce(Integer::sum)
.orElse(0); .orElse(0);
} }
} }

@ -1,6 +1,9 @@
package _7._3; package _7._3;
import java.io.*; import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -9,29 +12,37 @@ class Person implements Serializable {
private String lastname; private String lastname;
private transient String sortname; private transient String sortname;
public Person() { } public Person() {
}
public Person(String firstname, String lastname) { public Person(String firstname, String lastname) {
this.firstname = firstname; this.firstname = firstname;
this.lastname = lastname; this.lastname = lastname;
updateSortname(); updateSortname();
} }
public String getFirstname() { public String getFirstname() {
return firstname; return firstname;
} }
public void setFirstname(String firstname) { public void setFirstname(String firstname) {
this.firstname = firstname; this.firstname = firstname;
updateSortname(); updateSortname();
} }
public String getLastname() { public String getLastname() {
return lastname; return lastname;
} }
public void setLastname(String lastname) { public void setLastname(String lastname) {
this.lastname = lastname; this.lastname = lastname;
updateSortname(); updateSortname();
} }
public String getSortname() { public String getSortname() {
return sortname; return sortname;
} }
public void updateSortname() { public void updateSortname() {
sortname = lastname + firstname; sortname = lastname + firstname;
} }
@ -43,30 +54,34 @@ class Person implements Serializable {
public static List<Person> load(String filename) throws IOException { public static List<Person> load(String filename) throws IOException {
List<Person> people = new ArrayList<>(); try(DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(filename)))) {
try (DataInputStream in = new DataInputStream(new BufferedInputStream( return loadAll(in);
new FileInputStream(filename)))) {
boolean nextExists = in.readBoolean();
while (nextExists) {
people.add(load(in));
nextExists = in.readBoolean();
} }
} }
return people;
public static List<Person> loadAll(DataInputStream in) throws IOException {
int size = in.readInt();
List<Person> persons = new ArrayList<>();
for (int i = 0; i < size; i++) {
persons.add(load(in));
}
return persons;
} }
public static Person load(DataInputStream in) throws IOException { public static Person load(DataInputStream in) throws IOException {
return new Person(in.readUTF(), in.readUTF()); return new Person(in.readUTF(), in.readUTF());
} }
public static void save(String filename, List<Person> list) throws IOException { public static void save(String filename, List<Person> list) throws IOException {
try(DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)))) { try(DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)))) {
for (Person p : list) { saveAll(out, list);
out.writeBoolean(true);
save(out, p);
} }
out.writeBoolean(false); }
public static void saveAll(DataOutputStream out, List<Person> persons) throws IOException {
out.writeInt(persons.size());
for (Person person : persons) {
save(out, person);
} }
} }
@ -78,16 +93,13 @@ class Person implements Serializable {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static List<Person> unserialize(String filename) throws IOException, ClassNotFoundException { public static List<Person> unserialize(String filename) throws IOException, ClassNotFoundException {
List<Person> people;
try (ObjectInputStream in = new ObjectInputStream( try (ObjectInputStream in = new ObjectInputStream(
new BufferedInputStream(new FileInputStream(filename)))) { new BufferedInputStream(new FileInputStream(filename)))) {
people = (List<Person>) in.readObject(); List<Person> people = (List<Person>) in.readObject();
for (Person p : people) { people.forEach(Person::updateSortname);
p.updateSortname();
}
}
return people; return people;
} }
}
public static void serialize(String filename, List<Person> persons) throws IOException { public static void serialize(String filename, List<Person> persons) throws IOException {
try (ObjectOutputStream out = new ObjectOutputStream( try (ObjectOutputStream out = new ObjectOutputStream(

Loading…
Cancel
Save