Java集合将字符串转换为字符列表
我想将包含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