天天看點

List.isEmpty

jdk源碼:

/**
     * Returns <tt>true</tt> if this list contains no elements.
     *
     * @return <tt>true</tt> if this list contains no elements
     */
    public boolean isEmpty() {
	return size == 0;
    }
           

應用場景:

public <T> T find(Class<T> $class, Date statTime, String businessType) {
	Session session = sessionFactory.getCurrentSession();
	Map<String, Object> properties = new HashMap<String, Object>();

	StringBuffer hql = new StringBuffer("from " + $class.getSimpleName() + " t where t.businessType = :businessType and t.statTime = :statTime ");

	properties.put("statTime", statTime);
	properties.put("businessType", businessType);

	Query query = session.createQuery(hql.toString());
	query.setProperties(properties);

	List<T> list = query.list();
	if (list.isEmpty()) {// 為空 代表這條資料不存在
		return null;
	} else {
		return list.get(0);
	}
}