on 04-10-2007 11:08 AM
how to i add more than 1 records in Map
and loop in the Map
Map map=new HashMap();
map.put("InvalidAddress","andrewmilleranderson@hotmail.com");
map.put("sender","johnson@yahoo.com");
map.put("subject","New Message");
map.put("InvalidAddress","jul@hotmail.com");
map.put("sender","johnson@yahoo.com");
map.put("subject","New Message");
mailSLS.updateOutMailAck(map);
Hi,
You cannot add duplicate key value.
the key value needs to be unique.
ex
map.put("InvalidAddress","andrewmilleranderson@hotmail.com");
<b>map.put("sender","johnson@yahoo.com");</b>
map.put("subject","New Message");
map.put("InvalidAddress","jul@hotmail.com");
<b>map.put("sender","johnson@yahoo.com");</b>
map.put("subject","New Message");
by default map overwrites duplicate key
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
while loop found then exit the loop
how can i do this
since the subject and sender is the same in arraylist
if(clsinvalidMailAck.getSender()!=null && clsinvalidMailAck.getSender()!="" &&
clsinvalidMailAck.getSubject()!=null && clsinvalidMailAck.getSubject()!=""){ strSender=clsinvalidMailAck.getSender();
strSubject=clsinvalidMailAck.getSubject();
}
the final result i get is like this
String strInvalidMail="andadfcaasdadfadf@hotmail.com,johnnieafadfalker@yahoo.com,"
strInvalidMail.subString(0,strInvalidMail.length()-1);
will eliminate the colon ,
how to i addd 'xxxx.com','xxxx.com'
based on
InvalidMailAck invalidMailAck=new InvalidMailAck();
invalidMailAck.setInvalidAddress("johnniewsdfafasdfalker@yahoo.com");
invalidMailAck.setSender("");
invalidMailAck.setSubject("");
arrRet.add(invalidMailAck);
InvalidMailAck invalidMailAck1=new InvalidMailAck();
invalidMailAck1.setInvalidAddress("");
invalidMailAck1.setSender("ph.won@emi-solutions.biz ");
invalidMailAck1.setSubject("zap");
arrRet.add(invalidMailAck1);
InvalidMailAck invalidMailAck2=new InvalidMailAck();
invalidMailAck2.setInvalidAddress("andadfcaasdadfadf@hotmail.com");
invalidMailAck2.setSender("");
invalidMailAck2.setSubject("");
arrRet.add(invalidMailAck2);
InvalidMailAck invalidMailAck3=new InvalidMailAck();
invalidMailAck3.setInvalidAddress("");
invalidMailAck3.setSender("ph.won@emi-solutions.biz ");
invalidMailAck3.setSubject("zap");
arrRet.add(invalidMailAck3);
InvalidMailAck invalidMailAck4=new InvalidMailAck();
invalidMailAck4.setInvalidAddress("johnniewsdfafasdfalker@yahoo.com");
invalidMailAck4.setSender("");
invalidMailAck4.setSubject("");
arrRet.add(invalidMailAck4);
InvalidMailAck invalidMailAck5=new InvalidMailAck();
invalidMailAck5.setInvalidAddress("");
invalidMailAck5.setSender("ph.won@emi-solutions.biz ");
invalidMailAck5.setSubject("zap");
arrRet.add(invalidMailAck5);
ArrayList arrResult=new ArrayList();
String strSender="";
String strSubject="";
String strInvalidMail="";
boolean found2=true;
System.err.println("Size:" +arrRet.size());
if(arrRet.size()>0){
for(Iterator it=arrRet.iterator();it.hasNext();){
InvalidMailAck clsinvalidMailAck=(InvalidMailAck)it.next();
if(clsinvalidMailAck.getInvalidAddress()!=null && clsinvalidMailAck.getInvalidAddress()!=""){
arrResult.add(clsinvalidMailAck.getInvalidAddress());
}
if(clsinvalidMailAck.getSender()!=null && clsinvalidMailAck.getSender()!="" &&
clsinvalidMailAck.getSubject()!=null && clsinvalidMailAck.getSubject()!=""){ strSender=clsinvalidMailAck.getSender();
strSubject=clsinvalidMailAck.getSubject();
}
}
}
System.err.println("Sender:" +strSender);
System.err.println("Subject:" +strSubject);
System.err.println("Invalid Result >>" +arrResult.size());
// i get the arrResult array with duplicate values, i want to eliminates it
if(arrResult.size()>0){
for(Iterator it=arrResult.iterator();it.hasNext();){
String invalidMail=(String)it.next(); //class cast exception
String found=invalidMail;
strInvalidMail=strInvalidMail.replaceAll(found+",","");
strInvalidMail=strInvalidMail +found +",";
}
System.err.println("InvalidMail >>" +strInvalidMail);
}
Message was edited by:
yzme yzme
Message was edited by:
yzme yzme
Message was edited by:
yzme yzme
Hi,
You can use the below code to store and retrieve the records using HashMap.
1) Storing records using HashMap
//Create ArrayList
ArrayList newList = new ArrayList();
Map map = new HashMap ();
map.put("InvalidAddress","andrewmilleranderson@hotmail.com");
map.put("sender","johnson@yahoo.com");
map.put("subject","New Message");
newList.add (map);
map.put("InvalidAddress","jul@hotmail.com");
map.put("sender","johnson@yahoo.com");
map.put("subject","New Message");
newList.add (map);
//Retrieving records using HAshMap
ArrayList retrieveList = newList;
if( retrieveList != null ) {
for (int index = 0 ; index < retrieveList .size() ; index ++ ) {
HashMap row = new HashMap();
if (row != null){
row = (HashMap) retrieveList .get(index);
String InvalidAddress= (String) row.get("InvalidAddress");
String sender= (String) row.get("sender");
String subject= (String) row.get("subject");
}
}
}
Hope it helps.
Regards,
Suresh
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
while loop found then exit the loop
how can i do this
since the subject and sender is the same in arraylist
if(clsinvalidMailAck.getSender()!=null && clsinvalidMailAck.getSender()!="" &&
clsinvalidMailAck.getSubject()!=null && clsinvalidMailAck.getSubject()!=""){ strSender=clsinvalidMailAck.getSender();
strSubject=clsinvalidMailAck.getSubject();
}
the final result i get is like this
String strInvalidMail="andadfcaasdadfadf@hotmail.com,johnnieafadfalker@yahoo.com,"
strInvalidMail.subString(0,strInvalidMail.length()-1);
will eliminate the colon ,
how to i addd 'xxxx.com','xxxx.com'
yzme,
No way with your code -- Map by design overrides entries with same key.
So either use different keys like "InvalidAddress1" and "InvalidAddress2" or use arrays of values or better yet create JavaBean that holds parameters and use List instead of Map:
V1 (separate messages about every address):
Map map=new HashMap();
map.put("InvalidAddress1","andrewmilleranderson@hotmail.com");
map.put("sender1","johnson@yahoo.com");
map.put("subject1","New Message");
map.put("InvalidAddress2","jul@hotmail.com");
map.put("sender2","johnson@yahoo.com");
map.put("subject2","New Message");
V2 (single message about several addresses):
Map map=new HashMap();
map.put("InvalidAddress", new String[]{
"andrewmilleranderson@hotmail.com", "jul@hotmail.com"});
map.put("sender","johnson@yahoo.com");
map.put("subject","New Message");
V3
class MailParams {
public final String invalidAddress;
public final String sender;
public final String subject;
public MailParams(String pInvalidAddress, String pSender, String pSubject) {
invalidAddress = pInvalidAddress; sender = pSender, subject = pSubject;
}
}
...
List list = new ArrayList(2);
list.add( new MailParams(
"andrewmilleranderson@hotmail.com", "johnson@yahoo.com", "New Message"
) );
list.add( new MailParams(
"jul@hotmail.com", "johnson@yahoo.com", "New Message"
) );
Actually, this all depends on coding of mailSLS class -- definitely you need to alter it's source to support either of variants above.
Valery Silaev
SaM Solutions
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You cannot use a map in this way. The assignments in the second block will overwrite the values assigned in the frist block. Try to create a class to hold your values and use a java.util.List or java.util.Set instead of a map.
To loop over a map use this:
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
Object value = map.get(it.next());
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
...or more efficient form if you need both keys and values:
for (final Iterator i = map.entrySet().iterator(); i.hasNext(); ) {
final Map.Entry e = (Map.Entry)i.next();
final Object key = e.getKey();
final Object val = e.getValue();
}
Valery Silaev
SaM Solutions
User | Count |
---|---|
88 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.