/* Sorting Map and return LinkedHashMap, u can send HashMap implemented Object but it should a map like
Map map = new HashMap();
ma p = sortHashMapByValues(map);
*/
public LinkedHashMap sortHashMapByValues(Map passedMap) {
List mapKeys = new ArrayList(passedMap.keySet());
List mapValues = new ArrayList(passedMap.values());
Collections.sort(mapValues);
Collections.sort(mapKeys);
LinkedHashMap sortedMap =
new LinkedHashMap();
Iterator valueIt = mapValues.iterator();
while (valueIt.hasNext()) {
Object val = valueIt.next();
Iterator keyIt = mapKeys.iterator();
while (keyIt.hasNext()) {
Object key = keyIt.next();
String comp1 = passedMap.get(key).toString();
String comp2 = val.toString();
if (comp1.equals(comp2)){
passedMap.remove(key);
mapKeys.remove(key);
sortedMap.put((String)key, val);
break;
}
}
}
return sortedMap;
}
Let us share our views in java. Not only java, we can also share examples and configurations on Spring, Hibernate, javascript, CSS and Sqlserver queries. This blog will help you in giving the complete information and resolving your complete java related problems like the problems in core java, servlets, jsp spring, hibernate. And also the web technologies which includes all css related problems(Web 2.0).and java script and the latest smart jquery java script frame work
Mar 22, 2009
Mar 18, 2009
Sql server pagination query
select* from ( SELECT ROW_NUMBER() OVER (ORDER BY ja.AppName) AS RowNumber, ja.* from Applicant ja )a where a.RowNumber <= uplt and a.RowNumber > lowlt
Oct 16, 2008
how to use ACEGI filter
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/javascript/**=#NONE#
/css/**=#NONE#
/images/**=#NONE#
/sounds.*=#NONE#
/secureimg=#NONE#
/**/tmplt.*=#NONE#
/**/.*popup.xhtml=securityFilter
/**/*.jsp=securityFilter
/**/*.xhtml=channelProcessingFilter,httpSessionContextIntegrationFilter,logoutFilter,casProcessingFilter,basicProcessingFilter,exceptionTranslationFilter,filterInvocationInterceptor,securityFilter
/**=httpSessionContextIntegrationFilter,logoutFilter,casProcessingFilter,basicProcessingFilter,exceptionTranslationFilter,filterInvocationInterceptor,securityFilter
Jul 12, 2008
Force authentication with Java Mail API
package com;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
public class EmailTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
String[] strarr = { "recipient@email.addresses" };
postMail(strarr, "Test u", "Test Mesage", "from@email.address", false);
System.out.println("Mail sent!...");
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void postMail(String recipients[], String subject, String message, String from, boolean isHtml) throws MessagingException {
boolean debug = false;
// Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host","hostipaddr");
String hostEmail = "admin@mail.com";
String pwd = "password";
// create some properties and get the default Session
// Session session = Session.getDefaultInstance(props, null);
Session session = Session.getInstance(props, new ForcedAuthenticator(hostEmail,pwd));
session.setDebug(debug);
// create a message
MimeMessage msg = new MimeMessage(session);
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(msg);
mimeMessageHelper.setTo(recipients);
mimeMessageHelper.setFrom(from);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setReplyTo(from);
mimeMessageHelper.setText(message, isHtml);
Transport.send(mimeMessageHelper.getMimeMessage());
}
}
class ForcedAuthenticator extends Authenticator {
String username;
String password;
public ForcedAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.username, this.password);
}
}
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
public class EmailTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
String[] strarr = { "recipient@email.addresses" };
postMail(strarr, "Test u", "Test Mesage", "from@email.address", false);
System.out.println("Mail sent!...");
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void postMail(String recipients[], String subject, String message, String from, boolean isHtml) throws MessagingException {
boolean debug = false;
// Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host","hostipaddr");
String hostEmail = "admin@mail.com";
String pwd = "password";
// create some properties and get the default Session
// Session session = Session.getDefaultInstance(props, null);
Session session = Session.getInstance(props, new ForcedAuthenticator(hostEmail,pwd));
session.setDebug(debug);
// create a message
MimeMessage msg = new MimeMessage(session);
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(msg);
mimeMessageHelper.setTo(recipients);
mimeMessageHelper.setFrom(from);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setReplyTo(from);
mimeMessageHelper.setText(message, isHtml);
Transport.send(mimeMessageHelper.getMimeMessage());
}
}
class ForcedAuthenticator extends Authenticator {
String username;
String password;
public ForcedAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.username, this.password);
}
}
Jun 9, 2008
Wildcard To Regex in Java
This code works like wild card expression in java. there is no in-built methods to identify which are using wild card chars '?', and '*'.
Every programmer know about these('?','*') chars how there are working.
Here the code snippet to work like their behavior
public static String wildcardToRegex(String wildcard){
StringBuffer s = new StringBuffer(wildcard.length());
s.append('^');
for (int i = 0, is = wildcard.length(); i < is; i++) {
char c = wildcard.charAt(i);
switch(c) {
case '*':
s.append(".*");
break;
case '?':
s.append(".");
break;
// escape special regexp-characters
case '(': case ')': case '[': case ']': case '$':
case '^': case '.': case '{': case '}': case '|':
case '\\':
s.append("\\");
s.append(c);
break;
default:
s.append(c);
break;
}
}
s.append('$');
return(s.toString());
}
Every programmer know about these('?','*') chars how there are working.
Here the code snippet to work like their behavior
public static String wildcardToRegex(String wildcard){
StringBuffer s = new StringBuffer(wildcard.length());
s.append('^');
for (int i = 0, is = wildcard.length(); i < is; i++) {
char c = wildcard.charAt(i);
switch(c) {
case '*':
s.append(".*");
break;
case '?':
s.append(".");
break;
// escape special regexp-characters
case '(': case ')': case '[': case ']': case '$':
case '^': case '.': case '{': case '}': case '|':
case '\\':
s.append("\\");
s.append(c);
break;
default:
s.append(c);
break;
}
}
s.append('$');
return(s.toString());
}
Subscribe to:
Posts (Atom)