首页 > 网站 > WEB开发 > 正文

ArrList、HashSet、HashMap 的遍历和区别

2024-04-27 15:13:56
字体:
来源:转载
供稿:网友

一、ArrList 的遍历

(1)       

public List<String>  getArrayList(){    List<String>    list =new ArrayList<String>();    list.add("1");    list.add("2");    list.add("2");    list.add("3");    list.add("4");    list.add("5");    list.add("6");
    list.add(" ");    return list;}

for (int i = 0, len = list.size(); i < len; i++) {     String s = list.get(i);     Log.e("TAG", s.toString()); }

for(String s :list){    Log.e("TAG", s.toString());}

Iterator<String> iterator =list.iterator(); while (iterator.hasNext()){     String s = iterator.next();     Log.e("TAG", s.toString()); }

打印结果:

02-08 10:34:19.549 17555-17555/com.testopensourceapplication.citydemo E/TAG: 102-08 10:34:19.549 17555-17555/com.testopensourceapplication.citydemo E/TAG: 202-08 10:34:19.549 17555-17555/com.testopensourceapplication.citydemo E/TAG: 202-08 10:34:19.549 17555-17555/com.testopensourceapplication.citydemo E/TAG: 302-08 10:34:19.549 17555-17555/com.testopensourceapplication.citydemo E/TAG: 402-08 10:34:19.549 17555-17555/com.testopensourceapplication.citydemo E/TAG: 502-08 10:34:19.549 17555-17555/com.testopensourceapplication.citydemo E/TAG: 602-08 10:34:19.549 17555-17555/com.testopensourceapplication.citydemo E/TAG:  

  

特性:ArrayList  可以重复 和可以为空       

           如果需要删除的时候,只能使用Iterator

二、HashSet 的遍历

                

public Set<String> getHashSet(){   Set<String> set = new HashSet<>();        set.add("1");        set.add("2");        set.add("2");        set.add("3");        set.add("4");        set.add("5");        set.add("6");        set.add(" ");        return  set;}

Set<String> set =getHashSet();for(String value : set){    Log.e("TAG",value);}

   

Iterator<String> iterator = set.iterator();while (iterator.hasNext()){    String value =iterator.next();    Log.e("TAG",value);}

  特性:HashSet 存入和输出数据的顺序不一定,元素不可重复

              Hash Set 是通过hashCode 和equals 两个方法 来保证元素的唯一性

              HashCode 值相同,才会判断equals 是否为true

              HashCode 值不同,那么就不调用equals

02-08 13:07:47.768 8960-8960/com.testopensourceapplication.citydemo E/TAG: 502-08 13:07:47.768 8960-8960/com.testopensourceapplication.citydemo E/TAG: 402-08 13:07:47.768 8960-8960/com.testopensourceapplication.citydemo E/TAG: 102-08 13:07:47.768 8960-8960/com.testopensourceapplication.citydemo E/TAG: 302-08 13:07:47.768 8960-8960/com.testopensourceapplication.citydemo E/TAG:  02-08 13:07:47.768 8960-8960/com.testopensourceapplication.citydemo E/TAG: 602-08 13:07:47.768 8960-8960/com.testopensourceapplication.citydemo E/TAG: 2

三、HashMap 的遍历

public HashMap<String,String> getHashMap(){    HashMap<String,String> map =new HashMap<>();    map.put("a","1");    map.put("a","7");    map.put(" ","2");    map.put("b","2");    map.put("c","3");    map.put("d","4");    map.put("e","5");    map.put("f","6");    return map;}

Map<String,String> map =getHashMap();

for(Map.Entry<String,String> entry :map.entrySet()){     Log.e("TAG", "key" + entry.getKey() + "-----" + "value:" + entry.getValue()); }

Iterator<Map.Entry<String,String>> iterator  = map.entrySet().iterator(); while (iterator.hasNext()){     Map.Entry<String,String> entry =iterator.next();     Log.e("TAG", "key" + entry.getKey() + "-----" + "value:" + entry.getValue()); }

/** * 方式4,通过键遍历(效率低) */

for(String key :map.keySet()){    String value = map.get(key);    Log.e("TAG", "key:" + key + "Value:" + value);}

/** * 方式5,只需要key集合或者value集合时候使用 */

 for(String key :map.keySet()){        Log.e("TAG","key:"+key);    }

for(String value :map.values()){    Log.e("TAG","value:"+value);}

特性:

 HashMap 可以坚守null 键值和值  存储是键值对的形式   键没有重复,没有顺序map 添加存在相同key的时候,其value会覆盖前面的value。

02-08 13:38:44.364 16864-16864/com.testopensourceapplication.citydemo E/TAG: key:dValue:402-08 13:38:44.364 16864-16864/com.testopensourceapplication.citydemo E/TAG: key:aValue:702-08 13:38:44.364 16864-16864/com.testopensourceapplication.citydemo E/TAG: key:fValue:602-08 13:38:44.364 16864-16864/com.testopensourceapplication.citydemo E/TAG: key:bValue:202-08 13:38:44.364 16864-16864/com.testopensourceapplication.citydemo E/TAG: key:cValue:302-08 13:38:44.364 16864-16864/com.testopensourceapplication.citydemo E/TAG: key: Value:202-08 13:38:44.364 16864-16864/com.testopensourceapplication.citydemo E/TAG: key:eValue:5


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表