繁体   English   中英

更新 LinkedHashSet 的元素,它是一个集合

[英]Update element of LinkedHashSet which is a collection

我有一个嵌套的集合结构,表示模式、表、列

public class SSchema{
    private String schemaName;
    private LinkedHashSet<STable> tableSet;
}
public class STable {
    private String tableFullName;
    private String tableDisplayName;
    private LinkedHashSet<SColumn> columnSet;
}
public class SColumn {
    private String columnFullName;
    private String columnDisplayName;
    private String columnType;
}

我从 API 中检索列列表并填充列的 LinkedHashSet,然后获取这些列所属的表,并尝试更新每个表内的列的 LinkedHashSet。 不幸的是,没有 API 来获取此 API 中的表的列

LinkedHashSet<SColumn> sColumnSet = getSColumnsFromAPI();
LinkedHashSet<STable> sTableSet = getSTablesForColumns(sColumnSet);

在 function getSTablesForColumns 中,

private LinkedHashSet<STable> getSTablesForColumns(LinkedHashSet<SColumn> sColumnSet) throws IOException,JsonParseException {
        sTableSet = new LinkedHashSet<STable>();
        sColumnSet.forEach(sColumn -> {
        String[] tablesList = getTablesFromApi(sColumn) //get the tables this column is in
        foreach(String tableName:tablesList){
           STable table = STable.builder()
                                  .tableFullName(tableName)
                                  .build();
           //This is where the problem is - need a way to get the existing table
           if(sTableSet.contains(table){
                 // get existing table from sTableSet - how do I do this?
                 existingTable.getColumnSet().add(stiboColumn);
           }
           else { //new table, not in collection
               LinkedHashSet<SColumn> columnSet = new LinkedHashSet<SColumn>();
               columnSet.add(sColumn);
               table.setColumnSet(columnSet);
               sTableSet.add(table);
                 };
            }
      });
    return sTableSet;
}

我遇到的问题/困难是:如何有效地找到 LinkedHashSet 中的现有 sTable 并使用附加 sColumn 更新此 sTable 中的 LinkedHashSet?

编辑:在下面更好的解决方案之前,我想出了以下修复方法:

我之前的解决方案是检查集合中是否存在该表(我已将 sTableSet 移至 class 变量):

boolean existingTable=false;
   for(STable sTable : sTableSet){
      String collectionSTableName = sTable.getTableFullName();
      if (collectionSTableName.equals(table)){
            existingTable=true;
            sTable.getColumnSet().add(sColumn);
            break;
      }
   }
   if(existingTable==false) { //new table, not in collection
       LinkedHashSet<SColumn> columnSet = new LinkedHashSet<SColumn>();
       columnSet.add(sColumn);
       table.setColumnSet(columnSet);
       sTableSet.add(table);
   };

您的第一行getSTablesForColumns正在重新初始化全局字段sTableSet 如果您再次调用此方法,您将丢失已存储的内容。

但是,除此之外,您可能正在寻找的数据结构是HashMap (我还将返回类型更改为 void 以避免第一个问题)

Class Test {
   private Map<String, STable> sTableMap = new HashMap<>();

   public Test() {
      LinkedHashSet<SColumn> sColumnSet = getSColumnsFromAPI();
      updateSTablesForColumns(sColumnSet); // sTableMap will be populated after this call
   }

   private void updateSTablesForColumns(LinkedHashSet<SColumn> sColumnSet) throws IOException,JsonParseException {
        sColumnSet.forEach(sColumn -> {
           String[] tablesList = getTablesFromApi(sColumn) //get the tables this column is in
           for (String tableName : tablesList){
              STable table = STable.builder()
                                     .tableFullName(tableName)
                                     .build();
   
              STable existingTable = sTableMap.get(table.getTableFullName());
              if(existingTable != null){
                  existingTable.getColumnSet().add(stiboColumn);
              } else { //new table, not in collection
                  LinkedHashSet<SColumn> columnSet = new LinkedHashSet<SColumn>();
                  columnSet.add(sColumn);
                  table.setColumnSet(columnSet);
                  sTableMap.put(table.getTableFullName(), table);
              }
           }
      });
   }

}

注意副作用和修改函数内的参数。 在此处的方法中,您通过调用sColumnSet.add()来更改sColumnSet 这意味着您的方法调用后的参数现在已更改。 此外,您不应在遍历集合时将其添加到集合中。 你甚至可能在这里得到一个例外columnSet.add(sColumn);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM