Cheat Sheet for Selecting Map/List/Set in Java

I have prepared a cheat sheet that can help to select appropriate Map, Set or List implementation when required. Java API has many implementations of interfaces mentioned, and it is not always clear which implementation should be used in a particular scenario. The cheat sheet comes in the form of a flow chart, which makes it easy to follow and understand.

The idea was inspired by Sergiy Kovalchuk. Sergiy made a flow chart using the most commonly used collections. I tried to take it one step further and make it much more thorough.  Please do let me know if anything needs to be corrected. I hope this can help developers out there to choose the right collection for the right job.

Cheat sheet for selecting appropriate set/list/map in Java

Click on the image to load it in a new window

Inspired by Sergiy Kovalchuk

The Truth About “SEO In a Box” Or Gimmicks Alike

In this article I would like to speak about SEO (Search Engine Optimization) services offered by some companies and/or individuals on the Internet.  I am not an SEO expert, but I do have some clue what it means. The reason for me writing about this is a discussion that I had with one of my friends over a beer. My friend (lets call him Mark) has an online business, that sells some gadgets and obviously as any other online business owner, wants to be ranked higher in search results and get more traffic to his site.

During the chat, Mark started telling me with excitement how he came across some SEO product online, product that promises people fast results in terms of ranking. The vendor of the product (SEO in a box, package, suitcase or some other type of container) swears that once you commit and purchase it, you will know for sure the secrets of how to obtain first position in Google search results and overall SEO secrets in general.

Now, Mark is a good business man, but he is not really a tech savvy guy. So after patiently listening to his fascinating story, I asked him: “Do you know that SEO cannot be packaged in a box, nor it comes as a software, and it is actually a process?”. As an answer, the only thing I got back was an empty stare: “What do you mean?”. I saw that I need to spend some time on that, so I quickly finished my beer, and replied:

Imagine that SEO is like a diet. You cant do it in one go, one day, one hour – you can try, but you wont get any results. It is a process, you have to change your life style, what you eat, what you do etc., and after a period of time you are going to get results. SEO works in a similar way – you need to undertake a number of steps to ensure that your SEO yields results. For example, the following are some of the common things you can have in place:

  1. Make sure that your HTML code validates using W3C HTML Validation Service. In terms of application, its easier for search bots to index your pages.
  2. To have a domain name which describes the nature of your business is a big plus too
  3. No broken links. You get really penalized for that.
  4. Good quality content, frequent site updates
  5. Link building with other sites/blog (I am not talking about link farming, but genuine replies on other blogs/site to try to engage users to visit your site)
  6. Keyword optimization
  7. Submission of your site/blog to quality blog and website directories
  8. To leverage social media like Facebook or/and Twitter (Not an SEO technique, but just another way to try to increase traffic to your site)

To do what I mentioned previously can take some time, and still it does not guarantee that your website is going to be ranked 1st. Think what about other businesses like your self that are doing SEO for their websites? It is a tough competition.

Some of the companies that sell these products are often making use of black hat SEO techniques in order to deliver fast results to their customers.

What is black hat SEO? Black SEO techniques help to manipulate relevance of search results within a short period of time. Some dudes that do SEO full time can be really good at it. They learn from observation after many trial-and-errors how search engines react in certain situations.

It is a known fact these days, that the more links point to your site – the higher your page rank is with Google, and the better it is for your search results. Therefore some websites willingly exchange links with you. Some other websites that have high traffic of visitors, wont exchange links with you, but will link to you for a fee. This is called link farming, one of the black hat SEO techniques.

Google does not like manipulations. It is smart enough to identify websites that do black hat SEO. Therefore when such website detected, it is going to be buried deep deep inside the pages of search results. Long story short – shortcuts do not work, or if they do work, you as a web site owner should be prepared to bare possible consequences in the future. If you are running a successful business, last thing that you need is to be penalized by search engines for your short cuts.

I am not saying that the moment you exchanged a couple of links you are doomed. There are many blog/sites out there who exchange links with friends. Its just looks suspicious when you start hosting hundreds or even thousands of links to other sites, or let someone who does link farming link to your site. Google is able to pick up on many naughty things some people do in order to spin off their sites.

Therefore the whole idea of “SEO in a what not” is ridiculous. Many of these SEO software companies target people who do not really understand what SEO is really is. These guys try to sell an illusion that the moment you paid for their course, software or what not, you are going to be promised a spot on the first page.

No one can really tell you the secrets of how Google operates (unless its actually a Google engineer directly involved with search algorithm or Sergey Brin himself), its all one big illusion, sold to non-tech business people in order to scam some quick cash. Well this is just my opinion about the whole thing ;-)

Update

A buddy of mine who is tinkering with SEO techniques made some comments:

- Validation in W3C is really not needed… It’s good practice I agree, but really what should be said is that the developer should be using a clean, css-based layout with scripts (css, js etc) being stored externally.

- Domain name describing the business is less important that having a domain name that contains the keyword(s) you are wanting to get ranked for. Also, having a .com.au for an Aussie businesses is generally better than having a .com

