如何根据给定的模式重新排列列表中的项目?

编程入门 行业动态 更新时间:2024-10-07 16:24:54
本文介绍了如何根据给定的模式重新排列列表中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个ID列表,像这样: 5,3,2,4,1 ,然后我用这个ID从DB查询项目。但他们来自DB通常按id排序,我的意思是 1,2,3,4,5 。我需要将它们重新排列成与它们的id列表相同的顺序。我怎么能做到这一点?

编辑:澄清,因为许多人似乎对这个问题感到困惑。一个ID列表用于查询数据库,例如像这样:

SELECT * FROM Foo WHERE id in(5, 3,2,4,1)

Foo对象的结果列表没有以相同的方式排序作为ID的列表。问题是如何获得与ID的初始列表相同顺序的Foo对象列表。

解决方案

我想你必须在代码中重新排序查询结果:

public static void main(String [] args){ 列表<整数> ids = Arrays.asList(5,3,2,4,1,6); 列表< Foo>结果= Arrays.asList(新的Foo(1),新的Foo(8),新的Foo(2),新的Foo(4),新的Foo(5),新的Foo(7)); System.out.println(sortResults1:+ sortResults1(ids,results)); System.out.println(sortResults2:+ sortResults2(ids,results)); } private static List< Foo> sortResults1(List< Integer> ids,List< Foo>结果){ Foo [] sortedResultsArray = new Foo [ids.size()]; for(Foo result:results){ //查找此结果的id所需的位置 int pos = ids.indexOf(result.getId()); if(pos> = 0){ sortedResultsArray [pos] = result; } } 列表< Foo> sortedResults = new ArrayList<>(Arrays.asList(sortedResultsArray)); sortedResults.removeAll(Collections.singleton(null)); return sortedResults; } private static List< Foo> sortResults2(List< Integer> ids,List< Foo> results){ Collections.sort(results,ComparatorparingInt(item - > ids.indexOf(item.getId()))); 返回结果; $ / code>

第一个解决方案省略了列表中没有的ID的ids。

比较结果的第二个解决方案将任何未知ID的结果放在结果列表的前面。

输出:

sortResults1:[Foo 5,Foo 2,Foo 4,Foo 1] sortResults2 [Foo 8,Foo 7,Foo 5,Foo 2,Foo 4,Foo 1]

注意:在这里找到类似的问题:排序具有特定顺序的(数组)列表 它具有TreeMap的合理答案。

I have list of ids, like this: 5, 3, 2, 4, 1 , then i query items from DB with that ids. But they coming from DB usually sorted by id, i mean 1, 2, 3, 4, 5. I need to rearrange them to be in the same order as they ids list. How can i achieve that?

EDIT: A clarification since many people seem confused about this question. A list of IDs is used to query the database, for example like this:

SELECT * FROM Foo WHERE id in (5, 3, 2, 4, 1)

The resulting list of Foo objects is not ordered in the same way as the list of IDs. The question is how to obtain a list of Foo objects with the same order as the initial list of IDs.

解决方案

I think you have to re-order the result of the query in code:

public static void main(String[] args) { List<Integer> ids = Arrays.asList(5, 3, 2, 4, 1, 6); List<Foo> results = Arrays.asList(new Foo(1), new Foo(8), new Foo(2), new Foo(4), new Foo(5), new Foo(7)); System.out.println("sortResults1: " + sortResults1(ids, results)); System.out.println("sortResults2: " + sortResults2(ids, results)); } private static List<Foo> sortResults1(List<Integer> ids, List<Foo> results) { Foo[] sortedResultsArray = new Foo[ids.size()]; for (Foo result : results) { // look up the required position of this result's id int pos = ids.indexOf(result.getId()); if (pos >= 0) { sortedResultsArray[pos] = result; } } List<Foo> sortedResults = new ArrayList<>(Arrays.asList(sortedResultsArray)); sortedResults.removeAll(Collections.singleton(null)); return sortedResults; } private static List<Foo> sortResults2(List<Integer> ids, List<Foo> results) { Collections.sort(results, ComparatorparingInt(item -> ids.indexOf(item.getId()))); return results; }

The first solution omits any results with an ID that does not occur in the list of ids.

The second solution with the comparator puts any results with unknown IDs at the front of the result list.

Output:

sortResults1: [Foo 5, Foo 2, Foo 4, Foo 1] sortResults2[Foo 8, Foo 7, Foo 5, Foo 2, Foo 4, Foo 1]

Note: Found a similar question here: Sort an (Array)List with a specific order It has a reasonable answer with a TreeMap.

更多推荐

如何根据给定的模式重新排列列表中的项目?

本文发布于:2023-11-29 15:06:47,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:排列   模式   项目   列表中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!