在Java中用于存储键值对的集合。

huangapple go评论87阅读模式
英文:

Set to hold key value pairs in java

问题

我面临一个问题,需要在集合中存储唯一的键值对。据我所知,集合中无法存储键值对,所以我正在使用映射(map),并检查entryset()的值来确认我要插入的内容是否已经存在。如果我检查一对(a,c)在映射中不存在,但存在(a,b),插入(a,c)会更新(a,b)的条目,我无法同时保存这两对键值对。请指导如何实现这一点。

英文:

I am facing a problem where i need to store unique key value pairs in a set. As per my knowledge I can't have key value pairs in set so I am using a map and checking the entryset() values to confirm what I am about to insert doesn't already exist. If i check a pair (a,c) doesnt exist in map but there is (a,b) present, the insert of (a,c) updates the (a,b) entry and I am unable to save both the key value pairs. Please advise how to achieve this.

答案1

得分: 2

以下是您要翻译的部分:

您可以在没有与Spring、Guava等配对相关的依赖项的情况下创建自定义的配对类,然后必须重写equals和hashcode方法,以便拥有包含您想要的元素的集合:

@SuppressWarnings("unused")
public final class Pair<L, R> {

    private final L key;

    private final R value;

    private Pair(final L key, final R value) {
        this.key = key;
        this.value = value;
    }

    public static <L, R> Pair<L, R> of(final L key, final R value) {
        return new Pair<>(key, value);
    }

    public L getKey() {
        return this.key;
    }

    public R getValue() {
        return this.value;
    }

    @Override
    @SuppressWarnings("rawtypes")
    public boolean equals(@Nullable final Object object) {
        if (this == object) {
            return true;
        }
        if (object == null) {
            return false;
        }
        if (this.getClass() != object.getClass()) {
            return false;
        }
        final Pair<?, ?> other = (Pair) object;
        return Objects.equals(this.key, other.key) && Objects.equals(this.value, other.value);
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.key, this.value);
    }

    @Override
    public String toString() {
        return String.format("(%s - %s)", this.key, this.value);
    }

}

以下是我与之一起使用的结果,可能满足您的期望输出:

public static void main(final String[] args) {
    final HashSet<Pair<Integer, String>> pairs = new HashSet<>();
    pairs.add(Pair.of(12, "Hello"));
    pairs.add(Pair.of(12, "Hi"));
    pairs.add(Pair.of(13, "Hello"));
    pairs.add(Pair.of(14, "Hello"));
    pairs.add(Pair.of(15, "Hello"));
    System.out.println(pairs); // => [(13 - Hello), (12 - Hello), (15 - Hello), (14 - Hello), (12 - Hi)]
}

如果您不想创建新类,可以使用Map中的内置类Entry。

public static void main(final String[] args) {
    final HashSet<Map.Entry<Integer, String>> pairs = new HashSet<>();
    pairs.add(Map.entry(12, "Hello"));
    pairs.add(Map.entry(12, "Hi"));
    pairs.add(Map.entry(12, "Hi"));
    pairs.add(Map.entry(13, "Hello"));
    pairs.add(Map.entry(14, "Hello"));
    pairs.add(Map.entry(15, "Hello"));
    System.out.println(pairs);
    // [12=Hello, 13=Hello, 14=Hello, 15=Hello, 12=Hi]
}

希望这对您有所帮助。

英文:

You can create your custom pair class in case you have no dependency related to pair such as Spring, Guava,... Then you have to override equals and hashcode method to have a set contains elements you wanted:

@SuppressWarnings(&quot;unused&quot;)
public final class Pair&lt;L, R&gt; {
private final L key;
private final R value;
private Pair(final L key, final R value) {
this.key = key;
this.value = value;
}
public static &lt;L, R&gt; Pair&lt;L, R&gt; of(final L key, final R value) {
return new Pair&lt;&gt;(key, value);
}
public L getKey() {
return this.key;
}
public R getValue() {
return this.value;
}
@Override
@SuppressWarnings(&quot;rawtypes&quot;)
public boolean equals(@Nullable final Object object) {
if (this == object) {
return true;
}
if (object == null) {
return false;
}
if (this.getClass() != object.getClass()) {
return false;
}
final Pair&lt;?, ?&gt; other = (Pair) object;
return Objects.equals(this.key, other.key) &amp;&amp; Objects.equals(this.value, other.value);
}
@Override
public int hashCode() {
return Objects.hash(this.key, this.value);
}
@Override
public String toString() {
return String.format(&quot;(%s - %s)&quot;, this.key, this.value);
}
}

Following is the result that I use with it that might meet your desired output:

    public static void main(final String[] args) {
final HashSet&lt;Pair&lt;Integer, String&gt;&gt; pairs = new HashSet&lt;&gt;();
pairs.add(Pair.of(12, &quot;Hello&quot;));
pairs.add(Pair.of(12, &quot;Hi&quot;));
pairs.add(Pair.of(13, &quot;Hello&quot;));
pairs.add(Pair.of(14, &quot;Hello&quot;));
pairs.add(Pair.of(15, &quot;Hello&quot;));
System.out.println(pairs); // =&gt; [(13 - Hello), (12 - Hello), (15 - Hello), (14 - Hello), (12 - Hi)]
}

In case you don't want to create new class. You can use buildin class Entry from Map.

public static void main(final String[] args) {
final HashSet&lt;Map.Entry&lt;Integer, String&gt;&gt; pairs = new HashSet&lt;&gt;();
pairs.add(Map.entry(12, &quot;Hello&quot;));
pairs.add(Map.entry(12, &quot;Hi&quot;));
pairs.add(Map.entry(12, &quot;Hi&quot;));
pairs.add(Map.entry(13, &quot;Hello&quot;));
pairs.add(Map.entry(14, &quot;Hello&quot;));
pairs.add(Map.entry(15, &quot;Hello&quot;));
System.out.println(pairs);
// [12=Hello, 13=Hello, 14=Hello, 15=Hello, 12=Hi]
}

huangapple
  • 本文由 发表于 2023年6月5日 18:37:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定