- On broken links… Yes this is true, but worth elaborating, tell the user to setup Webmaster Tools & check for 404s. If they exist, 301 redirect to the most appropriate page.

- Directories are almost pointless in this day & age. I’d pick the top 3 in your location / relevance area & move on. (for example, hot frog is a good one for aussies)

- Leveraging social media is actually becoming an SEO technique now, Google assigns value to the popularity of your content of social networks, its only a fairly recent phenomena. It’s also VERY useful for getting pages indexed. (tweet the full URL and you’ll have google bots there in a jiffy ;) )

How To Define a Spring Bean With Generics Parameter

At the time when this article was written, the Spring framework does not allow to define beans in application context xml file using parameterized classes. The reason for that is that generics parameters are compiler information, and not run-time. Compiler information is something that Spring does not care about. For example, the following (or something similar) will NOT work:

[java]
<bean id="someClass" class="asia.javabeans.SomeClass<asia.javabeans.Blah>" />
[/java]

Sometimes, this restriction can create a problem if you have a parameterized class that has to be loaded by Spring. As a workaround, create an empty child class that extends your parameterized class, and then use that class’s canonical name as your bean definition. Consider the following parameterized parent and the extending child classes:

[java]
public class SomeParent<Blah> {

}

public class Child extends SomeParent<Blah> {
// Just an empty class
}
[/java]

and your Spring definition can now look like this:

[java]
<bean id="childClass" class="asia.javabeans.Child" />
[/java]

The down side here is that you get stuck with an empty class, but at least you can have your bean definition in your application context xml file.

Inheritance and Generics with Abstract Data Types

In this article, I want to demonstrate a simple inheritance example that uses generics.  The abstract parent class is generic in the type, and it defines an abstract method that accepts as a method parameter the generic data type.

The generic data type used by the extending child in the implementation of the abstract method. This example is going to discuss the benefit of this approach.

Consider the following parent class:

[java]
public abstract class Parent<T> {
protected abstract void process(T data);
}
[/java]

Consider the following two child classes that extend the parent:

[java]
public class Child extends Parent<SomeClass> {
@Override
public void process(SomeClass data) {
// Do stuff
}
}
[/java]

[java]
public class AnotherChild extends Parent<OtherClass> {
@Override
public void process(OtherClass data) {
// Do stuff
}
}
[/java]

You can clearly see how this approach creates flexibility when multiple child classes require different data type parameters when implementing the abstract method. Because of the generic type setting in the abstract method, any data type can be passed to the child. This approach can be particularly useful when implementing a strategy design pattern.

CrowdSauce is Live!

This is one of the coolest ideas that I have seen since Facebook became a hit. CrowdSauce is a new location based application that allows you to harness the power of the people to get discounts from businesses. CrowdSauce was born out of the idea that, the people, have the power to put pressure on businesses by using the power of the crowd. What is most important – its really fun :)

You get medals as you make requests for rewards from businesses, and you get promoted in rank. They have this army system going on. I am a second lieutenant now :) )

How To Select a Child Element Using jQuery

Let’s assume you have an HTML string containing a parent element with a number of children and their sub children elements. If you’d like to extract HTML of one of the children (by class name or id), you can use this jQuery line:

[javascript]
var html = $(htmlString).find(‘div.target’).html();
[/javascript]

It can be useful when you get an AJAX response from the server, and only a particular section needs to extracted.

How To Populate a Timestamp Field With Current Timestamp

Raw:

[sql]
SELECT SYSTIMESTAMP AS TSTAMP FROM DUAL;
[/sql]

Formatted:

[sql]
SELECT TO_CHAR(SYSTIMESTAMP, ‘DD/MM/YYYY HH24:MI:SS.FF6′) AS TSTAMP FROM DUAL;
[/sql]

Populating a timestamp database field using a current timestamp:
[sql]
INSERT INTO TABLE_NAME(TSTAMP) VALUES ((SELECT SYSTIMESTAMP FROM DUAL));
[/sql]

Or another way is simply to use TO_TIMESTAMP() if the timestamp time is known:

