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);
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章