java集合常见的处理

不积硅步无以至千里 不积小溪无以成江海

1.遍历ArrayList方法

/**
*这种方式在循环执行过程中会进行数据锁定,性能稍差.同时,如果想在遍历过程 
*中去掉某个元素,只能调用迭代器it的remove方法,不能使用集合list的
*remove方法,否则一定出现并发访问的错误. 
 */   
for(Iterator<String> it = list.iterator();it.hasNext(); ){   }    

/**
*底层还是调用第一种,只是简写了而已,因此比Iterator 慢,这种循环方式还其
*他限制, 不建议使用它,但是代码简洁。数据量不大可采用
*/   
for(String data : list){   }

/**
*内部不锁定,效率最高,但是当写多线程时要考虑并发操作的问题。以上方法调时
*必须确保list会初始化,不然会爆空指针异常  
**/
for(int    i=0;    i<list.size();    i++)    {   
       ...   
   }   

2.对map的遍历

//JDK1.4中, 键值对遍历
Iterator it = map .entrySet().iterator();
while (it.hasNext()) {
   Map.Entry entry = (Map.Entry) it.next();
   Object key = entry.getKey();
   Object value = entry.getValue();
   System.out.println("key=" + key + " value=" + value);
}
//JDK1.5中,应用新特性For-Each循环  ----键值对遍历
for (Map.Entry<String, Integer> entry : map .entrySet()) {
   String key = entry.getKey().toString();
   String value = entry.getValue().toString();
   System.out.println("key=" + key + " value=" + value);
}
//  HashMap keySet() 遍历 ----遍历key
 for (Iterator i = map .keySet().iterator(); i.hasNext();) {
   Object obj = i.next();
   System.out.println("key=" + obj + " value=" + map .get(obj));
  }

 // 遍历方法四 map keySet()遍历 ----遍历key ,代码最简洁
 for (Object o : map .keySet()) {
   System.out.println("key=" + o + " value=" + map .get(o));
  }

3.list,array,set,map相互转化

将map的values转成成list

//复杂实现
public static <T> List<T> mapTransitionListList(Map<T,T> map){
    List<T> list=new ArrayList<>();
    Iterator iter = map.entrySet().iterator();
    while(iter.hasNext()){
         Entry entry = (Entry) iter.next();
         list.add((T) entry.getValue());
    }
    return list;
}

//简单实现
public static <T> List<T> mapTransitionListList2(Map<T,T> map){      
    List<T> list= new ArrayList<T>(map.values());             
    return list;
 }

List–>数组

 List<String> list = new ArrayList<String>();   
 //第一种,将转化后的数组放入已经创建好的对象中   
 String[] strings1 = new String[list.size()];     
 list.toArray(strings1);  
 //第二种,将转化后的数组赋给新对象       
 String[] strings2 = list.toArray(new String[0]);

数组–>List



String[] ss = {"JJ","KK"};     
List<String> list1 = Arrays.asList(ss);     
List<String> list2 = Arrays.asList("AAA","BBB");  
//通过查看API,Arrays.asList(ss)表示返回一个新的的ArrayList    
@SafeVarargs   
public static <T> List<T> asList(T... a) {
       return new ArrayList<>(a);   
}

Set–>List

List<String> l = new ArrayList<String>(new HashSet<String>());  

List–>Set

Set<String> set = new HashSet<String>(new ArrayList<String>()); 

数组–>Set

 String[] strs = {"a","b"};     
 Set<String> set = new HashSet<String>(Arrays.asList(strs));     

Set–>数组

Set<String> set = new HashSet<String>(Arrays.asList("PP","OO"));     
String[] strSet = new String[set3.size()];     
set3.toArray(strSet);     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值