Πέμπτη 2 Απριλίου 2009

How to remove elements from an array

Problem

I have an array, e.g.

String strArray[] = new String[3];

and I 'd like to remove elements that satisfy a certain condition.

You could write something like:

List lstStrings = Arrays.asList(strArray);
for (int i=
lstStrings.size()-1; i>=0; i--) {
   String s =  (String)
lstStrings.get(i);
   if (s.equals("target")) {
      
lstStrings.remove(i);
   }
}


and you 'll get a

java.lang.UnsupportedOperationException at java.util.AbstractList.remove

What's wrong? Well, the problem lies in this command:

List lstStrings = Arrays.asList(strArray);

The result of
Arrays.asList is an Arrays$ArrayList (and not an ArrayList) which is immutable and doesn't provide for a remove method.

Solution

List lstStrings = new ArrayList(Arrays.asList(strArray));

Παρασκευή 27 Φεβρουαρίου 2009

JSPs and mime types

I have a simple request. I have an .xml file stored somewhere in my server. The file is an Excel sheet saved in .xml format. I simply point to it from an anchor in my jsp page. I want, when the user, left-clicks to it, MS-Excel (or OO Calc) to open to display the file instead of its contents being displayed inside the browser. So, I set the mime type in the anchor tag like so:

<a type="application/vnd.ms-excel; charset=UTF-8" href="<c:url value='/templates/template.xml'/>"><img src="<c:url value='/images/excel.gif'/>"></a>

The trick seems to work fine for Firefox, but alas, not for Internet Explorer. If someone knows a solution, please let me know. :-)

Τετάρτη 11 Φεβρουαρίου 2009

Speed up Intellij IDEA startup time

Intellij IDEA 8 has become a big IDE and takes quite sometime to startup. Here are a couple of hints to improve startup time:
  1. replace idea.exe.vmoptions contents with the following one:
-Xms400m
-Xmx800m
-XX:MaxPermSize=120m
-ea
-Dsun.awt.keepWorkingSetOnMinimize=true

increasing the RAM size allocation even more if your machine can handle it, e.g. to 1024m

2. modify idea.properties by moving the config directory to a faster drive if e.g. you home profile is saved in a network drive, e.g.:

# idea.config.path=${user.home}/.IntelliJIdea80/config
idea.config.path=D:/Config/.IntelliJIdea80/config

Πέμπτη 15 Ιανουαρίου 2009

Freelance in Northern Europe vs. in Southern

Did you know this? If you negotiate your daily rate in Northern Europe and/or US, it is without VAT. If you do in the south, then VAT is included! This is how companies save money!

So, if e.g. your daily rate is 500€, then in northern Europe you invoice the customer 617 € (assuming VAT 19%). In southern Europe, you invoice 500€, and you pay the VAT, hence your actual rate is 405€!!!

Τρίτη 2 Δεκεμβρίου 2008

Οι καθηγητές Πληροφορικής αγωνίζονται ενάντια στην απαξίωση - υποβάθμιση του επιπέδου σπουδών Πληροφορικής στο Δημόσιο Σχολείο όπου πρόσφατα μεθόδευσε το ΥΠΕΠΘ και απαιτούν:
  1. Οι αναθέσεις μαθημάτων ΚΑΙ για τα μαθήματα τα σχετικά με την Πληροφορική να γίνονται με βάση το γνωστικό αντικείμενο του πτυχίου και μόνο.
  2. Ο αριθμός μαθητών ανά καθηγητή στα εργαστηριακά μαθήματα να γίνει δώδεκα (12) ΚΑΙ για τα μαθήματα Πληροφορικής και όχι δεκαεπτά (17) όπως τώρα κατ’ εξαίρεση ορίζει η νέα εγκύκλιος.
  3. Ισονομία για τους υπεύθυνους εργαστηρίων Πληροφορικής με τρίωρη μείωση ωραρίου ΚΑΙ για το ΣΕΚ.
ΒΟΗΘΕΙΣΤΕ ΣΤΟΝ ΑΓΩΝΑ ΤΩΝ ΠΛΗΡΟΦΟΡΙΚΩΝ ΓΙΑ ΤΗΝ ΕΚΠΑΙΔΕΥΣΗ ΠΛΗΡΟΦΟΡΙΚΗΣ ΚΑΙ ΜΕΤΑΔΩΣΤΕ ΤΟ ΜΥΝΗΜΑ. Διαβάστε το αυτονόητο.

Δευτέρα 29 Σεπτεμβρίου 2008

Tips and tricks

  1. Tricks on immutability
a. Always make a copy of mutable objects inside your methods, e.g.

void myMethod(Date date) {
Date myCopy = new Date(date.getTime());
}
b. How do I create an immutable Java Bean or POJO? Make it read-only, and pass any parameters during initialisation (in the constructor):

public class MyClass {
private int param1;
private String param2;
private Date date;

public MyClass (int param1, String param2, Date d) {
this.param1 = param1;
this.param2 = param2;
this.date = new Date(d.getTime());
}

public int getParam1() {
return param1;
}

public String getParam2() {
return param2;
}

public Date getDate() {
return new Date(date.getTime());
}
}

