合并映射中键包含的值(Java) final Multimap[]actionMap=new Multimap[]{null}; 加载的最终布尔值[]={false}; 执行(连接->{ PreparedStatement=null; ResultSet ResultSet=null; actionMap[0]=ArrayListMultimap.create(); 试一试{ 语句=connection.prepareStatement(“诸如此类。。。 while(resultSet.next()){ 最终字符串名=。。。 actionMap[0].put(名称,new AbstractMap.SimpleEntry(int1,int2));

合并映射中键包含的值(Java) final Multimap[]actionMap=new Multimap[]{null}; 加载的最终布尔值[]={false}; 执行(连接->{ PreparedStatement=null; ResultSet ResultSet=null; actionMap[0]=ArrayListMultimap.create(); 试一试{ 语句=connection.prepareStatement(“诸如此类。。。 while(resultSet.next()){ 最终字符串名=。。。 actionMap[0].put(名称,new AbstractMap.SimpleEntry(int1,int2));,java,guava,Java,Guava,我有一个映射,使用SimpleEntry插入两个整数值(int1,int2)。在重复键上,我想合并已映射的值。我的想法是computeIfPresent,但我不知道双函数,因为我使用的是AbstractMap。请尝试输入两个值。如果有任何帮助,将不胜感激。在放置和覆盖之前,请检索现有值 如果您得到null,则不存在这样的值,您可以按照最初的预期放置 如果你得到了一些东西,把你的新数据合并成一些东西 基于输入的输入(可以更完整),似乎你试图为你的数据使用错误的结构。假设你想合并 >条目的值,如

我有一个映射,使用
SimpleEntry
插入两个整数值(int1,int2)。在重复键上,我想合并已映射的值。我的想法是
computeIfPresent
,但我不知道双函数,因为我使用的是
AbstractMap。请尝试输入两个值。如果有任何帮助,将不胜感激。

在放置和覆盖之前,请检索现有值

  • 如果您得到null,则不存在这样的值,您可以按照最初的预期放置
  • 如果你得到了一些东西,把你的新数据合并成一些东西

基于输入的输入(可以更完整),似乎你试图为你的数据使用错误的结构。假设你想合并<强> >条目的值,如果两者都是<代码> No> < /> >和 It1存在< /强>,你应该使用番石榴的表来代替。让我们把这段代码当作一个想法:

final Multimap<String, Map.Entry<Integer, Integer>>[] actionMap = new Multimap[]{null};
final boolean[] loaded = {false};

db.execute(connection -> {
    PreparedStatement statement = null;
    ResultSet resultSet = null;

    actionMap[0] = ArrayListMultimap.create();
    try {
        statement = connection.prepareStatement("Blah Blah...
        while (resultSet.next()) {
            final String name = ...

            actionMap[0].put(name, new AbstractMap.SimpleEntry<>(int1, int2));
@测试
public void shouldMergeValues()引发SQLException{
给定(resultSet.getString(“名称”))
.willReturn(“名称”)
.willReturn(“名称”)
.willReturn(“名称”)
.将返回(“姓名2”);
给定(resultSet.getInt(“key”))
.willReturn(1)
.willReturn(1)
.willReturn(2)
.willReturn(100);
给定(resultSet.getInt(“值”))
.willReturn(2)
.willReturn(40)
.willReturn(3)
.willReturn(200);
给定(resultSet.next())
.willReturn(true)
.willReturn(true)
.willReturn(true)
.willReturn(true)
.willReturn(false);
Table actionTable=HashBasedTable.create();
while(resultSet.next()){
字符串名称=resultSet.getString(“名称”);
int int1=resultSet.getInt(“键”);
int int2=resultSet.getInt(“值”);
if(actionTable.contains(name,int1)){
整数oldInt2=actionTable.get(名称,int1);
actionTable.put(name,int1,oldInt2+int2);//在本例中,“+”是“merge”函数
}否则{
actionTable.put(名称,int1,int2);
}
}
assertThat(actionTable)/{name={1=42,2=3},name2={100=200}
.containsCell(“名称”,1,42)
.containsCell(“名称”,2,3)
.containsCell(“名称2”,100200)
.hasSize(3);
}

给定4行:
(“名称”,1,2)
(“名称”,1,40)
(“名称”,2,3)
(“名称”,2,100,200)
,并且示例“合并”操作是加法,您将得到表
{name={1=42,2=3},name2={100=200}
。请检查它是否适合您的用例。

您指的是这个吗?是的。
@Test
public void shouldMergeValues() throws SQLException {
    given(resultSet.getString("name"))
            .willReturn("name")
            .willReturn("name")
            .willReturn("name")
            .willReturn("name2");
    given(resultSet.getInt("key"))
            .willReturn(1)
            .willReturn(1)
            .willReturn(2)
            .willReturn(100);
    given(resultSet.getInt("value"))
            .willReturn(2)
            .willReturn(40)
            .willReturn(3)
            .willReturn(200);
    given(resultSet.next())
            .willReturn(true)
            .willReturn(true)
            .willReturn(true)
            .willReturn(true)
            .willReturn(false);

    Table<String, Integer, Integer> actionTable = HashBasedTable.create();

    while (resultSet.next()) {
        String name = resultSet.getString("name");
        int int1 = resultSet.getInt("key");
        int int2 = resultSet.getInt("value");
        if (actionTable.contains(name, int1)) {
            Integer oldInt2 = actionTable.get(name, int1);
            actionTable.put(name, int1, oldInt2 + int2); // in this example "+" is the "merge" function
        } else {
            actionTable.put(name, int1, int2);
        }
    }

    assertThat(actionTable) // {name={1=42, 2=3}, name2={100=200}}
            .containsCell("name", 1, 42)
            .containsCell("name", 2, 3)
            .containsCell("name2", 100, 200)
            .hasSize(3);
}