First, the string splicing tool class
Package blank.util;
Import Java.util.Iterator;
Import Java.util.Map;
Import Java.util.Set;
Import Java.util.TreeMap;
Import Java.util.Vector;
Import Java.util.Map.Entry;
public class Stringutil {
public static string Jointstr (string sid,string ID) {
Vector<string> ids=new vector<string> ();
if (Sid!=null&&sid.equals (ID)) {
return SID;
}else{
Split
String[] Sids=sid.split (":");
if (sids.length==1) {
Return id+ ":" +SID;
}else{
for (String i:sids) {
Ids.add (i);
}
if (Ids.contains (ID)) {
String first=ids.firstelement ();
if (First.equals (ID)) {
return SID;
}else{
Delete First
Ids.remove (ID);
Ids.add (0, id);
String newvalue= "";
for (int i=0;i<ids.size (); i++) {
String Value=ids.elementat (i);
if (i==0) {
Newvalue=value;
}
else{
Newvalue=newvalue+ ":" +value;
}
}
return newvalue;
}
}else{
Return id+ ":" +SID;
}
}
}
}
public static string Jointstr (string ids,string id,integer num) {
Map<string,integer> map=new treemap<string, integer> ();
if ("". Equals (IDs)) {
Return id+ ":" +num;
}else{
String[] Tids=ids.split (",");
if (tids!=null) {
for (String tid:tids) {
String[] Idnum=tid.split (":");
Map.put (Idnum[0], integer.valueof (idnum[1]));
}
}
if (Map.containskey (ID)) {
Map.put (ID, map.get (ID) +num);
Set<entry<string, integer>> entities=map.entryset ();
Iterator<entry<string, integer>> iter=entities.iterator ();
int count=0;
String newvalue= "";
while (Iter.hasnext ()) {
Entry<string, integer> entity=iter.next ();
String Key=entity.getkey ();
Integer Value=entity.getvalue ();
String temp=key+ ":" +value;
if (count==0) {
Newvalue=temp;
}else{
Newvalue=newvalue+ "," +TEMP;
}
count++;
}
return newvalue;
}else{
Ids=ids+ "," +id+ ":" +num;
return IDs;
}
}
}
}
Description: The first method is used to record the ID number of the most recently browsed item, and the stitching form is 2:3:4. When there is repeated browsing, the original duplicates are deleted and the most recent browsing is placed before.
The second method is used to record the item ID and quantity of the shopping cart, and the stitching form is 2:1,3:4, which means that the number of goods with an ID of 2 1,id 3 is 4.
Javaweb Study Record (14)--shopping mall's string stitching to achieve the most recently viewed items and shopping cart features