在集合之间复制顺序

发布于 2024-10-14 10:44:14 字数 1386 浏览 0 评论 0原文

在我的应用程序中,我需要按照请求的顺序保留结果集合(用户发送他想要的列表,并且最好以相同的顺序回答他)。

数据,我将发送给用户的数据是由底层生成的,它们既不知道正确的顺序,也不生成有序的结果。所以我必须自己订购。

我的方法如下,但我认为没有必要实现它,因为应该有一些标准的方法。那么,你会推荐什么?

public class KeepOrder {

/**
 * Knows how to extract A from B.
 * 
 * @param <A>
 * @param <B>
 */
public interface Extractor<A, B> {

    A extract(B from);

}

@SuppressWarnings("serial")
public static <T, F> Collection<T> keepOrder(final Collection<T> data, final Collection<F> order,
        final Extractor<F, T> extractor) {

    final Comparator<T> tComparator = new Comparator<T>() {

        @Override
        public int compare(final T o1, final T o2) {

            final F field1 = extractor.extract(o1);
            final F field2 = extractor.extract(o2);

            for (final F currentField : order) {
                if (currentField.equals(field1) && currentField.equals(field2)) {
                    return 0;
                }

                if (currentField.equals(field1)) {
                    return -1;
                }

                if (currentField.equals(field2)) {
                    return 1;
                }
            }

            return 0;
        }
    };

    return new TreeSet<T>(tComparator) {
        {
            addAll(data);
        }
    };

}

}

In my application I need to keep resulting collection in the same order as requested (user sends a list of what he wants and it is desirable to answer him in the same order).

Data, what I will send to user is generated by underlying layers, and they neither know the correct order, nor generate ordered result. So I have to order it myself.

My approach is following, but I think that it's not necessary to implement it, because there should be some standard way. So, what would you recommend?

public class KeepOrder {

/**
 * Knows how to extract A from B.
 * 
 * @param <A>
 * @param <B>
 */
public interface Extractor<A, B> {

    A extract(B from);

}

@SuppressWarnings("serial")
public static <T, F> Collection<T> keepOrder(final Collection<T> data, final Collection<F> order,
        final Extractor<F, T> extractor) {

    final Comparator<T> tComparator = new Comparator<T>() {

        @Override
        public int compare(final T o1, final T o2) {

            final F field1 = extractor.extract(o1);
            final F field2 = extractor.extract(o2);

            for (final F currentField : order) {
                if (currentField.equals(field1) && currentField.equals(field2)) {
                    return 0;
                }

                if (currentField.equals(field1)) {
                    return -1;
                }

                if (currentField.equals(field2)) {
                    return 1;
                }
            }

            return 0;
        }
    };

    return new TreeSet<T>(tComparator) {
        {
            addAll(data);
        }
    };

}

}

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

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

发布评论

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

评论(1

温柔戏命师 2024-10-21 10:44:14

如果顺序在您的集合中很重要,您应该将自己限制在 List 中。

来自 List 的文档< /a>:

有序集合(也称为序列)。此界面的用户可以精确控制每个元素在列表中的插入位置。用户可以通过整数索引(列表中的位置)访问元素,并在列表中搜索元素。

If the order is important in your collection, you should restrict yourself to Lists.

From the documentation of List:

An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

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