[sql]
INSERT INTO TABLE_NAME(TSTAMP) VALUES (
TO_TIMESTAMP(’3/08/2010 4:38:52.000000 PM’,'fmDDfm/MM/YYYY fmHH12fm:MI:SS.FF AM’)
);
[/sql]

How To Tell XStream To Not Unmarshall XML Fields

My colleague found this when he was looking for a way not to unmarshall XML fields that did not exist in his POJO using XStream. The code makes use of a MapperWrapper class:

[java]
XStream xstream = new XStream() {
@Override
protected MapperWrapper wrapMapper(MapperWrapper next) {
return new MapperWrapper(next) {

@Override
public boolean shouldSerializeMember(Class definedIn, String fieldName) {
if (definedIn == Object.class) { return false; }
return super .shouldSerializeMember(definedIn, fieldName);
}
};
}
};
[/java]

What Is The Best Workplace For a Fresh Graduate?

Recently I was invited to do a presentation in-front of soon-to-be graduates at my university where I have obtained my qualification in Computer Science.  Myself and a few other of my fellow ex-classmates were suppose to give students a talk on what to expect after graduation, and what it is like working in the industry.

After my presentation, one of the soon-to-be graduates asked me a question: “What type of companies should the fresh graduate look for, and what jobs should be avoided?

Although it may sound simple, this question was not an easy one to answer. Sure, as an ex-grad my self, I remember our lecturers telling us to take any professional job, to grab any opportunity and just to get a foot in the door. Not a bad advise after all, and many including myself have followed this  path, and I am sure many still are. At the time, I was  told not to worry too much, as at the first job I will be taught everything I need to start my career path.

But only after a few years, I understood how important the first job really is. The first job can shape a graduate into a good or an average IT professional. The first job either can teach the grad the logical approach and how to be meticulous about little details or how to look for shortcuts. Also, the first job will dictate what technologies and tools the grad may use in the future, whether he or a she will become proprietary or an open source IT professional.

Off course, when applying for jobs and attending job interviews, as a  fresh graduate you are not in a position to bargain and demand the best (whatever “the best” means). Simply because the grad does not have the experience and skills to support the demands. Therefore, around the first two years, a graduate has be satisfied with the first job he got, in a hope that the experience gained will help in the professional future endeavors. The graduate hopes that the first company he got into, is a good one and considers him self very lucky to be there. But really, is the first company a good one?

One of the types of companies that I would advise for a fresh graduate to avoid is consultancy firms. Having said that, I want to say that there is nothing wrong in working for a consultancy firm (I am working for one now), it is just as a fresh grad it is not the best place in my opinion to get shaped as a strong IT professional. I will try to elaborate on this.

As a consultant, you are assigned to a different projects, usually on the clients’ site. For a fresh grad, this experience can be similar to a jump into the ocean from a plane high in the sky. One may say there is nothing wrong with that, this experience will force you to learn.

I am not sure that I fully agree here. Sure, this experience will force you to learn, learn something, definitely. Will force you to learn to do what the client needs, to follow the clients’ way. Which is often can be not entirely the right way. A consultant usually costs a client a lot of money, which is why clients want to see results delivered and delivered fast. Therefore this type of environment, is not very good for learning the skills and building a strong foundation nor the client is interested in spending the big bucks for teaching fresh grads.

Another type of companies that I don’t favor as the first job, are the big corporations (for example lets say banks). Working for a corporation at the beginning of your career, your freedom and creativity as IT professional are often restricted. You are forced to use tools that are in place and most probably cannot use other tools to get the job done, even if the other tools are better. In corporation, the fresh grad can start feeling very small, a little screw in a huge apparatus, without a clear understanding why he does what he does.

When I was hunting for jobs after Uni, I was quite skeptical about graduate programs run by some big banks or insurance companies. Usually they teach their own way to do things, which is not necessary the right way. They teach you what they need you to know for them. Corporation can be a big place, and often people don’t have time to teach you, they are there to do their job, get paid and go home. The corporations are lacking human touch, and often not having around a supervisor or a mentor who can be available when you need them, can be a real minus.

Having a mentor. I cannot stress enough when I say, how important is to have a good mentor who is there to give you a “slap” on the head and a “kick” in the right direction. Someone who really cares and have a  passion for what he does. One may say: yeah, having mentor is a good thing, but I don’t really need a “baby sitter”, I am capable learning by my self, Google is full of tutorials.

Sure, we all should (and must) be able to know how to improve ourselves through self learning. I am not saying that a lack of mentoring can be the crucial factor in failing to become a good IT professional. No, not at all. Mentor is just a person that simply has more hands on experience than you, and can explain you things in person, face to face which is sometimes much better than just reading a tutorial on the web.

How many of us, IT professionals are lucky to say that “yes, I have had a good mentor that helped when I began my career path”? I personally can say, that I was lucky. I have had two mentors. Each and every one of them have contributed in their own way to my development as an IT professional I am today. I am still drawing on those experiences and trying to improve my self on a daily basis.

So what is the right place to start working, in my opinion? I think starting in a small company (6-10 employees) is the best option for a fresh grad to get hands on experience and build a solid foundation. Usually small companies are more versatile in their choice of technologies and tools used, and there is a lot of space for creativity and for learning some basic concepts. Small companies have a lot of human interaction between the senior and junior developers, something that can only positively contribute towards professional development.

I admit, that now it is easy for me to sit and contemplate what is good and what is not so good. Many fresh grads, don’t really think that far, they just want to get out there after 3-4 years of University and start making some money.

In summary, I want to say, that the decisions one makes at the beginning of his/her career can really decide his/her professional future. The highest paying job is not necessary the best job, when starting a career. Gaining valuable skills is much more important. Therefore, the first few years of boot camp, are very important in the process of becoming a good, passionate IT professional , instead of someone who takes it as just another nine-to-five job. The latter lacks of creativity and unfortunately modern workplaces are full of them.