如何使用递增键创建重复项的唯一标签?

发布于 2024-08-03 05:17:56 字数 2022 浏览 4 评论 0原文

这是Java环境下的编程技术问题。

问题:我们有字符串键,所有键都必须是唯一的。集合的实现是 Map 和 ArrayList 的组合,它允许将集合用作 Map 或 ArrayList(如果您好奇,可以使用 JFreeChart 的 DefaultPieDataset)。如果我们有一条数据,我们希望它的键就是它本身。如果我们有由其他键分隔的倍数,我们需要数据加上分隔符和递增数字。

例如:

具有两个 Bob 条目和一个 Albert 条目的数据将具有类似于 ["Bob", "Albert"] 的键。

类似于 AlbertBob 的数据,其中有两个 Bob 条目,其间有一个 Albert ,其键类似于 [“鲍勃:1”,“阿尔伯特”,“鲍勃:2”]

这是我们到目前为止的代码:

String dataKey = "";
DefaultPieDataset ringDataset = new DefaultPieDataset();
for(String thisData: dataList)
{
    int dataValue;
    if(dataKey.equals(thisData))
    {
        dataValue= ringDataset.getValue(dataKey).intValue() + 1;
    }else
    {
        dataKey= thisData;
        if(ringDataset.getKeys().contains(dataKey) || ringDataset.getKeys().contains(dataKey+ " : 1")) //has this name been represented already?
        {
            if(ringDataset.getKeys().contains(dataKey)) //do we need to change the name to accommodate multiples?
            {
                int existingKeyIndex = ringDataset.getIndex(dataKey);
                int existingKeyValue = ringDataset.getValue(dataKey).intValue();
                ringDataset.remove(dataKey);
                ringDataset.insertValue(existingKeyIndex, dataKey+ " : 1", existingKeyValue);
            }

            int counter = 2;
            do{
                dataKey= thisData + " : " + counter;
                counter ++;
            }while(ringDataset.getKeys().contains(dataKey)); //ensure that we are using a new key
        }
        dataValue= 1;
  }
 ringDataset.setValue(dataKey, dataValue);
}

目前,代码为每个重复项添加了“: #”,这样它就不再是 Bob : 2 而是将 Bob : 1 : 2 变成了

一个额外的挑战是我无法保证该名称不会有分隔符(示例中的冒号)。

此外,添加任何库都需要大量繁文缛节,因此需要仅 Java API 的解决方案。

感谢您帮助解决这个特殊的脑筋急转弯,
Adam

编辑:为了进一步澄清代码,项目值基于数据类型连续出现的次数。因此,在第一个示例中,键 Bob 的值为 2,Albert 的值为 1。在第二个示例中,所有键的值为 1。

This is a programming technique problem in the context of Java.

Problem: We have String keys that all need to be unique. The implementation of the collection is a combination of Map and ArrayList which allows one to use the collection as either a Map or ArrayList (JFreeChart's DefaultPieDataset if you are curious). If we have a single piece of data we want it's key to be itself. If we have multiples that are separated by some other key we want the data plus a separator and an incrementing number.

For example:

Data that with two Bob entries and then an Albert would have keys like ["Bob", "Albert"].

Data that looks like Albert and Bob with two Bob entries with an Albert between would have keys like ["Bob : 1", "Albert", "Bob : 2"].

This is our code so far:

String dataKey = "";
DefaultPieDataset ringDataset = new DefaultPieDataset();
for(String thisData: dataList)
{
    int dataValue;
    if(dataKey.equals(thisData))
    {
        dataValue= ringDataset.getValue(dataKey).intValue() + 1;
    }else
    {
        dataKey= thisData;
        if(ringDataset.getKeys().contains(dataKey) || ringDataset.getKeys().contains(dataKey+ " : 1")) //has this name been represented already?
        {
            if(ringDataset.getKeys().contains(dataKey)) //do we need to change the name to accommodate multiples?
            {
                int existingKeyIndex = ringDataset.getIndex(dataKey);
                int existingKeyValue = ringDataset.getValue(dataKey).intValue();
                ringDataset.remove(dataKey);
                ringDataset.insertValue(existingKeyIndex, dataKey+ " : 1", existingKeyValue);
            }

            int counter = 2;
            do{
                dataKey= thisData + " : " + counter;
                counter ++;
            }while(ringDataset.getKeys().contains(dataKey)); //ensure that we are using a new key
        }
        dataValue= 1;
  }
 ringDataset.setValue(dataKey, dataValue);
}

Currently, the code tacks on the " : #" for every duplicate so that instead of Bob : 2 it makes Bob : 1 : 2

An added challenge is that I cannot gurantee that the name will not have the separator (colons in the example).

Also there is extensive red tape required for any libraries added, so a Java API only solution is needed.

Thank you for helping with this particular brain twister,
Adam

Edit: To further clarify the code an items value is based off of the number of times that data type has come up in a row. So in the first example the key Bob has a value of 2 and Albert 1. In the second example all keys have a value of 1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霊感 2024-08-10 05:17:56

我认为您不必处理分布式系统的唯一性,因为您的问题在客户端。此外,我假设您不必处理多个线程上的同步。如果没有,可以考虑在incrementLabelCount()方法中放入synchronized关键字,性能大概还可以。

我会将您的代码分成两部分,这样更易​​于管理:
- 第 1 部分:跟踪已知标签
- 第 2 部分:生成唯一标签

以下是我快速键入的代码(没有编译逻辑)

class KnownLabels
{
  Map<String, Integer> currentCountsByLabel;

  public int incrementLabelCount(String label)
  {
    Integer count = currentCountsByLabel.get(label);
    if(count==null)
      count = 1;
    else
      count += 1;

    currentCountsByLabel.put(label, count);

    return count;
  }
}

class UniqueLabelGenerator
{
  private KnownLabels knownLabels = new KnownLabels();

  public String getUniqueLabel(String label)
  {
    int i = knownLabels.incrementLabelCount(label);

    if(i>1)
      return label + ":" + i;

     return label;
  }
}

I assume you don't have to handle uniqueness of a distributed system since your problem is on the client side. Additionally, I assume you don't have to handle the synchronization on multiple threads. If not, you can consider to put the synchronized keyword in the incrementLabelCount() method, and the performance is probably okay.

I would break your codes into two parts, so it is more manageable:
- part 1: keep tracking of the already known labels
- part 2: generate the unique label

Following is the codes that I just type out quickly (without compilation logic)

class KnownLabels
{
  Map<String, Integer> currentCountsByLabel;

  public int incrementLabelCount(String label)
  {
    Integer count = currentCountsByLabel.get(label);
    if(count==null)
      count = 1;
    else
      count += 1;

    currentCountsByLabel.put(label, count);

    return count;
  }
}

class UniqueLabelGenerator
{
  private KnownLabels knownLabels = new KnownLabels();

  public String getUniqueLabel(String label)
  {
    int i = knownLabels.incrementLabelCount(label);

    if(i>1)
      return label + ":" + i;

     return label;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文