67,515
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) {
String[] x = {"G666#0002","G111#0001","G111#0002","G111#0003","G222#0001","G222#0002","G222#0003","G555#0001","G666#0001"};
Arrays.sort(x);
List<String> list = new ArrayList<String>();
String st1 = "";
String st2 = "";
String temp="";
for(int i = 0 ;i<x.length;i++){
st1 = x[i].substring(0, x[i].indexOf('#'));
if(i<x.length-1){
st2 = x[i+1].substring(0, x[i+1].indexOf('#'));
}else{
st2 = "";
}
String end = x[i].substring(x[i].indexOf('#')+1);
if(st2.equals(st1)){
if(temp.equals(""))
temp = end;
}else{
String val = "";
if(!temp.equals(end)){
val= st1+"#"+temp+"-"+end;
} else{
val = st1+"#"+end;
}
list.add(val);
}
}
for(String y : list){
System.out.println("*********"+y);
}
}
}
String[] ss = {"G111#0001","G111#0002","G111#0003","G222#0001","G222#0002","G222#0003","G555#0001","G666#0001","G666#0002"} ;
Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>() ;
for (int i = 0; i < ss.length; i++) {
String[] strs = ss[i].split("#");
if(map.containsKey(strs[0])) {
Map<String, String> map2 = map.get(strs[0]) ;
map2.put(strs[1], strs[0]) ;
map.put(strs[0], map2 ) ;
}else{
Map<String, String> map2 = new LinkedHashMap<String, String>() ;
map2.put(strs[1], strs[0]) ;
map.put(strs[0], map2) ;
}
}
System.out.println(map.size());
String s = "" ;
int i = 0 ;
for (String key :map.keySet()) {
Map<String, String> map2 = map.get(key) ;
int j = 0 ;
for(String key2 :map2.keySet()) {
if(j==0)
s=s+ map2.get(key2) +"#"+key2 ;
else
if(j== map2.size()-1)
s= s+"-"+key2 ;
j=j+1;
}
if(i!=map.size()-1)
s=s+",";
i=i+1 ;
}
System.out.println(s);
可以了
String[] ss = {"G111#0001","G111#0002","G111#0003","G222#0001","G222#0002","G222#0003","G555#0001","G666#0001","G666#0002"} ;
Map<String, String> map = new HashMap<String, String>() ;
for (int i = 0; i < ss.length; i++) {
String[] strs = ss[i].split("#");
map.put(strs[0], strs[1]) ;
}
String [] newSs= new String[map.size()] ;
int i = 0 ;
for (String key :map.keySet()) {
if(map.get(key).equals("0001"))
newSs[i] =key+"#"+"0001" ;
else
newSs[i] =key+"#"+"0001-"+map.get(key) ;
i+=1;
}
for (int j = 0; j < newSs.length; j++) {
System.out.println(newSs[j]);
}
去试试吧