有没有一种方法可以按照传递的顺序从集合中恢复对象?

发布于 2024-11-07 14:03:13 字数 62 浏览 0 评论 0原文

我需要按照传递到集合中的相同顺序恢复 java Set 集合接口的元素。

在java中怎么可能

I need to recover the elements of a java Set collection interface in the same order they were passed into the set.

How is it possible in java

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

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

发布评论

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

评论(2

表情可笑 2024-11-14 14:03:13

LinkedHashSet< 怎么样? /a>?

Set 接口的哈希表和链表实现,具有可预测的迭代顺序。此实现与 HashSet 的不同之处在于,它维护一个贯穿其所有条目的双向链表。该链表定义了迭代顺序,即元素插入集合的顺序(插入顺序)。请注意,如果将元素重新插入集合中,插入顺序不会受到影响。 (如果在调用之前 s.contains(e) 返回 true 时调用 s.add(e),则元素 e 会重新插入到集合 s 中。)

How about LinkedHashSet ?

Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set. (An element e is reinserted into a set s if s.add(e) is invoked when s.contains(e) would return true immediately prior to the invocation.)

天冷不及心凉 2024-11-14 14:03:13
public class SimpleLinkedHashSetExample {

  public static void main(String[] args) {
    //create object of LinkedHashSet
    LinkedHashSet lhashSet = new LinkedHashSet();

    /*
      Add an Object to LinkedHashSet using
      boolean add(Object obj) method of Java LinkedHashSet class.
      This method adds an element to LinkedHashSet if it is not 
      already present in LinkedHashSet.
      It returns true if the element was added to LinkedHashSet, false otherwise.
    */

    lhashSet.add(new Integer("1"));
    lhashSet.add(new Integer("2"));
    lhashSet.add(new Integer("3"));

    /*
      Please note that add method accepts Objects. Java Primitive values CAN NOT
      be added directly to LinkedHashSet. It must be converted to corrosponding
      wrapper class first.
    */

    System.out.println("LinkedHashSet contains.." + lhashSet);   
  }
}

/*
Output of the program would be
LinkedHashSet contains..[1, 2, 3]
*/ 

您可以使用上面的 LinkedhashSet 示例。

public class SimpleLinkedHashSetExample {

  public static void main(String[] args) {
    //create object of LinkedHashSet
    LinkedHashSet lhashSet = new LinkedHashSet();

    /*
      Add an Object to LinkedHashSet using
      boolean add(Object obj) method of Java LinkedHashSet class.
      This method adds an element to LinkedHashSet if it is not 
      already present in LinkedHashSet.
      It returns true if the element was added to LinkedHashSet, false otherwise.
    */

    lhashSet.add(new Integer("1"));
    lhashSet.add(new Integer("2"));
    lhashSet.add(new Integer("3"));

    /*
      Please note that add method accepts Objects. Java Primitive values CAN NOT
      be added directly to LinkedHashSet. It must be converted to corrosponding
      wrapper class first.
    */

    System.out.println("LinkedHashSet contains.." + lhashSet);   
  }
}

/*
Output of the program would be
LinkedHashSet contains..[1, 2, 3]
*/ 

You can use LinkedhashSet above is the example.

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