什么是获得arraylist的arraylist的有效方法?

时间:2016-04-11 21:20:06

标签: java

我有这个字符串:

string1="A.1,B.2,C.4"

我想得到以下arraylist的arraylist:

<<"A","1">,<"B","2">,<"c","4">>

除了使用for循环之外还有什么方法吗? 假设arraylists是唯一的,所以我会

Set<String> set= new HashSet<>(Arrays.asList(string1.split(",")));

现在我想在上面的set中拆分每个元素,而不使用for循环。

1 个答案:

答案 0 :(得分:2)

我在考虑首先基于,进行拆分。然后,将.上的每个值拆分,将两个值放在一个列表中,然后将该列表放在另一个列表中。应该有效的伪代码。 :)

string1="A.1,B.2,C.4"

stringsWithDots[] = string1.split(",");

List<List<String>> result = new ArrayList<List<String>>();
for(String stringWithDots: stringsWithDots) {
    finalSplit[] = stringWithDots.split(".");
    List<String> list1 = Arrays.asList(finalSplit);
    result.add(list1);
}

"A.1,B.2,C.4"看起来像[[A,1],[B,2],[C,4]]

修改

[asList source]

[split],用于分割字符串[有一个循环]。

ArrayList由数组支持。 asList函数只是设置对该数组[source]的引用。

那么,你说不使用循环的那个东西。好吧,无论是使用流还是内部函数,都会发生循环;只是你可能没有在你编写的直接代码中看到它。