Map, List 用法(java篇)

Map,是指映射表,java里主要是HashMap,代码用法如下

Map map = new HashMap();

map.put("key", obj);  //插入元素,key:键, obj:对象

map.remove("key");  //移除元素, key:键

map.clear();    //清空

List, 是指一个列表,于数组类似

List[<数据类型>] list = new ArrayList[<数据类型>](); //实例化

list.add(a);    //默认添加
list.add(index, e);    //指定下标添加(添加下标后的元素向后挪一位),

list.remove(e);    //删除元素
list.remove(index);    //直接删除制定下标的元素(只删除找到的第一个相符合的元素)

list.set(index, e);    //替换元素(指定下标)
list.get(index);        //取出指定下标的元素
list.clear();        //清空集合

list.contains(e);       //判断集合中是否存在某个元素(存在返回true,否则返回false)

list.equals(list2);   //对比两个对象一定相等,
list.hashCode() == list2.hashCode();    //两个对象不一定相等
list.lastIndex(e); //文件存在,则返回找到的最后一个元素的下标,不存在则返回-1;
list.isEmpty();  //判断是否为空,空返回true,
list.iterator();    //返回Iterator的集合对象
list.toString();  //将集合转换为字符串

list.subList(fromIndex,toIndex);    //截取集合(从fromIndex开始在toIndex前结束
list.toArray();    //将集合转换为数组
list.toArray(object);    //将集合转换为指定类型的数组
原文地址:https://www.cnblogs.com/david-zhao/p/5787963.html