Contenu connexe Similaire à Let's go Developer 2011 sendai Let's go Java Developer (Programming Language Groovy Part) (20) Plus de Uehara Junji (20) Let's go Developer 2011 sendai Let's go Java Developer (Programming Language Groovy Part)2. Java
2011 05/28
Slide # 2
2011 5 28
3. Java
2011 05/28
Slide # 3
2011 5 28
4. $ cat input.txt
That that is is that that is
not is not is that it it is
$ java WordCount input.txt
1: [That]
2: [not]
2: [it]
4: [that]
6: [is] Java
2011 05/28
Slide # 4
2011 5 28
5. Java WordCount:48 Set<Map.Entry<String, Integer>> entrySet =
import java.util.Comparator; map.entrySet();
import java.util.HashMap; Object[] list = entrySet.toArray();
import java.util.Map; Comparator comp = new Comparator(){
import java.util.Set; public int compare(Object o1, Object o2)
import java.util.List; {
import java.util.Arrays; Map.Entry<String, Integer> e1 =
import java.io.FileReader; (Map.Entry<String, Integer>) o1;
import java.io.BufferedReader; Map.Entry<String, Integer> e2 =
import java.io.FileNotFoundException; (Map.Entry<String, Integer>) o2;
import java.io.IOException; return e1.getValue() - e2.getValue();
}
public class WordCount { };
@SuppressWarnings(value = "unchecked") Arrays.sort(list, comp);
public static void main(String[] args) { for (Object it: list) {
FileReader fis = null; Map.Entry<String, Integer> entry =
BufferedReader br = null; (Map.Entry<String, Integer>)it;
try { System.out.println(entry.getValue() + ":
HashMap<String, Integer> map = new ["+entry.getKey()+"]");
HashMap<String, Integer>(); }
fis = new FileReader(args[0]); }
br = new BufferedReader(fis); catch (IOException e) {
String line; try {if (br != null) br.close();}catch
while ((line = br.readLine()) != null) { (IOException ioe){}
for (String it: line.split("s+")) { try {if (fis != null)fis.close();}catch
map.put(it, (map.get(it)==null) ? 1 : (IOException ioe){}
(map.get(it) + 1)); e.printStackTrace();
} }
Java
} }
}
2011 05/28
Slide # 5
2011 5 28
6. Groovy WordCount(9 )
def map = [:].withDefault{0}
new File(args[0]).eachLine {
it.split(/s+/).each {
map[it]++
}
}
map.entrySet().sort{it.value}.each {
println "${it.value}: [${it.key}]"
}
Java
2011 05/28
Slide # 6
2011 5 28
7. Java
Set<Map.Entry<String, Integer>>
import java.util.Comparator; entrySet = map.entrySet();
import java.util.HashMap; Object[] list = entrySet.toArray();
import java.util.Map; Comparator comp = new Comparator(){
import java.util.Set; public int compare(Object o1,
import java.util.List; Object o2) {
import java.util.Arrays; Map.Entry<String, Integer> e1
import java.io.FileReader; = (Map.Entry<String, Integer>) o1;
import java.io.BufferedReader; Map.Entry<String, Integer> e2
import java.io.FileNotFoundException; = (Map.Entry<String, Integer>) o2;
import java.io.IOException; return e1.getValue() -
e2.getValue();
public class WordCount { }
@SuppressWarnings(value = "unchecked") };
public static void main(String[] args) { Arrays.sort(list, comp);
FileReader fis = null; for (Object it: list) {
BufferedReader br = null; Map.Entry<String, Integer> entry
try { = (Map.Entry<String, Integer>)it;
HashMap<String, Integer> map = new System.out.println(entry.getValue
HashMap<String, Integer>(); () + ": ["+entry.getKey()+"]");
fis = new FileReader(args[0]); }
br = new BufferedReader(fis); }
String line; catch (IOException e) {
while ((line = br.readLine()) != try {if (br != null) br.close();}
null) { catch(IOException ioe){}
for (String it: line.split("s try {if (fis != null)fis.close();}
+")) { catch(IOException ioe){}
map.put(it, (map.get(it) e.printStackTrace();
==null) ? 1 : (map.get(it) + 1));
}
}
}
Java
} } 2011 05/28
Slide # 7
2011 5 28
8. Groovy WordCount(9 )
def map = [:].withDefault{0} // value 0 map
new File(args[0]).eachLine { //
it.split(/s+/).each { // /s+/
map[it]++ // map 1
}
}
map.entrySet().sort{it.value}.each {// map entrySet value
println "${it.value}: [${it.key}]"// key,value
}
Java
2011 05/28
Slide # 8
2011 5 28
9. Groovy WordCount(9 )
def map = [:].withDefault{0} // value 0 map
new File(args[0]).eachLine { //
it.split(/s+/).each { // /s+/
map[it]++ // map 1
}
}
map.entrySet().sort{it.value}.each {// map entrySet value
println "${it.value}: [${it.key}]"// key,value
}
Java
2011 05/28
Slide # 9
2011 5 28
10. Groovy WordCount(9 )
def map = [:].withDefault{0} // value 0 map
new File(args[0]).eachLine { //
it.split(/s+/).each { // /s+/
map[it]++ // map 1
}
}
map.entrySet().sort{it.value}.each {// map entrySet value
println "${it.value}: [${it.key}]"// key,value
}
Java
2011 05/28
Slide # 10
2011 5 28
11. Groovy WordCount(9 )
def map = [:].withDefault{0} // value 0 map
new File(args[0]).eachLine { //
it.split(/s+/).each { // /s+/
map[it]++ // map 1
}
}
map.entrySet().sort{it.value}.each {// map entrySet value
println "${it.value}: [${it.key}]"// key,value
}
Java
2011 05/28
Slide # 11
2011 5 28
12. Groovy WordCount(9 )
def map = [:].withDefault{0} // value 0 map
new File(args[0]).eachLine { //
it.split(/s+/).each { // /s+/
map[it]++ // map 1
}
}
map.entrySet().sort{it.value}.each {// map entrySet value
println "${it.value}: [${it.key}]"// key,value
}
Java
2011 05/28
Slide # 12
2011 5 28
13. Groovy WordCount(9 )
def map = [:].withDefault{0} // value 0 map
new File(args[0]).eachLine { //
it.split(/s+/).each { // /s+/
map[it]++ // map 1
}
}
map.entrySet().sort{it.value}.each {// map entrySet value
println "${it.value}: [${it.key}]"// key,value
}
Java
2011 05/28
Slide # 13
2011 5 28
14. Groovy WordCount(9 )
def map = [:].withDefault{0} // value 0 map
new File(args[0]).eachLine { //
it.split(/s+/).each { // /s+/
map[it]++ // map 1
}
}
map.entrySet().sort{it.value}.each {// map entrySet value
println "${it.value}: [${it.key}]"// key,value
}
Java
2011 05/28
Slide # 14
2011 5 28
15. Java
2011 05/28
Slide # 15
2011 5 28
16. public final class Person { if (obj == null)
private final String firstName; return false;
private final String lastName; if (getClass() != obj.getClass())
return false;
public Person(String firstName, String lastName) { Person other = (Person) obj;
this.firstName = firstName; if (firstName == null) {
this.lastName = lastName; if (other.firstName != null)
} return false;
} else if (!firstName.equals(other.firstName))
public String getFirstName() { return false;
return firstName; if (lastName == null) {
} if (other.lastName != null)
return false;
public String getLastName() { } else if (!lastName.equals(other.lastName))
return lastName; return false;
} return true;
}
@Override
public int hashCode() { @Override
final int prime = 31; public String toString() {
int result = 1; return "Person(firstName:" + firstName
result = prime * result + ((firstName == null) + ", lastName:" + lastName + ")";
? 0 : firstName.hashCode()); }
result = prime * result + ((lastName == null)
? 0 : lastName.hashCode()); }
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
Java
2011 05/28
Slide # 16
2011 5 28
17. public final class Person { if (obj == null)
private final String firstName; return false;
private final String lastName; if (getClass() != obj.getClass())
return false;
public Person(String firstName, String lastName) { Person other = (Person) obj;
this.firstName = firstName; if (firstName == null) {
this.lastName = lastName; if (other.firstName != null)
} return false;
} else if (!firstName.equals(other.firstName))
public String getFirstName() { return false;
return firstName; if (lastName == null) {
} if (other.lastName != null)
return false;
public String getLastName() { } else if (!lastName.equals(other.lastName))
return lastName; return false;
} return true;
}
@Override
public int hashCode() { @Override
final int prime = 31; public String toString() {
int result = 1; return "Person(firstName:" + firstName
result = prime * result + ((firstName == null) + ", lastName:" + lastName + ")";
? 0 : firstName.hashCode()); }
result = prime * result + ((lastName == null)
? 0 : lastName.hashCode()); }
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
Java
2011 05/28
Slide # 16
2011 5 28
18. Groovy :4
@Immutable
class Person {
String firstName, lastName
}
Java
2011 05/28
Slide # 17
2011 5 28
19. Groovy :4
@Immutable
class Person {
String firstName, lastName
}
x = new Person("Junji","Uehara")
assert x.lastName == "Uehara"
x.firstName = "abc"
//==>
groovy.lang.ReadOnlyPropertyException:
Cannot set readonly property: firstName
Java
for class: Person 2011 05/28
Slide # 17
2011 5 28
20. Groovy :4
@Immutable
class Person {
String firstName, lastName
}
x = new Person("Junji","Uehara")
assert x.lastName == "Uehara"
x.firstName = "abc"
Java
2011 05/28
Slide # 18
2011 5 28
21. Groovy :4
@Immutable
class Person {
String firstName, lastName
}
x = new Person("Junji","Uehara")
assert x.lastName == "Uehara"
x.firstName = "abc"
Java
2011 05/28
Slide # 19
2011 5 28
22. Groovy :4
@Immutable
class Person {
String firstName, lastName
}
x = new Person("Junji","Uehara")
assert x.lastName == "Uehara"
x.firstName = "abc"
Java
2011 05/28
Slide # 20
2011 5 28
23. Java
2011 05/28
Slide # 21
2011 5 28
24. Java
2011 05/28
Slide # 21
2011 5 28
25. Java
2011 05/28
Slide # 21
2011 5 28
26. Java
2011 05/28
Slide # 21
2011 5 28
27. Java
2011 05/28
Slide # 21
2011 5 28
28. DSL
OS
PHP Haskel
C++
Python
C Ruby
Java
Java + Groovy
Java
2011 05/28
Slide # 22
2011 5 28
29. Groovy
Java
Java
2011 05/28
Slide # 23
2011 5 28