當前位置:首頁 » 編程語言 » stringtolistjava

stringtolistjava

發布時間: 2022-06-29 10:47:16

A. string字元串轉換list<int>

//請參考:
stringa="1,2,3,4,5";
List<int>list=a.Split(newstring[]{","},StringSplitOptions.RemoveEmptyEntries).Cast<int>().ToList<int>();

//歡迎追問。

B. java 將List中所有item中的某一個欄位值提出來生成一個新的List

as.forEach(n->a1s.add(n.a1));

C. java里如何判斷Email是否發送成功

package com.liuns.mail.test;

import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class MailTest {
//發送的郵箱 內部代碼只適用qq郵箱
private static final String USER = "[email protected]";

//授權密碼 通過QQ郵箱設置->賬戶->POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務->開啟POP3/SMTP服務獲取
private static final String PWD = "xxx";
private String[] to;
private String[] cc;//抄送
private String[] bcc;//密送
private String[] fileList;//附件
private String subject;//主題
private String content;//內容,可以用html語言寫
public void sendMessage() throws Exception {
// 配置發送郵件的環境屬性
final Properties props = new Properties();
//下面兩段代碼是設置ssl和埠,不設置發送不出去。
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
// 表示SMTP發送郵件,需要進行身份驗證
props.setProperty("mail.transport.protocol", "smtp");// 設置傳輸協議
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.qq.com");//QQ郵箱的伺服器 如果是企業郵箱或者其他郵箱得更換該伺服器地址
// 發件人的賬號
props.put("mail.user", USER);
// 訪問SMTP服務時需要提供的密碼
props.put("mail.password", PWD);

// 構建授權信息,用於進行SMTP進行身份驗證
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 用戶名、密碼
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用環境屬性和授權信息,創建郵件會話
Session mailSession = Session.getInstance(props, authenticator);
// 創建郵件消息
MimeMessage message = new MimeMessage(mailSession);
BodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
// 設置發件人
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form);
//發送
if (to != null) {
String toList = getMailList(to);
InternetAddress[] iaToList = new InternetAddress().parse(toList);
message.setRecipients(RecipientType.TO, iaToList); // 收件人
}
//抄送
if (cc != null) {
String toListcc = getMailList(cc);
InternetAddress[] iaToListcc = new InternetAddress().parse(toListcc);
message.setRecipients(RecipientType.CC, iaToListcc); // 抄送人
}
//密送
if (bcc != null) {
String toListbcc = getMailList(bcc);
InternetAddress[] iaToListbcc = new InternetAddress().parse(toListbcc);
message.setRecipients(RecipientType.BCC, iaToListbcc); // 密送人
}
message.setSentDate(new Date()); // 發送日期 該日期可以隨意寫,你可以寫上昨天的日期(效果很特別,親測,有興趣可以試試),或者抽象出來形成一個參數。
message.setSubject(subject); // 主題
message.setText(content); // 內容
//顯示以html格式的文本內容
messageBodyPart.setContent(content,"text/html;charset=utf-8");
multipart.addBodyPart(messageBodyPart);
//保存多個附件
if(fileList!=null){
addTach(fileList, multipart);
}
message.setContent(multipart);
// 發送郵件
Transport.send(message);
}

public void setTo(String[] to) {
this.to = to;
}

public void setCc(String[] cc) {
this.cc = cc;
}

public void setBcc(String[] bcc) {
this.bcc = bcc;
}

public void setSubject(String subject) {
this.subject = subject;
}

public void setContent(String content) {
this.content = content;
}

public void setFileList(String[] fileList) {
this.fileList = fileList;
}

private String getMailList(String[] mailArray) {
StringBuffer toList = new StringBuffer();
int length = mailArray.length;
if (mailArray != null && length < 2) {
toList.append(mailArray[0]);
} else {
for (int i = 0; i < length; i++) {
toList.append(mailArray[i]);
if (i != (length - 1)) {
toList.append(",");
}
}
}
return toList.toString();
}

//添加多個附件
public void addTach(String fileList[], Multipart multipart) throws Exception {
for (int index = 0; index < fileList.length; index++) {
MimeBodyPart mailArchieve = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileList[index]);
mailArchieve.setDataHandler(new DataHandler(fds));
mailArchieve.setFileName(MimeUtility.encodeText(fds.getName(),"UTF-8","B"));
multipart.addBodyPart(mailArchieve);
}
}

//以下是演示demo
public static void main(String args[]) {
MailTest mail = new MailTest();
mail.setSubject("java郵件");
mail.setContent("你好 這是第一個java 程序發送郵件");
//收件人 可以發給其他郵箱(163等) 下同
mail.setTo(new String[] {"[email protected]"});
//抄送
// mail.setCc(new String[] {"[email protected]","[email protected]"});
//密送
//mail.setBcc(new String[] {"[email protected]","[email protected]"});
//發送附件列表 可以寫絕對路徑 也可以寫相對路徑(起點是項目根目錄)
// mail.setFileList(new String[] {"D:\\aa.txt"});
//發送郵件
try {
mail.sendMessage();
System.out.println("發送郵件成功!");
} catch (Exception e) {
System.out.println("發送郵件失敗!");
e.printStackTrace();
}
}
}

