Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking. I'm 24, born in Romania, studied at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff.

Wednesday, December 21, 2011

Sorting a HashMap By Values Without Losing Duplicates


The below method sorts a HashMap by values without removing duplicates. It works for <String,String> but it can be modified accordingly for other types:

private LinkedHashMap sortMapByValuesWithDuplicates(Map passedMap) {
List mapKeys = new ArrayList(passedMap.keySet());
List mapValues = new ArrayList(passedMap.values());
Collections.sort(mapValues);
Collections.sort(mapKeys);

LinkedHashMap sortedMap = new LinkedHashMap();

Iterator valueIt = mapValues.iterator();
while (valueIt.hasNext()) {
Object val = valueIt.next();
Iterator keyIt = mapKeys.iterator();

while (keyIt.hasNext()) {
Object key = keyIt.next();
String comp1 = passedMap.get(key).toString();
String comp2 = val.toString();

if (comp1.equals(comp2)) {
passedMap.remove(key);
mapKeys.remove(key);
sortedMap.put((String) key, (String) val);
break;
}
}
}
return sortedMap;
}

Saturday, December 17, 2011

Setting a path relative to the current class directory


Here are some useful and important Java paths:

package somepaths;

import java.io.File;

class SomeImportantPaths{

public void listPaths(){

//Path to current working folder
String ud = System.getProperty("user.dir");
System.out.println("Working directory:" + ud);

//Path to OS user directory
String uh = System.getProperty("user.home");
System.out.println("Working directory:" + uh);

//Path to this class directory
String cp = new File(getClass().getProtectionDomain().getCodeSource()
.getLocation().getPath()).getAbsolutePath();
System.out.println("This class directory is: " + cp);

//Path to this class parent directory
String cpp = new File(getClass().getProtectionDomain().getCodeSource()
.getLocation().getPath()).getParent();
System.out.println("This class directory is: " + cpp);
}
}

public class Main {

public static void main(String[] args) {
SomeImportantPaths sip = new SomeImportantPaths();
sip.listPaths();
}
}

Sunday, December 11, 2011

Display a Static Google Maps in a Java Desktop Application


Displaying a static Google Maps in a Java desktop application sound like something complicated, since is known that Google Maps provide a JavaScript API that can be exploit from web pages based on a Google key dedicated per host. Well, beside this API, Google Maps also provides an URL based API for extracting static Google Maps directly into desktop application, without requiring JavaScript or access keys. This API is very well explained here: http://code.google.com/apis/maps/documentation/staticmaps/index.html.
Based on this tutorial, Java can extract and save a Google Maps static map in just a few code lines, without high complexity involved – even if it is very simple it tooked me a while to get to this API, this is why I think to share this here:

try {
BufferedImage img = ImageIO.read(new URL("http://maps.googleapis.com/maps/
api/staticmap?sensor=false&size=512x512¢er=Brooklyn&zoom=12&style=feature:
road.local%7Celement:geometry%7Chue:0x00ff00%7 Csaturation:100&style=feature:
landscape%7Celement:geometry%7Clightness:-100"));

File outputfile = new File("map.png");
ImageIO.write(img, "png", outputfile);
System.out.println("Saved!");
} catch (Exception ex) {
System.out.println("Error!" + ex);
}

The map below is taken from Google Maps, and it looks like below: