Java 将数据存储到地图组中
我有一个字符串,例如:Java 将数据存储到地图组中,java,string,split,Java,String,Split,我有一个字符串,例如: 2014@2@200@0#2014@2@200@0#2012@2@200@0#2012@2@200@0#2011@2@200@0 现在,我需要使用以下规则将上述详细信息存储到一个地图中: 使用“#”在字符串上方拆分 再次使用@ 拆分后,将数据存储到地图中 例如,如果我们使用#拆分上面的字符串,那么我们可以将下面的数据放入字符串数组中: array[0] = "2014@2@200@0"; array[1] = "2014@2@200@0"; array[2] = "20
2014@2@200@0#2014@2@200@0#2012@2@200@0#2012@2@200@0#2011@2@200@0
现在,我需要使用以下规则将上述详细信息存储到一个地图中:
array[0] = "2014@2@200@0";
array[1] = "2014@2@200@0";
array[2] = "2012@1@100@0";
array[3] = "2012@3@200@0";
array[4] = "2011@2@200@0";
现在再次使用“@”拆分数组数据
因此,我们得到:
a[0] = "2014";
a[1] = "2";
a[2] = "200";
a[3] = "0";
Map看起来像:Map“String,TestClass”test
映射键:a[0]值
我需要按[0]值对这些数据进行分组
例如,2014年关键地图数据:
key : 2014
value : a4 = 2014, a1 = 2+2, a2 = 200 + 200, a3 = 0 + 0
key : 2012
value : a4 = 2012, a1 = 1+3, a2 = 100 + 200, a3 = 0 + 0
如何实现上述情况?试试这个
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class Test
{
String a1;
String a2;
String a3;
String a4;
Test(String[] str)
{
a1=str[0];
a2=str[1]+"+"+str[1];
a3=str[2]+"+"+str[2];
a4=str[3]+"+"+str[3];
}
public static void main(String[] args)
{
String str="2014@2@200@0#2014@2@200@0#2012@2@200@0#2012@2@200@0#2011@2@200@0";
String arr[]=str.split("#");
Map<String,Test> map=new HashMap<String,Test>();
for (String value : arr)
{
String s[]=value.split("@");
Test test=new Test(s);
map.put(s[0], test);
}
for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();)
{
Entry key=(Entry) iterator.next();
Test t1=map.get(key.getKey());
System.out.println("Map Key:"+key.getKey());
System.out.print("\ta1: "+t1.a1);
System.out.print("\ta2: "+t1.a2);
System.out.print("\ta3: "+t1.a3);
System.out.print("\ta4: "+t1.a4);
System.out.println();
}
}
}
您可以使用来完成此任务,如下所示:
Stream.of(str.split("#")).map(e -> e.split("@")).map(x -> new TestClass(x[1], x[2], x[3], x[0])).collect(Collectors.toMap(TestClass::getA4, z -> z,(p1, p2) -> p1));
示例实现,但您必须稍微调整逻辑:
SplitString.java
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class SplitString {
public static void main(String[] args) {
Map<String, TestClass> map = Stream.of(str.split("#")).map(e -> e.split("@")).map(x -> new TestClass(x[1], x[2], x[3], x[0])).collect(Collectors.toMap(TestClass::getA4, z -> z,(p1, p2) -> p1));
System.out.println(map);
}
}
public class TestClass {
private final String a1;
private final String a2;
private final String a3;
private final String a4;
public TestClass(String a1, String a2, String a3, String a4) {
this.a1 = a1;
this.a2 = a2;
this.a3 = a3;
this.a4 = a4;
}
public String getA1() {
return a1;
}
public String getA2() {
return a2;
}
public String getA3() {
return a3;
}
public String getA4() {
return a4;
}
@Override
public String toString() {
return "TestClass [a4=" + a4 + ", a1=" + a1 + ", a2=" + a2 + ", a3="
+ a3 + "]";
}
}
import java.util.stream.Stream;
public class TestSplit {
public static void main (String... args) {
Map<String, TestClass> map = split("2014@2@200@0#2014@2@200@0#2012@1@100@0#2012@3@200@0#2011@2@200@0");
map.forEach((key, value) -> System.out.println(key + " -> " + value));
}
private static Map<String, TestClass> split(String input) {
String[] testClassStrings = input.split("#");
Stream<String[]> testClassStringsStream = Arrays.stream(testClassStrings).map(s -> s.split("@"));
Stream<TestClass> testClasses = testClassStringsStream.map(s -> new TestClass(s[0], s[1], s[2], s[3]));
Map<String, TestClass> testClassesMap = new HashMap<>();
testClasses.forEach((TestClass tc) -> {
TestClass oldValue = testClassesMap.get(tc.a0);
testClassesMap.put(tc.a0, oldValue == null ? tc : new TestClass(oldValue, tc));
});
return testClassesMap;
}
}
class TestClass {
String a0;
String a1;
String a2;
String a3;
public TestClass(String a0, String a1, String a2, String a3) {
this.a0 = a0;
this.a1 = a1;
this.a2 = a2;
this.a3 = a3;
}
public TestClass(TestClass tc1, TestClass tc2) {
this.a0 = tc1.a0;
this.a1 = tc1.a1 + " + " + tc2.a1;
this.a2 = tc1.a2 + " + " + tc2.a2;
this.a3 = tc1.a3 + " + " + tc2.a3;
}
@Override
public String toString() {
return String.format("a0 = %s, a1 = %s, a3 = %s, a4 = %s", a0, a1, a2, a3);
}
}
输出:
Map Key:2014
a1:2014 a2:2+2 a3:200+200 a4:0+0
Map Key:2012
a1:2012 a2:2+2 a3:200+200 a4:0+0
Map Key:2011
a1:2011 a2:2+2 a3:200+200 a4:0+0
{2014=TestClass [a4=2014, a1=2, a2=200, a3=0], 2012=TestClass [a4=2012, a1=2, a2=200, a3=0], 2011=TestClass [a4=2011, a1=2, a2=200, a3=0]}
下面是一个使用Java 8的简短解决方案: TestSplit.java
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class SplitString {
public static void main(String[] args) {
Map<String, TestClass> map = Stream.of(str.split("#")).map(e -> e.split("@")).map(x -> new TestClass(x[1], x[2], x[3], x[0])).collect(Collectors.toMap(TestClass::getA4, z -> z,(p1, p2) -> p1));
System.out.println(map);
}
}
public class TestClass {
private final String a1;
private final String a2;
private final String a3;
private final String a4;
public TestClass(String a1, String a2, String a3, String a4) {
this.a1 = a1;
this.a2 = a2;
this.a3 = a3;
this.a4 = a4;
}
public String getA1() {
return a1;
}
public String getA2() {
return a2;
}
public String getA3() {
return a3;
}
public String getA4() {
return a4;
}
@Override
public String toString() {
return "TestClass [a4=" + a4 + ", a1=" + a1 + ", a2=" + a2 + ", a3="
+ a3 + "]";
}
}
import java.util.stream.Stream;
public class TestSplit {
public static void main (String... args) {
Map<String, TestClass> map = split("2014@2@200@0#2014@2@200@0#2012@1@100@0#2012@3@200@0#2011@2@200@0");
map.forEach((key, value) -> System.out.println(key + " -> " + value));
}
private static Map<String, TestClass> split(String input) {
String[] testClassStrings = input.split("#");
Stream<String[]> testClassStringsStream = Arrays.stream(testClassStrings).map(s -> s.split("@"));
Stream<TestClass> testClasses = testClassStringsStream.map(s -> new TestClass(s[0], s[1], s[2], s[3]));
Map<String, TestClass> testClassesMap = new HashMap<>();
testClasses.forEach((TestClass tc) -> {
TestClass oldValue = testClassesMap.get(tc.a0);
testClassesMap.put(tc.a0, oldValue == null ? tc : new TestClass(oldValue, tc));
});
return testClassesMap;
}
}
class TestClass {
String a0;
String a1;
String a2;
String a3;
public TestClass(String a0, String a1, String a2, String a3) {
this.a0 = a0;
this.a1 = a1;
this.a2 = a2;
this.a3 = a3;
}
public TestClass(TestClass tc1, TestClass tc2) {
this.a0 = tc1.a0;
this.a1 = tc1.a1 + " + " + tc2.a1;
this.a2 = tc1.a2 + " + " + tc2.a2;
this.a3 = tc1.a3 + " + " + tc2.a3;
}
@Override
public String toString() {
return String.format("a0 = %s, a1 = %s, a3 = %s, a4 = %s", a0, a1, a2, a3);
}
}
您所需要做的就是调用TestSplit
类中的split
方法。该方法应该简洁易读,但只要再努力一点,您就可以使其更具功能性。以下是精确的步骤:
#
(linestring[]testClassStrings=input.Split(“#”)
)拆分字符串。将执行以下转换:“2014@2@200@0#2014@2@200@0#2012@1@100@0#2012@3@200@0#2011@2@200@0“
到
testClassStrings[0] = "2014@2@200@0"
testClassStrings[1] = "2014@2@200@0"
testClassStrings[2] = "2012@1@100@0"
testClassStrings[3] = "2012@3@200@0"
testClassStrings[4] = "2011@2@200@0"
@
拆分每个字符串。(lineStream testclassstringstream=Arrays.Stream(testClassStrings).map(s->s.split(“@”);
。现在有了一个流,其中每个条目都是类似于[“2014”、“2”、“200”、“0”之类的数组
TestClass
,行为streamtestclasses=testClassStringsStream.map(s->newtestclass(s[0],s[1],s[2],s[3]);
testClassesMap
。如果映射中已包含某些测试类,则应用其元素的添加:
Map<String, TestClass> testClassesMap = new HashMap<>();
testClasses.forEach((TestClass tc) -> {
TestClass oldValue = testClassesMap.get(tc.a0);
testClassesMap.put(tc.a0, oldValue == null ? tc : new TestClass(oldValue, tc));
});
您需要创建一个连接所有分组的TestClass字段数据的自定义方法 在Java8中,您可以使用流接口轻松地映射/连接数据 数据映射器 输出
你到底想要什么?
Map
?因为你的值似乎只有3个值,TestClass
有4个值。什么是a4
?这实际上并不是OP想要的。你永远不会按照他在所需输出中显示的方式对字段求和。@IvayloToskov我只是想给OP一个想法,我也提到过在我的回答中,虽然您必须调整逻辑位,但这实际上并不能满足OP的要求。您永远不会按照他在所需输出中显示的方式对字段求和。如果s.split(“@”),我只需要添加额外的条件此长度大于2,则将添加数据。如何设置该条件?我没有完全理解您的意思。上面的代码确实添加了第4点所示代码段中元素的字段。s.split(“@”)的长度不应该拆分吗
如果字符串格式正确,则始终为4?我的意思是,如果拆分的字符串小于2,则我们不会将其放入列表中
key : 2014
value : a4 = 2014, a1 = 2 + 2, a2 = 200 + 200, a3 = 0 + 0
key : 2012
value : a4 = 2012, a1 = 1 + 3, a2 = 100 + 200, a3 = 0 + 0
key : 2011
value : a4 = 2011, a1 = 2, a2 = 200, a3 = 0