D. java大賽的一道編程

publicclassToList<T>{
privateList<T>list=newArrayList<T>();
@SuppressWarnings({"rawtypes","unchecked"})
publicstaticToListcreate(Object...vs){
ToListtoList=newToList();
toList.add(vs);
returntoList;
}
@SuppressWarnings("unchecked")
publicToList<T>add(T...vs){
if(vs!=null){
for(Tv:vs){
list.add(v);
}
}
returnthis;
}
publicList<T>toList(){
returnlist;
}
publicstaticvoidmain(String[]args){
ToList<String>toList1=newToList<String>();
//往toList1里添加元素
toList1.add("a","b");
//可以一次添加多個元素,而且add方法能以串式方式調用
toList1.add("c","d","e").add("f").add("g","h","i","j");
//添加完後,將元素導出成一個List
java.util.List<String>list1=toList1.toList();
//
//這段代碼檢測list1的內容
if(list1.size()!=10){
thrownewRuntimeException();
}
java.util.List<Integer>list2=ToList.create(0,1,2).add(3,4)
.add(0,1).toList();
//
//以下代碼檢測list2的內容
int[]ia={0,1,2,3,4,0,1};
for(inti=0;i<7;i++){
if(list2.get(i)!=ia[i]){
thrownewRuntimeException();
}
}
}
}

E. java如何把一個2W行數據的list弄成每5W存1個list,然後生成一個Map<List,String>


publicstaticvoidmain(String[]args){
List<String>all=newArrayList<>();
all.add("0");
all.add("1");
all.add("2");
all.add("3");
all.add("4");
all.add("5");
all.add("6");
all.add("7");

intsize=4;
introw=2;//這個可以改為5000,前提是all中有2W行數據

Map<Integer,List>map=newHashMap<>(size);//你要求的Map<List,String>感覺很奇怪
//從你代碼里看是要求Map<Integer,List>

for(inti=0;i<size;i++){
List<String>temp=all.stream()
.skip(row*i)
.limit(row)
.collect(Collectors.toList());
map.put(i,temp);
}

map.forEach((k,v)->System.out.println(k+":"+v));

}

F. java JSON下面字元串怎麼解析

packagesrc.util;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Map;
importnet.sf.json.JSONArray;
importnet.sf.json.JSONObject;
publicclassTestJson
{
//json字元串轉
=
"[{"n1":"asd","n2":22,"n3":"45.40GB","+""n4":"qwerty","n5":"asd",},"+"{"n1":"local","
+""n2":1,"n3":"279.40GB","n4":"ST3300656SS","n5":"\/devm\/d0"}]";
/***
*json字元串轉javaList
*@paramrsContent
*@return
*@throwsException
*/
privatestaticList<Map<String,String>>jsonStringToList(StringrsContent)throwsException
{
JSONArrayarry=JSONArray.fromObject(rsContent);
System.out.println("json字元串內容如下");
System.out.println(arry);
List<Map<String,String>>rsList=newArrayList<Map<String,String>>();
for(inti=0;i<arry.size();i++)
{
JSONObjectjsonObject=arry.getJSONObject(i);
Map<String,String>map=newHashMap<String,String>();
for(Iterator<?>iter=jsonObject.keys();iter.hasNext();)
{
Stringkey=(String)iter.next();
Stringvalue=jsonObject.get(key).toString();
map.put(key,value);
}
rsList.add(map);
}
returnrsList;
}
/**
*@paramargs
*@throwsException
*/
publicstaticvoidmain(String[]args)throwsException
{
List<Map<String,String>>list1=jsonStringToList(diskListContent);
System.out.println("json字元串成map");
for(Map<String,String>m:list1)
{
System.out.println(m);
}
System.out.println("map轉換成json字元串");
for(Map<String,String>m:list1)
{
JSONArrayjsonArray=JSONArray.fromObject(m);
System.out.println(jsonArray.toString());
}
System.out.println("list轉換成json字元串");
JSONArrayjsonArray2=JSONArray.fromObject(list1);
System.out.println(jsonArray2.toString());
}
}

G. 求助大神:java 中 現有一個list 要取出list中所有元素拼接成字元串以逗號隔開應該如何做

publicclassTest{
publicstaticvoidmain(String[]args){
List<String>list=newArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");

Stringstr="";
for(inti=0;i<list.size();i++){
str+=list.get(i)+",";
}
System.out.println(str);
}
}

H. JAVA中String如何轉為Map

