Created
January 29, 2014 09:51
-
-
Save forax/8684788 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.time.DayOfWeek; | |
import java.time.LocalDate; | |
import java.time.LocalDateTime; | |
import java.time.temporal.Temporal; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.function.Function; | |
import java.util.stream.Collectors; | |
public class HeightIn8 { | |
static class Person { | |
private final String name; | |
public Person(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
@Override | |
public String toString() { | |
return name + ' ' + Integer.toHexString(super.hashCode()); | |
} | |
} | |
public static void main(String[] args) throws IOException { | |
// 1 | |
List<String> list = Arrays.asList(args); | |
list.sort((s1, s2) -> s1.compareToIgnoreCase(s2)); | |
list.sort(String::compareToIgnoreCase); | |
// 2 | |
/*Iterator<String> it = list.iterator(); | |
while(it.hasNext()) { | |
String s = it.next(); | |
if (s.length() %2 == 0) { | |
it.remove(); | |
} | |
}*/ | |
list.removeIf(s -> s.length() %2 == 0); | |
// 3 | |
Map<String, Long> map = new HashMap<>(); | |
for(String s: args) { | |
map.put(s, 1 + map.getOrDefault(s, 0L)); | |
} | |
// 4 | |
List<Person> people = Arrays.asList(new Person("Paul"), new Person("John"), new Person("Paul")); | |
Map<String, List<Person>> byNameMap = new HashMap<>(); | |
for(Person person: people) { | |
byNameMap.computeIfAbsent(person.getName(), name -> new ArrayList<>()).add(person); | |
} | |
// 5 | |
//Map<String, List<Person>> byNameMap = ... | |
byNameMap.forEach((name, persons) -> { | |
System.out.println(name + ' ' + persons); | |
}); | |
// 6 | |
Map<String, List<Person>> byNameMap2 = | |
people.stream().collect(Collectors.groupingBy(Person::getName)); | |
System.out.println(byNameMap2); | |
// 7 | |
Path dictionnary = Paths.get("dict.txt"); | |
Map<String, Long> histoMap = | |
Files.lines(dictionnary) | |
.flatMap(line -> Arrays.stream(line.split(" "))) | |
.collect(Collectors.groupingBy(Function.identity(), | |
Collectors.counting())); | |
System.out.println(histoMap); | |
// 8 | |
List<Temporal> temporals = Arrays.asList(LocalDate.now(), LocalDateTime.now()); | |
temporals.stream().map(temporal -> temporal.query(DayOfWeek::from)).forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great post. I noticed two bugs in your snippets:
should be
Otherwise you would end up with an empty map, if I'm not mistaken, since
getOrDefault()
doesn't put back the default value in the map automatically likecomputeIfAbsent()
does.And
should of course be
which, BTW, makes it much less readable than the Java 8 version.