将list中某个元素放在首位

需求:程序开发过程中,业务方要求在展示国家列表时要将US放在首位,其余按照字母顺序排序

思路:遍历获取US暂时保存,然后删除list中的US,最后将US放在首位

问题:遍历的同时进行删除操作是不允许的

解决:

tcCountryTelCodes = ServiceUtil.tcCountryTelCodeService
		.getCountryNameList(); // 国家区号信息
// 将美国放在列表第一位
TcCountryTelCodeDTO temp = null ;
for(TcCountryTelCodeDTO countryTelCode : tcCountryTelCodes ){
	if("United States".equals(countryTelCode.getCountryname())){
		temp = countryTelCode ;
		break;
	}
}
tcCountryTelCodes.remove(temp);
tcCountryTelCodes.add(0, temp);

你可能感兴趣的:(list)