十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
import java.util.ArrayList;
创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、做网站、宕昌网络推广、成都小程序开发、宕昌网络营销、宕昌企业策划、宕昌品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供宕昌建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class $ {
public static void main(String[] args) {
ListInteger data = new ArrayListInteger();
data.add(100);
data.add(58);
data.add(76);
data.add(24);
System.out.println(data);
// 升序
Collections.sort(data, new ComparatorInteger() {
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
});
System.out.println(data);
// 降序
Collections.sort(data, new ComparatorInteger() {
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
System.out.println(data);
}
}
基本注释都很清楚啊 Map.Entry 是 map映射项 键-值对
再没有什么了
经常我们要删除集合中的某些元素 有些可能会这么写
复制代码 代码如下: public void operate(List list){ for (Iterator it = list iterator(); it hasNext();) { String str = (String)it next(); if (str equals("chengang")){ list remove(str); } } }
这种写法一运行就会报如下异常
Exception in thread "main" java util ConcurrentModificationException at java util AbstractList$Itr checkForComodification(AbstractList java: )
因为list在循环中的时候是不可以删除它的元素的 后来我是这样做的 一种很笨的方法 思路是这样的 创建一个List专门存放要被删除的元素 循环过后 用List removeAll方法把元素删除 代码如下
复制代码 代码如下: public void operate(List list){ List removeList= new ArrayList(); for (Iterator it = list iterator(); it hasNext();) { String str = (String)it next(); if (str equals("chengang")){ removeList add(str); } } list removeAll(removeList); }
这样也确实可以解决问题了 但是方法实在太笨重 其实可以有更简单的更高效的方法 就是用Iterator remove方法 如下
复制代码 代码如下: for (Iterator it = list iterator(); it hasNext();) { String str = (String)it next(); if (str equals("chengang")){ it remove(); } } lishixinzhi/Article/program/Java/JSP/201311/19832
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static String str = "[{TIME=2006-06, PRICE=246.91388889, AccountMethod=7, BW_VALUE=88888}, {TIME=2006-07, PRICE=305611.11111111, AccountMethod=1, BW_VALUE=7070120}, {TIME=2006-08, PRICE=246.91388889, AccountMethod=1, BW_VALUE=88888}, {TIME=2006-08, PRICE=246.91388889, AccountMethod=7, BW_VALUE=7777}]";
public static ListDemo demos = new ArrayListDemo();
public static MapString, Demo maps = new HashMapString, Demo();
public static Pattern p = Pattern点抗 pile("\\{.*?\\}");
public static Pattern p_time = Pattern点抗 pile("TIME=.*?,");
public static Pattern p_price = Pattern点抗 pile("PRICE=.*?,");
public static Pattern p_account = Pattern点抗 pile("AccountMethod=.*?,");
public static Pattern p_value = Pattern点抗 pile("BW_VALUE=.*?\\}");
public static void main(String[] args) {
Matcher m = p.matcher(str);
while (m.find()) {
Demo demo = null;
String s = m.group();
String t = null;
Matcher m2 = null;
m2 = p_time.matcher(s);
if (m2.find()) {
t = m2.group();
t = t.substring(5, t.length() - 1);
if (maps.containsKey(t)) {
demo = maps.get(t);
m2 = p_price.matcher(s);
if (m2.find()) {
String p = m2.group();
demo.setPRICE(demo.getPRICE() + "+"
+ p.substring(6, p.length() - 1));
}
m2 = p_account.matcher(s);
if (m2.find()) {
String a = m2.group();
a = a.substring(14, a.length() - 1);
if (a.equals("7")) {
m2 = p_value.matcher(s);
if (m2.find()) {
String v = m2.group();
v = v.substring(9, v.length() - 1);
demo.setFree(Double.parseDouble(v)
+ demo.getFree());
}
} else {
m2 = p_value.matcher(s);
if (m2.find()) {
String v = m2.group();
v = v.substring(9, v.length() - 1);
demo.setCharge(Double.parseDouble(v)
+ demo.getCharge());
}
}
}
} else {
demo = new Demo();
demo.setTIME(t);
m2 = p_price.matcher(s);
if (m2.find()) {
String p = m2.group();
demo.setPRICE(p.substring(6, p.length() - 1));
}
m2 = p_account.matcher(s);
if (m2.find()) {
String a = m2.group();
a = a.substring(14, a.length() - 1);
if (a.equals("7")) {
m2 = p_value.matcher(s);
if (m2.find()) {
String v = m2.group();
v = v.substring(9, v.length() - 1);
demo.setFree(Double.parseDouble(v));
}
} else {
m2 = p_value.matcher(s);
if (m2.find()) {
String v = m2.group();
v = v.substring(9, v.length() - 1);
demo.setCharge(Double.parseDouble(v));
}
}
}
}
}
maps.put(t, demo);
}
System.out.println(getResult());
}
public static String getResult() {
StringBuffer sb = new StringBuffer();
sb.append("[");
IteratorString iter = maps.keySet().iterator();
while (iter.hasNext()) {
String key = iter.next();
Demo demo = maps.get(key);
String temp = null;
if (demo.getFree() != 0) {
if (demo.getCharge() != 0) {
temp = "{TIME=" + demo.getTIME() + ",PRICE="
+ demo.getPRICE() + ",charge=" + demo.getCharge()
+ ",free=" + demo.getFree() + "}";
} else {
temp = "{TIME=" + demo.getTIME() + ",PRICE="
+ demo.getPRICE() + ",free=" + demo.getFree() + "}";
}
} else {
if (demo.getCharge() != 0) {
temp = "{TIME=" + demo.getTIME() + ",PRICE="
+ demo.getPRICE() + ",charge=" + demo.getCharge()
+ "}";
} else {
temp = "{TIME=" + demo.getTIME() + ",PRICE="
+ demo.getPRICE() + "}";
}
}
sb.append(temp + ",");
}
sb.deleteCharAt(sb.length() - 1);
sb.append("]");
return sb.toString();
}
}
其中的Demo就是一个javabean。