php map 转 arraylist,1、Collection 、 Map和Array 介绍和各种操作

1、Collection是一个接口,他有两个子接口List和Set

1、List实现类===>Arraylist、 LinkedList和Vector //基于数组实现,每次增加1.5倍空间,非线程安全

ArrayList arrayList = new ArrayList();

//基于数组实现,每次增加2倍空间,线程安全

Vector vector = new Vector();

//基于list实现,插入、删除快,查询慢

LinkedList stringLinkedList = new LinkedList<>();

2、Set实现类: new HashSet<>() //这个用的最多 new TreeSet<>()

new SortedSet<>()

new LinkedHashSet<>()

2、Collection 和 Map 的遍历

public class CollectionAndMap {

public static void main(String[] args) {

ArrayList arrayList = new ArrayList();

arrayList.add("name = liufu");

arrayList.add("gender = men");

forEarchList(arrayList);

HashMap hashMap = new HashMap();

hashMap.put("name", "liufu");

hashMap.put("gender", "nan");

iteratorMap(hashMap);

Hashtable map = System.getProperties();

iteratorMap(map);

}

public static void forEarchList(List list){

long startTime = System.currentTimeMillis();

for (String valueString : list){

System.out.println(valueString);

}

System.out.println(System.currentTimeMillis() - startTime);

}

public static void iteratorMap(Map map){

long startTime = System.currentTimeMillis();

//map则需要先将其转化为Set然后再获取Iterator迭代器来变了

Set> set = map.entrySet();

for (Entry entry : set){

System.out.println(entry);

}

System.out.println(System.currentTimeMillis() - startTime);

}

}

3、Collection和array之间的相互转换

LinkedList strings = new LinkedList<>();

strings.add("name");

strings.add("age");

strings.add("addr");

String[] stringArr = (String[]) strings.toArray();

String[] strings = new String[100];

strings[0] = "name";

List stringList = Arrays.asList(strings);

4、map.values使用

注意:如果是hashmap拿出来的顺序和插入进去的顺序不一致,如果想要一致,则使用LinkedHashMap

Map.values返回一个collecton集合

要想把它转化为ArrayList或者LinkedList,不能如下使用(会卡死)

List values = (List) map.values();

而是要

ArrayList arraylist = new ArrayList<>();

arraylist.addAll(map.values());

或者

ArrayList alarmInfos1 = new ArrayList<>(map.values());

如果想要转化为数组

String[] alarmInfos1 = new String[10];

map.values().toArray(alarmInfos1);

5、有顺序的hashmap==> linkedHashMap

LinkedHashMap map = new LinkedHashMap<>();

AlarmInfo alarmInfo = new AlarmInfo();

alarmInfo.setServiceCode("123");

map.put("123", alarmInfo);

AlarmInfo alarmInfo1 = new AlarmInfo();

alarmInfo1.setServiceCode("456");

map.put("456", alarmInfo1);

AlarmInfo alarmInfo2 = new AlarmInfo();

alarmInfo2.setServiceCode("789");

map.put("789", alarmInfo2);

AlarmInfo alarmInfo3 = new AlarmInfo();

alarmInfo3.setServiceCode("xiugai");

map.put("123", alarmInfo3);

for (Map.Entry entry : map.entrySet()){

System.out.println(entry.getKey() + "==>" + entry.getValue().getServiceCode());

}

结果是

ba5daafaf04b9b7910905ba69ff3f9a3.png

6、hashmap的特殊用法

HashMap result = new HashMap(1) {{

put("count", "abc");

if (!containsKey("count")) {

put("count", "bcd");

}

}};

System.out.println(result.get("count"));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值