map 缓存方法的使用
import java.util.LinkedHashMap; import java.util.List; import java.util.Map; public class OrganizationCached { public static OrganizationCached instance = null; private static Map<String, Organization> organizationMap = new LinkedHashMap<String, Organization>(); private OrganizationCached() { } public static OrganizationCached getInstance() { if (null == instance) { return new OrganizationCached(); } return instance; } public static void clear() { organizationMap.clear(); } public void put(Organization organization) { organizationMap.put(String.valueOf(organization.getId()), organization); } public void putAll(List<Organization> organizationList) { if (!CollectionUtils.isEmpty(organizationList)) { for (Organization organization : organizationList) { organizationMap.put(String.valueOf(organization.getId()), organization); } } } public Organization get(String key) { return organizationMap.get(key); } public Map<String, Organization> getAll() { return organizationMap; } public void remove(String key) { organizationMap.remove(key); } public void remove(List<String> keys) { if (CollectionUtils.isEmpty(keys)) { return; } for (String key : keys) { organizationMap.remove(key); } } }
故乡明