Set,List,配列の相互変換
9772 ワード
JAvaにおけるSet,List,配列の相互変換
public class ArraysTest {
/**
* List
* @param arrays
* @return
*/
private static List arraysToList(Object[] arrays) {
return Arrays.asList(arrays); // list, add remove, java.lang.UnsupportedOperationException
// , List
/*List list = new ArrayList();
if (arrays != null && arrays.length > 0) {
for (Object obj : arrays) {
list.add(obj);
}
}
return list;*/
}
/**
* set
* @param arrays
* @return
*/
private static Set arraysToSet(Object[] arrays) {
return new HashSet(Arrays.asList(arrays)); // list, HashSet Set
}
/**
* list
* @param list
* @return
*/
private static Object[] listToArrays(List list) {
return list.toArray();
}
/**
* list set
* @param list
* @return
*/
private static Set listToSet(List list) {
return new HashSet(list);
}
/**
* set
* @param set
* @return
*/
private static Object[] setToArray(Set set) {
return set.toArray();
}
/**
* set List
* @param set
* @return
*/
private static List setToList(Set set) {
return new ArrayList(set);
}
public static void main(String[] args) {
String[] strArrays = new String[]{"csdn", "ddp", "Roman", "ForEver"};
List list1 = arraysToList(strArrays);
Set set = arraysToSet(strArrays);
Object[] objArrays = listToArrays(list1);
Set set1 = listToSet(list1);
Object[] objectArrays2 = setToArray(set1);
List list2 = setToList(set1);
System.out.println("sss");
}
private static void printList(List list) {
if (list != null && list.size() > 0) {
for (Object obj : list) {
System.out.println(obj.toString());
}
}
}
}