About nobukimi@gmail.com

I’ve been working on Java developer for 15+ years. Hope this website shares my experiences for developing and maintaining business web apps.

Hash Set vs Array List

title-img-nlogn-1
Recalling a Big-O notation in (CS) Computer Science major course I have taken in College:

O(n)O(1)O(log n)O(n log n)O(n^2)O(n^3)
111111
211248
41281664
8132464512
1614642654096
10241101024010485761073741824

In Biz apps, you need to use List object all the time. And often has performance issue. It’s better to use some other data structure, such as Map object because HashSet is much faster compared to ArrayList:
getcontainsaddremove
ArrayListO(1)O(n)O(1)O(n)
HashSetN/AO(1)O(1)O(1)

Note: HashSet does not provide “get” method, but as long as “contains” method is available, we know what element we need

Here is how to work on:
First, converting ArrayList to HashSet

private Set<String> convertToHashSet(List<Map<String, Object>> listToDelete) {
	Set<String> set = new HashSet<String>();
	for (Map<String, Object> obj : listToDelete) {
		set.add(new String("test"));
	}
	return set;
}

Or just simply set a list as constructor’s parameter:

private Set<String> convertToHashSetDirectory(List<String> list) {
	return new HashSet<String>(list);
}

“contains” with for loop version:

private void hashSetDemoFor(Set<String> setOfMdseOnScreen, Set<String> setOfMdseAcctTo) {
	Map<String, Long> resultMap = new HashMap<String, Long>();
	for (String mdseAcctTo : setOfMdseAcctTo) {
		if (setOfMdseOnScreen.contains(mdseAcctTo)) {
			// Do something
			continue;
		}
	}
}

“contains” with while loop version:

private void hashSetDemoWhile(Set<String> setOfMdseOnScreen, Set<String> setOfMdseAcctTo) {
	Iterator<String> itr = setOfMdseOnScreen.iterator();
	while (itr.hasNext()) {
		String mdseOnScreen = itr.next();
		if (setOfMdseAcctTo.contains(mdseOnScreen)) {
			// Do something
			continue;
		}
	}
}

Generic DAO

db-chain1
For the CRUD operation in Hibernate, you don’t need to repeat Insert, Update, Delete at all. The Generic DAO can take any entity object to do this.

It can be accessed by any entity object with empty method, or some additional customize method

There are Service, and DAO layer, and your controller access Service class
Controller <-> Service <-> DAO : Source Code
model2

The interface of Generic DAO.

package com.ns.spring.dao;

import java.io.Serializable;
import java.util.List;

import com.ns.spring.exception.NSException;

public interface GenHbDao<E, K extends Serializable> {

	public List<E> findAll();	
	public E findById(K key) throws NSException;
	public void save(E obj);
	public void update(E obj);
	public void saveOrUpdate(E obj);
	public void delete(E obj);

}

The implementation of above interface

package com.ns.spring.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import org.hibernate.ObjectNotFoundException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ns.spring.exception.NSException;

@SuppressWarnings("unchecked")
@Repository
public abstract class GenHbDaoImpl<E, K extends Serializable> implements GenHbDao<E, K> {

	private SessionFactory sessionFactory;
	private Class<E> clazz;

	public GenHbDaoImpl() {
		Type t = getClass().getGenericSuperclass();
		ParameterizedType pt = (ParameterizedType) t;
		this.clazz = (Class<E>) pt.getActualTypeArguments()[0];
	}

	public Class<E> getCurrClazz() {
		return clazz;
	}

	@Autowired
	public void setSessionFactory(SessionFactory sf) {
		this.sessionFactory = sf;
	}

	protected Session getCurrSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public void save(E obj) {
		this.getCurrSession().persist(obj);
	}

	@Override
	public void update(E obj) {
		this.getCurrSession().update(obj);
	}

	@Override
	public void saveOrUpdate(E obj) {
		this.getCurrSession().saveOrUpdate(obj);
	}

	@Override
	public void delete(E obj) {
		this.getCurrSession().delete(obj);
	}

	@Override
	public List<E> findAll() {
		List<E> list = this.getCurrSession().createCriteria(getCurrClazz()).list();
		return list;
	}

	@Override
	public E findById(K key) throws NSException {
		Session session = this.getCurrSession();
		try {
			E p = (E) session.load(getCurrClazz(), key);
			return p;
		} catch (ObjectNotFoundException e) {
			throw new NSException("No result");
		}
	}
}

The interface of Service class (nearly identical of Generic DAO) that actually called by your controller.

package com.ns.spring.service;

import java.io.Serializable;
import java.util.List;

public interface GenHbService<E, K extends Serializable> {

	public List<E> findAll();
	public E findById(K key);
	public void save(E obj);
	public void update(E obj);
	public void saveOrUpdate(E obj);
	public void delete(E obj);

}

And finally the implementation of the above service interface. Notice the @Transactional annotation in each method

package com.ns.spring.service;

import java.io.Serializable;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ns.spring.dao.GenHbDao;
import com.ns.spring.exception.NSException;

@Service
public abstract class GenHbServiceImpl<E, K extends Serializable> implements GenHbService<E, K> {

	private GenHbDao<E, K> genDao;

	public GenHbServiceImpl(GenHbDao<E, K> genDao) {
		this.genDao = genDao;
	}

	public GenHbServiceImpl() {
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void save(E obj) {
		genDao.save(obj);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void update(E obj) {
		genDao.update(obj);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void saveOrUpdate(E obj) {
		genDao.saveOrUpdate(obj);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void delete(E obj) {
		genDao.delete(obj);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public List<E> findAll() {
		return genDao.findAll();
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public E findById(K key) {
		try {
			return genDao.findById(key);
		} catch (NSException e) {
			return null;
		}
	}
}