我想將包含abc
的字符串轉換爲字符列表和字符哈希集。我怎麼能在Java中做到這一點?Java集合將字符串轉換爲字符列表
List<Character> charList = new ArrayList<Character>("abc".toCharArray());
我想將包含abc
的字符串轉換爲字符列表和字符哈希集。我怎麼能在Java中做到這一點?Java集合將字符串轉換爲字符列表
List<Character> charList = new ArrayList<Character>("abc".toCharArray());
你將不得不要麼使用循環,或創建一個像原始字符數組(或直接在字符串)上工作的Arrays.asList
集合包裝。
List<Character> list = new ArrayList<Character>();
Set<Character> unique = new HashSet<Character>();
for(char c : "abc".toCharArray()) {
list.add(c);
unique.add(c);
}
這裏是一個Arrays.asList
像包裝的字符串:
public List<Character> asList(final String string) {
return new AbstractList<Character>() {
public int size() { return string.length(); }
public Character get(int index) { return string.charAt(index); }
};
}
這是一個不可改變的名單,雖然。如果你想有一個可變的列表,使用了char[]
:
public List<Character> asList(final char[] string) {
return new AbstractList<Character>() {
public int size() { return string.length; }
public Character get(int index) { return string[index]; }
public Character set(int index, Character newVal) {
char old = string[index];
string[index] = newVal;
return old;
}
};
}
類似於這樣就可以實現這個對於其他基本類型。 請注意,不建議使用此功能,因爲對於每次訪問,您都會執行裝箱和拆箱操作。
的Guava library包含similar List wrapper methods for several primitive array classes,像Chars.asList,並在Lists.charactersOf(String)絃樂的包裝。
的最直接方式是使用for
循環將元素添加到一個新的List
:
String abc = "abc";
List<Character> charList = new ArrayList<Character>();
for (char c : abc.toCharArray()) {
charList.add(c);
}
同樣,對於Set
:
String abc = "abc";
Set<Character> charSet = new HashSet<Character>();
for (char c : abc.toCharArray()) {
charSet.add(c);
}
也許這可能工作 -
List<Character> characterList = new ArrayList<Character>();
char arrayChar[] = abc.toCharArray();
for (char aChar : arrayChar)
{
characterList.add(aChar); // autoboxing
}
缺乏一個基本數組和它對應的包裝類型的集合之間進行轉換的一個好辦法是通過一些第三方庫解決。番石榴,一個很普通的一個,has a convenience method to do the conversion:
List<Character> characterList = Chars.asList("abc".toCharArray());
Set<Character> characterSet = new HashSet<Character>(characterList);
在Java8你可以使用我想流。 名單性格對象:
List<Character> chars = str.chars().mapToObj(e->(char)e).collect(Collectors.toList());
和SET可以以類似的方式獲得:
Set<Character> charsSet = str.chars().mapToObj(e->(char)e).collect(Collectors.toSet());
使用Java 8 Stream
。
myString.chars().mapToObj(i -> (char) i).collect(Collectors.toList());
擊穿:
myString
.chars() // Convert to an IntStream
.mapToObj(i -> (char) i) // Convert int to char, which gets boxed to Character
.collect(Collectors.toList()); // Collect in a List<Character>
(我絕對不知道爲什麼String#chars()
返回IntStream
。)
如果使用Eclipse Collections你可以做到這一點沒有拳擊:
CharAdapter abc = CharAdapter.adapt("abc");
CharList list = abc.toList();
CharSet set = abc.toSet();
CharBag bag = abc.toBag();
因爲CharAdapter
是ImmutableCharList
,呼籲它collect
將返回ImmutableList
。
ImmutableList<Character> immutableList = abc.collect(Character::valueOf);
如果你想返回一個盒裝List
,Set
或Character
Bag
,下面的工作:
LazyIterable<Character> lazyIterable = abc.asLazy().collect(Character::valueOf);
List<Character> list = lazyIterable.toList();
Set<Character> set = lazyIterable.toSet();
Bag<Character> set = lazyIterable.toBag();
注:我的Eclipse集合的參與者。
IntStream可用於訪問每個字符並將它們添加到列表中。
String str = "abc";
List<Character> charList = new ArrayList<>();
IntStream.range(0,str.length()).forEach(i -> charList.add(str.charAt(i)));
看起來我已經在這裏重新創造了輪子......我想'Chars.asList'確實是關於我的答案中的'asList'方法。 – 2011-06-12 04:07:18
另外在番石榴是'Lists.charactersOf(「abc」)'這是有點短,並沒有讓我們調用'toCharArray()' – 2014-01-06 18:45:23