eric.mariacher@gmail.com
Affichage des articles dont le libellé est Java. Afficher tous les articles
Affichage des articles dont le libellé est Java. Afficher tous les articles
mardi, avril 20, 2010
When you need to build a java jar with external dependencies
When you need to build a java jar with external dependencies use Fat Jar Eclipse Plug-In
mercredi, avril 07, 2010
Java program using lambdaj
A while ago I wrote a program in Java doing some filtering and displaying without using for while statements and an other one using haskell functional language.
Today, I did the same thing (i.e. no for or while statements), but using lambdaj java library.
There is still a for statement but if you make country as an object and not a string then you can use lambdaj features.
European people: [Eric from France, Martine from France, John from Great-Britain, Martha from Great-Britain, Carine from France, Gerd from Deutschland, Giuseppe from Italia, Martha from Deutschland]
2 Get the different countries:
European countries: [Great-Britain, France, Italia, Deutschland]
3 List the people that belong to each country:
People from: [France]: [Carine from France, Eric from France, Martine from France]
People from: [Deutschland]: [Gerd from Deutschland, Martha from Deutschland]
People from: [Great-Britain]: [John from Great-Britain, Martha from Great-Britain]
People from: [Italia]: [Giuseppe from Italia]
Here is the code:
1 import static ch.lambdaj.Lambda.*; 2 import ch.lambdaj.group.*; 3 import java.util.List; 4 import java.util.Arrays; 5 import java.util.Set; 6 7 8 public class Europeans1 { 9 10 static List<People> l_europeans = Arrays.asList( 11 new People("Eric", "France"), 12 new People("Martine", "France"), 13 new People("John", "Great-Britain"), 14 new People("Martha", "Great-Britain"), 15 new People("Carine", "France"), 16 new People("Gerd", "Deutschland"), 17 new People("Giuseppe", "Italia"), 18 new People("Martha", "Deutschland")); 19 20 public static void main(String[] args) { 21 System.out.println("European people: "+l_europeans); 22 Group<People> g_countries = Groups.group(l_europeans, 23 Groups.by(on(People.class).getNationality())); 24 Set<String> set_countries = g_countries.keySet(); 25 System.out.println("European countries: "+set_countries); 26 for(String s_country:set_countries) { 27 print_inhabitants(s_country); 28 } 29 } 30 31 static void print_inhabitants(String s_country) { 32 System.out.print("People from "+s_country+": "); 33 List<People> l_inhabitants = select(l_europeans, 34 having(on(People.class).getNationality(), 35 org.hamcrest.Matchers.equalTo(s_country))); 36 forEach(l_inhabitants).printFirstName(); 37 System.out.println(""); 38 } 39 }dimanche, avril 04, 2010
Java vs Haskell
A while ago I wrote a program in Java doing some filtering and displaying without using for while statements and another one using lambdaj java library.
At that time I did not know it, but I was doing some kind of functional programming. I discovered Haskell a few weeks ago, and I duplicated what I did in Java.
As you can guess the Haskell code is way shorter, and goes even a little further.
European people: [Eric from France, Martine from France, John from Great-Britain, Martha from Great-Britain, Carine from France, Gerd from Deutschland, Giuseppe from Italia, Martha from Deutschland]
2 Get the different countries:
European countries: ["Deutschland","France","Great-Britain","Italia"]
3 List the people that belong to each country:
People from: [France]: [Carine from France, Eric from France, Martine from France]
People from: [Deutschland]: [Gerd from Deutschland, Martha from Deutschland]
People from: [Great-Britain]: [John from Great-Britain, Martha from Great-Britain]
People from: [Italia]: [Giuseppe from Italia]
import Data.List
import Data.Function
le = [
("Eric", "France"),("Martine", "France"),
("John", "Great-Britain"),("Martha", "Great-Britain"),("Carine", "France"),
("Gerd", "Deutschland"),("Giuseppe", "Italia"),("Martha", "Deutschland")
]
dla xs = "European people: " ++ show [ fst x ++ " from " ++ snd x | x <- xs]
lc xs = [ head y | y <- group(sort([ snd x | x <- xs]))]
dlc = "European countries: " ++ show(lc le)
lpc5 xps = groupBy (\x y -> snd x == snd y) (sortBy (compare `on` snd)([ xp | xp <-xps ]))
lpc9 xps = unlines [ "People from " ++ show (snd (head xp)) ++ ":" ++ (show [fst x | x<-xp ]) | xp <-xps ]
fait9 = do { m <- [lpc5 le]; lpc9 m }
dlad = [ dla le, dlc, fait9 ]
main = putStrLn (unlines(dlad))
import Data.Function
le = [
("Eric", "France"),("Martine", "France"),
("John", "Great-Britain"),("Martha", "Great-Britain"),("Carine", "France"),
("Gerd", "Deutschland"),("Giuseppe", "Italia"),("Martha", "Deutschland")
]
dla xs = "European people: " ++ show [ fst x ++ " from " ++ snd x | x <- xs]
lc xs = [ head y | y <- group(sort([ snd x | x <- xs]))]
dlc = "European countries: " ++ show(lc le)
lpc5 xps = groupBy (\x y -> snd x == snd y) (sortBy (compare `on` snd)([ xp | xp <-xps ]))
lpc9 xps = unlines [ "People from " ++ show (snd (head xp)) ++ ":" ++ (show [fst x | x<-xp ]) | xp <-xps ]
fait9 = do { m <- [lpc5 le]; lpc9 m }
dlad = [ dla le, dlc, fait9 ]
main = putStrLn (unlines(dlad))
vendredi, octobre 09, 2009
lundi, avril 06, 2009
java list management and filtering without any loop (for, while)
Just a Java exercize for the fun of it: do some list manipulation / filtering without using for/while statements:
1 Have a list of people belonging to various countries:
European people: [Eric from France, Martine from France, John from Great-Britain, Martha from Great-Britain, Carine from France, Gerd from Deutschland, Giuseppe from Italia, Martha from Deutschland]
2 Get the different countries:
European countries: [Gerd from Deutschland, Eric from France, John from Great-Britain, Giuseppe from Italia]
3 List the people that belong to each country:
People from: [France]: [Carine from France, Eric from France, Martine from France]
People from: [Deutschland]: [Gerd from Deutschland, Martha from Deutschland]
People from: [Great-Britain]: [John from Great-Britain, Martha from Great-Britain]
People from: [Italia]: [Giuseppe from Italia]
Never use for while statements to perform these tasks but make heavy use of the SortedSet type. Find code here.
The trick is to use a compare function that return 0 when the guy's/lady's country does not match the country we are listing inhabitants for.
Read also the same exercize done differently in Java and Haskell:
1 Have a list of people belonging to various countries:
European people: [Eric from France, Martine from France, John from Great-Britain, Martha from Great-Britain, Carine from France, Gerd from Deutschland, Giuseppe from Italia, Martha from Deutschland]
2 Get the different countries:
European countries: [Gerd from Deutschland, Eric from France, John from Great-Britain, Giuseppe from Italia]
3 List the people that belong to each country:
People from: [France]: [Carine from France, Eric from France, Martine from France]
People from: [Deutschland]: [Gerd from Deutschland, Martha from Deutschland]
People from: [Great-Britain]: [John from Great-Britain, Martha from Great-Britain]
People from: [Italia]: [Giuseppe from Italia]
Never use for while statements to perform these tasks but make heavy use of the SortedSet type. Find code here.
The trick is to use a compare function that return 0 when the guy's/lady's country does not match the country we are listing inhabitants for.
Read also the same exercize done differently in Java and Haskell:
- one using lambdaj java library.
- using haskell functional language.
mardi, février 17, 2009
Developing Java Agents for Lotus Notes under Eclipse
I started developing some java agent for Lotus Notes 8.5 .
The native Lotus Notes eclipse environment does not really meet my needs.
I usually use a regular eclipse environment to develop my java applications with all its nice help features.
I wanted to develop these Lotus Notes java agents under the same environment, but when calling Lotus specific java classes, I got some errors because eclipse could not reference the right jar.
I fixed the problem by just adding Notes.jar (that I found in LotusNotes program directory) as an external jar in the build path of my eclipse project.
The native Lotus Notes eclipse environment does not really meet my needs.
I usually use a regular eclipse environment to develop my java applications with all its nice help features.
I wanted to develop these Lotus Notes java agents under the same environment, but when calling Lotus specific java classes, I got some errors because eclipse could not reference the right jar.
I fixed the problem by just adding Notes.jar (that I found in LotusNotes program directory) as an external jar in the build path of my eclipse project.
mercredi, novembre 12, 2008
Java Extracting only first Integer value from String
Here is the new java function that extracts the 1st Integer value from String:
String s = new String("Eric Mariacher 1965 AoxomoxoA 1967 Cowabunga 2009 ");
Matcher matcher = Pattern.compile("\\d+").matcher(s);
matcher.find();
int i = Integer.valueOf(matcher.group());
Here is the old java function that extracts the 1st Integer value from String:
String s = new String("Eric Mariacher 1965 AoxomoxoA 1967 Cowabunga 2009 ");
Matcher matcher = Pattern.compile("\\d+").matcher(s);
matcher.find();
int i = Integer.valueOf(matcher.group());
Here is the old java function that extracts the 1st Integer value from String:
3: public static int findNextValidInteger(String s) throws Exception {
4:
5: // detect 1st digit after start of string
6: int beginIndex=0;
7: int endIndex=1;
8: int length=s.length();
9: while(!s.substring(beginIndex,endIndex).matches("\\d")) {
10: beginIndex++;
11: endIndex++;
12: if(endIndex>length) {
13: throw new Exception("No digit found.");
14: }
15: }
16: int i_startNumber= beginIndex;
17:
18: // detect 1st non digit after start previously detected 1st digit
19: beginIndex=i_startNumber;
20: endIndex=i_startNumber+1;
21: while(s.substring(beginIndex,endIndex).matches("\\d")) {
22: beginIndex++;
23: endIndex++;
24: if(endIndex>length) {
25: break;
26: }
27: }
28: int i_endNumber= beginIndex;
29:
30: // return absolute value of integer
31: return Integer.valueOf(s.substring(i_startNumber, i_endNumber));
32: }
33:
Can also be done the following way, but may take lot of resources if String
is very long and you only need the 1st Integer.
35:
36: StringTokenizer str = new StringTokenizer("A 1965 Eric");
37:
38: while(str.hasMoreElements()) {
39: System.out.println(str.nextElement());
40: int i = Integer.parseInt(str.nextElement());
41: }
42:
lundi, juillet 28, 2008
An enhanced TreeMap Applet
TreeMap is a way to present information that is very efficient sometimes...
This TreeMap Applet has been enhanced with custom colors and comments using 2 additional xml fields to the standard xml description:
- details for comments when clicking on squares.
- hue / color for discriminating between main groups of squares.
Labels:
applet,
information visualization,
IT geek,
Java,
treemap
jeudi, mai 22, 2008
Inscription à :
Articles (Atom)