Thursday 15 March 2012

Remove duplicates from an ArrayList

A way I have found of removing duplicates from a Collection is doing this is to put the list into a Set (which does not allow duplicates). If you really need the it to be a List then you can add the Set back to it:

ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("test");
arrayList.add("test"); // fill the list..
// etc..
Set<String> set = new Set<String>(arrayList); // add to the set
arrayList.clear(); // clear the list
arrayList.addAll(set); // add the set back into the list without the duplicates

You can also use LinkedHashSet which preserves the order of the Collection:

Set<String> set = new LinkedHashSet<String>(arrayList);

Wednesday 14 March 2012

Ant target shortcuts in NetBeans

To solve some time, Ant targets can be given short cuts in the NetBeans IDE. To do this:

1. Open the build.xml  (e.g. through the Files tab). Within the Navigator tab you will see the file contents (i.e., the targets).
2. RIght click on a target and select Create Shortcut. A wizard will open that lets you configure the shortcut (e.g., toolbar button or a keyboard shortcut).

Monday 12 March 2012

NetBeans: Unrecognised project; missing plugin?

I recently upgraded my NetBeans to 7.1.1 from 6.8. After installation I could not open my web application projects, receiving the error: Project Name: [unrecognised project; missing plugin?].

To fix this you need to add the EAR project plugin (via Tools>Plugins). The plugin is named EJB and EAR. Once installed, the problem should no longer persist.

Friday 2 March 2012

WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

When I ssh into a server I get this message;

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
[long list of characters..].
Please contact your system administrator.


To get rid of this message I opened the known hosts file on my machine;

vi /home/user/.ssh/known_hosts

I then removed the line for the server I am trying to ssh into and saved the file. Next time I tried to ssh into the server I got the option to add it to my list of known hosts which I accepted.

This warning can appear when Linux is reinstalled or UNIX with OpenSSH.