Your SlideShare is downloading. ×
0
Productive Programming with
Groovy
S G Ganesh
http://ocpjp7.blogspot.in
Introducing Groovy
How I Felt Programming in Groovy vs.
Java!
Simple (first) Example
import java.io.*;
class Type {
public static void main(String []files) {
// process each file passe...
Mere Syntactic Sugar?
Map<String, String> inglishWords = new HashMap<>();
inglishWords.put("Shampoo", "'Chapmo' (Hindi)");...
Just Concise Expression?
System.out.println("The (key-based) sorted map is: ");
Map<String, String> keySortedMap = new Tre...
Now for the Real Magic! 
class MapUtil {
// this code segment from http://stackoverflow.com/questions/109383/how-to-sort-...
HTML Creation Example
try (PrintWriter pw = new PrintWriter(new FileWriter("./index.java.html"))) {
pw.println("<html> <he...
HTML Creation Example
def writer = new StringWriter()
def doc = new MarkupBuilder(writer)
doc.html() {
head {
title("Words...
Not Convinced?! Check XML Creation
Example
try (PrintWriter pw = new PrintWriter(new FileWriter("./words.xml"))) {
pw.prin...
Where to Learn More About Groovy?
Some Dynamic Languages for .NET
World
Upcoming SlideShare
Loading in...5
×

Productive programming with groovy - an introduction

2,344

Published on

Are you a Java programmer having wrist pain typing a lot (yes, I have!)? Does your mind keep swinging from loving to hating Java? Do you want to become more productive without leaving your world of Java? If so, this presentation is for you. I have been experimenting with Groovy for the last three months and I'm loving it - I am not going back to programming in plan old Java anymore. Yes, I have become Groovy - have you?

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,344
On Slideshare
0
From Embeds
0
Number of Embeds
41
Actions
Shares
0
Downloads
3
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Productive programming with groovy - an introduction"

  1. 1. Productive Programming with Groovy S G Ganesh http://ocpjp7.blogspot.in
  2. 2. Introducing Groovy
  3. 3. How I Felt Programming in Groovy vs. Java!
  4. 4. Simple (first) Example import java.io.*; class Type { public static void main(String []files) { // process each file passed as argument for(String file : files) { // try opening the file with FileReader try (FileReader inputFile = new FileReader(file)) { int ch = 0; while( (ch = inputFile.read()) != -1) { // ch is of type int - convert it back to char System.out.print( (char)ch ); } } catch (FileNotFoundException fnfe) { System.err.printf("Cannot open the given file %s ", file); } catch(IOException ioe) { System.err.printf("Error when processing file %s; skipping it", file); } // try-with-resources will automatically release FileReader object } } } args.each { println new File(it).getText() }
  5. 5. Mere Syntactic Sugar? Map<String, String> inglishWords = new HashMap<>(); inglishWords.put("Shampoo", "'Chapmo' (Hindi)"); inglishWords.put("Sugar", "'Sarkkarai' (Tamil)"); inglishWords.put("Copra", "'Koppara' (Malayalam)"); inglishWords.put("Jute", "'Jhuto' (Bengali)"); inglishWords.put("Cot", "'Khatva' (Sanskrit)"); for(Map.Entry<String, String> word : inglishWords.entrySet()) { System.out.printf("%s => %s n", word.getKey(), word.getValue()); } def inglishWords = [ "Shampoo" : "'Chapmo' (Hindi)", "Sugar" : "'Sarkkarai' (Tamil)", "Copra" : "'Koppara' (Malayalam)", "Jute" : "'Jhuto' (Bengali)", "Cot" : "'Khatva' (Sanskrit)" ] inglishWords.each { key, value -> println "$key => $value" }
  6. 6. Just Concise Expression? System.out.println("The (key-based) sorted map is: "); Map<String, String> keySortedMap = new TreeMap<>(inglishWords); for(Map.Entry<String, String> word : keySortedMap.entrySet()) { System.out.printf("%s => %s n", word.getKey(), word.getValue()); } println "The (key-based) sorted map is: " def keySortedMap = inglishWords.sort() keySortedMap.each { key, value -> println "$key => $value" }
  7. 7. Now for the Real Magic!  class MapUtil { // this code segment from http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue( Map<K, V> map ) { List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>( map.entrySet() ); Collections.sort( list, new Comparator<Map.Entry<K, V>>() { public int compare( Map.Entry<K, V> o1, Map.Entry<K, V> o2 ) { return (o1.getValue()).compareTo( o2.getValue() ); } } ); Map<K, V> result = new LinkedHashMap<K, V>(); for (Map.Entry<K, V> entry : list) { result.put( entry.getKey(), entry.getValue() ); } return result; } } System.out.println("The (value-based) sorted map is: "); Map<String, String> valueSortedMap = MapUtil.sortByValue(inglishWords); for(Map.Entry<String, String> word : valueSortedMap.entrySet()) { System.out.printf("%s => %s n", word.getKey(), word.getValue()); } println "The (value-based) sorted map is: " def valueSortedMap = inglishWords.sort { it.value } valueSortedMap.each { key, value -> println "$key => $value" }
  8. 8. HTML Creation Example try (PrintWriter pw = new PrintWriter(new FileWriter("./index.java.html"))) { pw.println("<html> <head> <title>Words from Indian origin in English</title> </head> <body>"); pw.println("<h1>Words from Indian origin in English</h1>"); pw.println("<table border='1'> <tr> <th>Inglish word</th> <th>Origin</th></tr>"); for(Map.Entry<String, String> word : inglishWords.entrySet()) { pw.printf("<tr> <td> %s </td> <td> %s </td> </tr>", word.getKey(), word.getValue()); } pw.println("</table> <p style='font-style:italic;font-size:small;float:right'>Results obtained at " + new Date() + "</p> </body> </html>"); }
  9. 9. HTML Creation Example def writer = new StringWriter() def doc = new MarkupBuilder(writer) doc.html() { head { title("Words from Indian origin in English") } body { h1("Words from Indian origin in English") table(border:1) { tr { th("Inglish word") th("Origin") } inglishWords.each { word, root -> tr { td("$word") td("$root") } } } p(style:'font-style:italic;font-size:small;float:right', "Results obtained at ${new Date().dateTimeString}") } } def filename = 'index.groovy.html' new File(filename).write(writer.toString())
  10. 10. Not Convinced?! Check XML Creation Example try (PrintWriter pw = new PrintWriter(new FileWriter("./words.xml"))) { pw.println("<inglishwords>"); for(Map.Entry<String, String> word : inglishWords.entrySet()) { pw.printf("t<language word='%s'> n tt <origin> '%s'</origin> n t </language> n", word.getKey(), word.getValue()); } pw.println("</inglishwords>"); } xmlbuilder = new groovy.xml.MarkupBuilder() xmlbuilder.inglishwords { words.each { key, value -> language(word:key) { origin(value) } } }
  11. 11. Where to Learn More About Groovy?
  12. 12. Some Dynamic Languages for .NET World
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×