c. Immutable objects and Hibernate
  • set the access type to “field” in order to use reflection to set the fields instead of property access, which uses setters.
  • set mutable to false (Hibernate will perform small optimizations, too).

Κυριακή 7 Σεπτεμβρίου 2008

Compare and sort Greek Strings

Inspired from the article Compare and sort Strings, I hereby present an example of Collators and Comparators with Strings that contain Greek letters.

import java.text.Collator;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

public class GreekCollator {
private static final Locale GREEK_LOCALE = new Locale("el", "GR");
private static final int NO_COLLATOR = -1;

private enum CollatorEnum {
NoCollator(NO_COLLATOR),
Primary(Collator.PRIMARY),
Secondary(Collator.SECONDARY),
Tertiary(Collator.TERTIARY),
Identical(Collator.IDENTICAL);

private int collatorStrength;
CollatorEnum(int collatorStrength) {
this.collatorStrength = collatorStrength;
}
}

/**
* @param args
*/
public static void main(String[] args) {
List<String> words = Arrays.asList("άλλος", "αλάνι", "βασικός", "ελεύθερος", "Ελεύθερος", "Άλλος", "Αλάνι");

System.out.println(words + " - Original Data\n");
sort(words, CollatorEnum.NoCollator); // don't use a collator
sort(words, CollatorEnum.Primary);
sort(words, CollatorEnum.Secondary);
sort(words, CollatorEnum.Tertiary);
sort(words, CollatorEnum.Identical);

System.out.println();
compare("αβγ", "ΆΒΓ", CollatorEnum.NoCollator);
compare("αβγ", "ΆΒΓ", CollatorEnum.Primary);
compare("αβγ", "ΆΒΓ", CollatorEnum.Secondary);
compare("αβγ", "ΆΒΓ", CollatorEnum.Tertiary);
compare("αβγ", "ΆΒΓ", CollatorEnum.Identical);

System.out.println();
compare("αβγ", "ΑΒΓ", CollatorEnum.NoCollator);
compare("αβγ", "ΑΒΓ", CollatorEnum.Primary);
compare("αβγ", "ΑΒΓ", CollatorEnum.Secondary);
compare("αβγ", "ΑΒΓ", CollatorEnum.Tertiary);
compare("αβγ", "ΆΒΓ", CollatorEnum.Identical);
}

private static void sort(List<String> aWords, CollatorEnum collatorEnum) {
if (collatorEnum.collatorStrength < 0) {
Collections.sort(aWords);
} else {
Collator collator = Collator.getInstance(GREEK_LOCALE);
collator.setStrength(collatorEnum.collatorStrength);
Collections.sort(aWords, collator);
}
System.out.println(aWords.toString() + " " + collatorEnum);
}

private static void compare(String aThis, String aThat, CollatorEnum collatorEnum) {
int comparison = 999;
if (collatorEnum.collatorStrength < 0) {
comparison = aThis.compareTo(aThat);
} else {
Collator collator = Collator.getInstance(GREEK_LOCALE);
collator.setStrength(collatorEnum.collatorStrength);
comparison = collator.compare(aThis, aThat);
}
if (comparison == 0) {
System.out.println("Collator sees them as the same : " + aThis
+ ", " + aThat + " - " + collatorEnum);
} else {
System.out.println("Collator sees them as DIFFERENT : " + aThis
+ ", " + aThat + " - " + collatorEnum);
}
}

}

Here is the output of the program, for your comments.

[άλλος, αλάνι, βασικός, ελεύθερος, Ελεύθερος, Άλλος, Αλάνι] - Original Data

[Άλλος, Αλάνι, Ελεύθερος, άλλος, αλάνι, βασικός, ελεύθερος] NoCollator
[Αλάνι, αλάνι, Άλλος, άλλος, βασικός, Ελεύθερος, ελεύθερος] Primary
[Αλάνι, αλάνι, Άλλος, άλλος, βασικός, Ελεύθερος, ελεύθερος] Secondary
[αλάνι, Αλάνι, άλλος, Άλλος, βασικός, ελεύθερος, Ελεύθερος] Tertiary
[αλάνι, Αλάνι, άλλος, Άλλος, βασικός, ελεύθερος, Ελεύθερος] Identical

Collator sees them as DIFFERENT : αβγ, ΆΒΓ - NoCollator
Collator sees them as the same : αβγ, ΆΒΓ - Primary
Collator sees them as DIFFERENT : αβγ, ΆΒΓ - Secondary
Collator sees them as DIFFERENT : αβγ, ΆΒΓ - Tertiary
Collator sees them as DIFFERENT : αβγ, ΆΒΓ - Identical

Collator sees them as DIFFERENT : αβγ, ΑΒΓ - NoCollator
Collator sees them as the same : αβγ, ΑΒΓ - Primary
Collator sees them as the same : αβγ, ΑΒΓ - Secondary
Collator sees them as DIFFERENT : αβγ, ΑΒΓ - Tertiary
Collator sees them as DIFFERENT : αβγ, ΆΒΓ - Identical

References
  1. Java Practices, Compare and sort Strings.