/**
*String轉換Map
*
*@parammapText
*:需要轉換的字元串
*@paramKeySeparator
*:字元串中的分隔符每一個key與value中的分割
*@paramElementSeparator
*:字元串中每個元素的分割
*@returnMap<?,?>
*/
publicstaticMap<String,Object>StringToMap(StringmapText){

if(mapText==null||mapText.equals("")){
returnnull;
}
mapText=mapText.substring(1);

mapText=EspUtils.DecodeBase64(mapText);

Map<String,Object>map=newHashMap<String,Object>();
String[]text=mapText.split("\"+SEP2);//轉換為數組
for(Stringstr:text){
String[]keyText=str.split(SEP1);//轉換key與value的數組
if(keyText.length<1){
continue;
}
Stringkey=keyText[0];//key
Stringvalue=keyText[1];//value
if(value.charAt(0)=='M'){
Map<?,?>map1=StringToMap(value);
map.put(key,map1);
}elseif(value.charAt(0)=='L'){
List<?>list=StringToList(value);
map.put(key,list);
}else{
map.put(key,value);
}
}
returnmap;
}

I. Java 泛型方法 出錯

你 看你傳的是啥?
傳的是Class類型。
但是想反悔MainPlay類型的集合?
錯誤裡面已經說的很清楚了,
UserActionCacheBean這個類型的setMainPlay(List<MainPlay>)無法使用參數List<Class<MainPlay>>

將你的jsonStringToListBean(Object object, T clazz)第二個參數改為Class<T> clazz

J. Java數組分成N個數組的所有組合

這個問題不是這么想的,
你可以想像一個n位二進制的數,找出所有隻有k個1,其他位都是0的數,這個二進制數的第x位為1就表示取字母表中的第x個字母,為0不取,最後得到的就是這個二進制數代表的組合,將所有的二進制數都翻譯成字母組合後,就是你要取得的所有字母組合了。

如果實在不會的話,待會再給你寫個代碼

public class Combination {
public static void main(String[] args) {
String[] valueSets = { "a", "b", "c", "d", "e" };
int n = 3;
List<String> list = combination(valueSets, n);
System.out.println(list);
for(String string: list){
System.out.println(string);
}
System.out.println("一共 "+list.size()+" 個。");
}

public static List<String> combination(String[] valueSets, int n) {
System.out.println(">>>>>combination");
List<String> binaryList = searchBinaryList(valueSets.length, n);
List<String> combinationList = toCombinationList(binaryList, valueSets);
return combinationList;
}

public static List<String> toCombinationList(List<String> binaryList,
String[] valueSets) {
List<String> combinationList = new ArrayList<String>();
for (String binary : binaryList) {
String combination = changeBinaryToCombination(binary, valueSets);
if (combination != null && combination.trim() != "") {
combinationList.add(combination);
}
}
return combinationList;
}

public static String changeBinaryToCombination(String binary,
String[] valueSets) {
String combination = "";
if (binary == null || binary.trim() == "") {
return null;
}
for (int i = 0; i < binary.length(); i++) {
if (binary.charAt(i) == '1') {
combination += valueSets[i];
}
}
return combination;
}

public static List<String> searchBinaryList(int length, int n) {
System.out.println(">>>>>searchBinaryList");
List<String> binaryList = new ArrayList<String>();
for (int i = 0; i < (int) Math.pow(2, length); i++) {
String binary = Integer.toBinaryString(i);
int count = oneCountsContainsInBinary(binary);
if (count == n) {
binaryList.add(toSpecifiedBitsBinary(binary, length));
}
}
return binaryList;
}

public static String toSpecifiedBitsBinary(String binary, int length) {
String specifiedBitsBinary = "";
for (int i = 0; i < length - binary.length(); i++) {
specifiedBitsBinary += 0;
}
specifiedBitsBinary += binary;
return specifiedBitsBinary;
}

public static int oneCountsContainsInBinary(String binary) {
int count = 0;
if (binary == null || binary.trim() == "") {
return count;
}
for (int i = 0; i < binary.length(); i++) {
if (binary.charAt(i) == '1') {
count++;
}
}
return count;
}
}

熱點內容
linux下ntp伺服器搭建 發布:2024-09-08 08:26:46 瀏覽:742
db2新建資料庫 發布:2024-09-08 08:10:19 瀏覽:171
頻率計源碼 發布:2024-09-08 07:40:26 瀏覽:778
奧迪a6哪個配置帶後排加熱 發布:2024-09-08 07:06:32 瀏覽:101
linux修改apache埠 發布:2024-09-08 07:05:49 瀏覽:209
有多少個不同的密碼子 發布:2024-09-08 07:00:46 瀏覽:566
linux搭建mysql伺服器配置 發布:2024-09-08 06:50:02 瀏覽:995
加上www不能訪問 發布:2024-09-08 06:39:52 瀏覽:811
銀行支付密碼器怎麼用 發布:2024-09-08 06:39:52 瀏覽:513
蘋果手機清理瀏覽器緩存怎麼清理緩存 發布:2024-09-08 06:31:32 瀏覽:554