• <i id='14vNp'><tr id='14vNp'><dt id='14vNp'><q id='14vNp'><span id='14vNp'><b id='14vNp'><form id='14vNp'><ins id='14vNp'></ins><ul id='14vNp'></ul><sub id='14vNp'></sub></form><legend id='14vNp'></legend><bdo id='14vNp'><pre id='14vNp'><center id='14vNp'></center></pre></bdo></b><th id='14vNp'></th></span></q></dt></tr></i><div id='14vNp'><tfoot id='14vNp'></tfoot><dl id='14vNp'><fieldset id='14vNp'></fieldset></dl></div>
    <legend id='14vNp'><style id='14vNp'><dir id='14vNp'><q id='14vNp'></q></dir></style></legend>
      • <bdo id='14vNp'></bdo><ul id='14vNp'></ul>
      <tfoot id='14vNp'></tfoot>

        <small id='14vNp'></small><noframes id='14vNp'>

      1. C# 中的 LinkedHashSet (Java) 等价物是什么?

        时间:2023-10-14

        <tfoot id='tKUI0'></tfoot>
            <tbody id='tKUI0'></tbody>
        1. <i id='tKUI0'><tr id='tKUI0'><dt id='tKUI0'><q id='tKUI0'><span id='tKUI0'><b id='tKUI0'><form id='tKUI0'><ins id='tKUI0'></ins><ul id='tKUI0'></ul><sub id='tKUI0'></sub></form><legend id='tKUI0'></legend><bdo id='tKUI0'><pre id='tKUI0'><center id='tKUI0'></center></pre></bdo></b><th id='tKUI0'></th></span></q></dt></tr></i><div id='tKUI0'><tfoot id='tKUI0'></tfoot><dl id='tKUI0'><fieldset id='tKUI0'></fieldset></dl></div>

          <small id='tKUI0'></small><noframes id='tKUI0'>

            <legend id='tKUI0'><style id='tKUI0'><dir id='tKUI0'><q id='tKUI0'></q></dir></style></legend>

              • <bdo id='tKUI0'></bdo><ul id='tKUI0'></ul>
                • 本文介绍了C# 中的 LinkedHashSet (Java) 等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  什么是 C# 中的 LinkedHashSet (Java) 的等价物?

                  What is the equivalent of a LinkedHashSet (Java) in C#?

                  推荐答案

                  我完成了未完成的方法,并大致打磨了'achitaka-san'发布的课程.

                  I completed the unfinished methods and generally polished the class that 'achitaka-san' posted.

                  public class LinkedHashSet<T> : ISet<T> {
                  
                      private readonly IDictionary<T, LinkedListNode<T>> dict;
                      private readonly LinkedList<T> list;
                  
                      public LinkedHashSet(int initialCapacity) {
                          this.dict = new Dictionary<T,LinkedListNode<T>>(initialCapacity);
                          this.list = new LinkedList<T>();
                      }
                  
                      public LinkedHashSet() {
                          this.dict = new Dictionary<T,LinkedListNode<T>>();
                          this.list = new LinkedList<T>();
                      }
                  
                      public LinkedHashSet(IEnumerable<T> e) : this() {
                          addEnumerable(e);
                      }
                  
                      public LinkedHashSet(int initialCapacity, IEnumerable<T> e) : this(initialCapacity) {
                          addEnumerable(e);
                      }
                  
                      private void addEnumerable(IEnumerable<T> e) {
                          foreach (T t in e) {
                              Add(t);
                          }
                      }
                  
                      //
                      // ISet implementation
                      //
                  
                      public bool Add(T item) {
                          if (this.dict.ContainsKey(item)) {
                              return false;
                          }
                          LinkedListNode<T> node = this.list.AddLast(item);
                          this.dict[item] = node;
                          return true;
                      }
                  
                      public void ExceptWith(IEnumerable<T> other) {
                          if (other == null) {
                              throw new ArgumentNullException("other cannot be null");
                          }
                          foreach (T t in other) {
                              Remove(t);
                          }
                      }
                  
                      public void IntersectWith(IEnumerable<T> other) {
                          if (other == null) {
                              throw new ArgumentNullException("other cannot be null");
                          }
                          T[] ts = new T[Count];
                          CopyTo(ts, 0);
                          foreach (T t in ts) {
                              if (!System.Linq.Enumerable.Contains(other, t)) {
                                  Remove(t);
                              }
                          }
                      }
                  
                      public bool IsProperSubsetOf(IEnumerable<T> other) {
                          if (other == null) {
                              throw new ArgumentNullException("other cannot be null");
                          }
                          int contains = 0;
                          int noContains = 0;
                          foreach (T t in other) {
                              if (Contains(t)) {
                                  contains++;
                              } else {
                                  noContains++;
                              }
                          }
                          return contains == Count && noContains > 0;
                      }
                  
                      public bool IsProperSupersetOf(IEnumerable<T> other) {
                          if (other == null) {
                              throw new ArgumentNullException("other cannot be null");
                          }
                          int otherCount = System.Linq.Enumerable.Count(other);
                          if (Count <= otherCount) {
                              return false;
                          }
                          int contains = 0;
                          int noContains = 0;
                          foreach (T t in this) {
                              if (System.Linq.Enumerable.Contains(other, t)) {
                                  contains++;
                              } else {
                                  noContains++;
                              }
                          }
                          return contains == otherCount && noContains > 0;
                      }
                  
                      public bool IsSubsetOf(IEnumerable<T> other) {
                          if (other == null) {
                              throw new ArgumentNullException("other cannot be null");
                          }
                          foreach (T t in this) {
                              if (!System.Linq.Enumerable.Contains(other, t)) {
                                  return false;
                              }
                          }
                          return true;
                      }
                  
                      public bool IsSupersetOf(IEnumerable<T> other) {
                          if (other == null) {
                              throw new ArgumentNullException("other cannot be null");
                          }
                          foreach (T t in other) {
                              if (!Contains(t)) {
                                  return false;
                              }
                          }
                          return true;
                      }
                  
                      public bool Overlaps(IEnumerable<T> other) {
                          if (other == null) {
                              throw new ArgumentNullException("other cannot be null");
                          }
                          foreach (T t in other) {
                              if (Contains(t)) {
                                  return true;
                              }
                          }
                          return false;
                      }
                  
                      public bool SetEquals(IEnumerable<T> other) {
                          if (other == null) {
                              throw new ArgumentNullException("other cannot be null");
                          }
                          int otherCount = System.Linq.Enumerable.Count(other);
                          if (Count != otherCount) {
                              return false;
                          }
                          return IsSupersetOf(other);
                      }
                  
                      public void SymmetricExceptWith(IEnumerable<T> other) {
                          if (other == null) {
                              throw new ArgumentNullException("other cannot be null");
                          }
                          T[] ts = new T[Count];
                          CopyTo(ts, 0);
                          HashSet<T> otherList = new HashSet<T>(other);
                          foreach (T t in ts) {
                              if (otherList.Contains(t)) {
                                  Remove(t);
                                  otherList.Remove(t);
                              }
                          }
                          foreach (T t in otherList) {
                              Add(t);
                          }
                      }
                  
                      public void UnionWith(IEnumerable<T> other) {
                          if (other == null) {
                              throw new ArgumentNullException("other cannot be null");
                          }
                          foreach (T t in other) {
                              Add(t);
                          }
                      }
                  
                      //
                      // ICollection<T> implementation
                      //
                  
                      public int Count {
                          get {
                              return this.dict.Count;
                          }
                      }
                  
                      public bool IsReadOnly {
                          get {
                              return this.dict.IsReadOnly;
                          }
                      }
                  
                      void ICollection<T>.Add(T item) {
                          Add(item);
                      }
                  
                      public void Clear() {
                          this.dict.Clear();
                          this.list.Clear();
                      }
                  
                      public bool Contains(T item) {
                          return this.dict.ContainsKey(item);
                      }
                  
                      public void CopyTo(T[] array, int arrayIndex) {
                          this.list.CopyTo(array, arrayIndex);
                      }
                  
                      public bool Remove(T item) {
                          LinkedListNode<T> node;
                          if (!this.dict.TryGetValue(item, out node)) {
                              return false;
                          }
                          this.dict.Remove(item);
                          this.list.Remove(node);
                          return true;
                      }
                  
                      //
                      // IEnumerable<T> implementation
                      //
                  
                      public IEnumerator<T> GetEnumerator() {
                          return this.list.GetEnumerator();
                      }
                  
                      //
                      // IEnumerable implementation
                      //
                  
                      IEnumerator IEnumerable.GetEnumerator() {
                          return this.list.GetEnumerator();
                      }
                  
                  }
                  

                  需要的用途:

                  using System;
                  using System.Collections;
                  using System.Collections.Generic;
                  

                  警告:该类大部分未经测试,尤其是 ISet 方法.使用风险自负.
                  我希望有人觉得这很有用.:)

                  Warning: The class is largely untested, especially the ISet methods. Use at your own risk.
                  I hope someone finds this useful. :)

                  这篇关于C# 中的 LinkedHashSet (Java) 等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:无法实例化类型 Set 下一篇:如何获得两个地图Java之间的差异?

                  相关文章

                    <bdo id='r4n9B'></bdo><ul id='r4n9B'></ul>
                • <small id='r4n9B'></small><noframes id='r4n9B'>

                      <i id='r4n9B'><tr id='r4n9B'><dt id='r4n9B'><q id='r4n9B'><span id='r4n9B'><b id='r4n9B'><form id='r4n9B'><ins id='r4n9B'></ins><ul id='r4n9B'></ul><sub id='r4n9B'></sub></form><legend id='r4n9B'></legend><bdo id='r4n9B'><pre id='r4n9B'><center id='r4n9B'></center></pre></bdo></b><th id='r4n9B'></th></span></q></dt></tr></i><div id='r4n9B'><tfoot id='r4n9B'></tfoot><dl id='r4n9B'><fieldset id='r4n9B'></fieldset></dl></div>
                      <legend id='r4n9B'><style id='r4n9B'><dir id='r4n9B'><q id='r4n9B'></q></dir></style></legend>

                      <tfoot id='r4n9B'></tfoot>