Skip to content

Instantly share code, notes, and snippets.

@forax
Created January 29, 2014 09:51
Show Gist options
  • Save forax/8684788 to your computer and use it in GitHub Desktop.
Save forax/8684788 to your computer and use it in GitHub Desktop.
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);
}
}
@jnizet
Copy link

jnizet commented Feb 5, 2014

Great post. I noticed two bugs in your snippets:

for(Person person: people) {
    byNameMap.getOrDefault(person.getName(), new ArrayList<>()).add(person);
}

should be

for(Person person: people) {
    List<Person> persons = byNameMap.getOrDefault(person.getName(), new ArrayList<>());
    persons.add(person);
    byNameMap.put(persons);
}

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 like computeIfAbsent() does.

And

for(Map.Entry<String, List<Person>> entry: byNameMap.entrySet()) {
    System.out.println(name + ' ' + persons);
}

should of course be

for(Map.Entry<String, List<Person>> entry: byNameMap.entrySet()) {
    System.out.println(entry.getKey() + ' ' + entry.getValue());
}

which, BTW, makes it much less readable than the Java 8 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment