java集合
List:
import java.util.ArrayList;
import java.util.List;
public class ListTest {
public static void main(String[] args) {
List<String> books=new ArrayList<>();
books.add("hello");
books.add(" ");
books.add("java");
books.add(" !");
System.out.println(books);
books.add(1, " 我 ");
System.out.println(books);
for(int i=0;i<books.size();i++){
System.out.println(books.get(i));
}
books.remove(2);
//删除
System.out.println(books);
//判断所在的位置
System.out.println(books.indexOf("hello"));
books.set(1, "你");
//修改
System.out.println(books);
System.out.println(books.subList(1, 3));
//截取
}
}
Map:
Object Put(Object key,Object value)如存在则更新,如不存在则添加
public class MapTest {
public static void main(String[] args) {
Map<Object, Object> map=new HashMap<>();
map.put("a", "啊");
map.put("b", "吧");
map.put("c", "测");
map.put("d", "的");
System.out.println(map.put("e", "额"));
System.out.println(map.put("d", "的"));
System.out.println(map.put("d", "对"));
System.out.println(map);
System.out.println(map.containsKey("f"));
System.out.println(map.containsValue("对"));
for(Object key:map.keySet()){
System.out.println(key+"==>"+map.get(key));
}
for(Object value:map.values()){
System.out.println("==>"+value);
}
map.remove("a");
System.out.println(map);
map.clear();
System.out.println(map);
}
}
Properties:
properties就是一个key、value都是String类型的Map
public class PropertiesTest {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties props = new Properties();
props.setProperty("name", "屁股王");
props.setProperty("password", "pgone");
props.store(new FileOutputStream("a.ini"), "comment line");
Properties props2 = new Properties();
props2.setProperty("gender", "male");
props2.load(new FileInputStream("a.ini"));
System.out.println(props2);
}
}
本地缓存的应用:
public class ConcurrentHashMapTest {
private static ConcurrentHashMap<String, String> cacheMap = new ConcurrentHashMap<>();
/**
* 获取缓存的对象
*
* @param account
* @return String
*/
public static String getCache(String key, String value) {
key = getCacheKey(key);
// 如果缓冲中有该账号,则返回value
if (cacheMap.containsKey(key)) {
return cacheMap.get(key);
}
// 如果缓存中没有该账号,把该帐号对象缓存到concurrentHashMap中
initCache(key, value);
return cacheMap.get(key);
}
/**
* 初始化缓存
*
* @param key
*/
private static void initCache(String key, String value) {
// 一般是进行数据库查询,将查询的结果进行缓存
cacheMap.put(key, value);
}
/**
* 拼接一个缓存key
*
* @param key
*/
private static String getCacheKey(String key) {
return Thread.currentThread().getId() + "-" + key;
}
/**
* 移除缓存信息
*
* @param key
*/
public static void removeCache(String key) {
cacheMap.remove(getCacheKey(key));
}
}
HashSet:
HashSet是Set接口的典型实现,大多数情况下使用Set集合都是HashSet的特点:1、顺序会发生变化,2、HashSet是不能同步的,3、集合元素的值可以为null
class A{
public boolean equals(Object obj){
return true;
}
}
class B{
public int hashCode(){
return 1;
}
}
class C{
public int hashCode(){
return 2;
}
public boolean equals(Object obj){
return true;
}
}
public class HashSetTest {
public static void main(String[] args) {
HashSet<Object> books=new HashSet<>();
books.add(new A());
books.add(new A());
books.add(new B());
books.add(new B());
books.add(new C());
books.add(new C());
System.out.println(books);
}
}
LinkedHashSet:
LinkedHashSet集合的元素与元素添加的顺序是一致的
public class LinkedHashSetTest {
public static void main(String[] args) {
LinkedHashSet<String> books=new LinkedHashSet<>();
books.add("java");
books.add("hello");
System.out.println(books);
books.remove("java");
books.add("java");
System.out.println(books);
}
}
TreeSetTest:
public class TreeSetTest {
public static void main(String[] args) {
TreeSet<String> nums=new TreeSet<>();
nums.add("a");
nums.add("abc");
nums.add("d");
nums.add("5");
nums.add("16");
nums.add("10");
nums.add("-9");
nums.add("57");
nums.add("0");
System.out.println(nums);
System.out.println(nums.first());
System.out.println(nums.last());
System.out.println(nums.headSet("4"));
//返回小于4的子集,不包含4
System.out.println(nums.tailSet("5"));
//返回大于5的子集,包含5
System.out.println(nums.subSet("-3", "4"));
//返回大于等于-3、小于4的子集
}
}