Java小知識,

來源:互聯網
上載者:User

Java小知識,

1.轉換編碼:
str = new String(str.getBytes("ISO-8859-1"),"utf-8");
**************************************************************
2.資料類型之間的轉換:
int a=(int)b;
int a=b.intValue();
int a=Ingtger.parseInt(b);
Integer a= new Integer(b);
int i=Integer.valueOf(b).intValue();
String類型:
String a=b.toString();
String a=b+"";
a.startsWith(b);//前包含
a.endsWith(b);//後包含
a.equals(b);//等於
a.equalsIgnoreCase (b);//忽略大小寫
a.contains(b);//包含
****************************************************************
3.double資料類型的大小比較
    double a = 0.01;  
    double b = 0.001;  
    BigDecimal data1 = new BigDecimal(a);  
    BigDecimal data2 = new BigDecimal(b);  
    double result=data1.compareTo(data2);
    data1>data2  result=1;
    data1<data2  result=-1;
    data1=data2  result=0;
**************************************************************************
4.將IP轉化為Long型的公式:
     String[] oraginal = this.value.split("\\.");
     long o = 0l;//原值
     o += Long.parseLong(oraginal[i]) << (24 - i * 8);
************************************************************************************
5.得到當前方法的名字
String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
**********************************************************************************************
6.單一實例Singleton 樣本
public class SimpleSingleton {
  private static SimpleSingleton singleInstance = new SimpleSingleton();
   
  //Marking default constructor private
  //to avoid direct instantiation.
  private SimpleSingleton() {
  }
   
  //Get instance for class SimpleSingleton
  public static SimpleSingleton getInstance() {
   
    return singleInstance;
  }
}
*******************************************************************************************************
7.加密解密
Encrypt.encode("admin");
Encrypt.decode(str1),"utf-8")
***************************************************************************************************************
8.mongodb的一些用法
DBCursor result1 = mongoTemplates.firstMongoTemplate().getCollection("alarm").find(bdb, keys).sort(new BasicDBObject("_id", -1)).limit(1000);
Gson gson = new Gson();
while (result1.hasNext()) {
   DBObject obj = result1.next();
   Map<String, Object> mapp = obj.toMap();
   Iterator<Map.Entry<String, Object>> entries = mapp.entrySet().iterator();
   while (entries.hasNext()) {
     Map.Entry<String, Object> entry = entries.next();
   }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.