索引一个数组列表

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

Indexing an array list

问题

我在Java中有一个类似这样的ArrayList,其中大约有242个元素。

ArrayList<String> AL = new ArrayList<>();

AL.add("Apple");
AL.add("Banana");
AL.add("BArbie");
AL.add("Elephant");
AL.add("Zombie");
// 以此类推...

一旦值被插入,我使用以下方式进行排序:

Collections.sort(AL);

现在我想基于第一个字符为这个ArrayList创建一个索引。在上面的示例中,如果我想定位Z,它将位于位置5,依此类推。如果选择了Y,则应该给出最近的先前值,在这种情况下是Elephant。

目前通过使用2个数据库表和Java处理来实现此解决方案。有关如何减少复杂性的任何建议都可以吗?

如果我们获得关于如何达到替代解决方案的指导,我们将非常高兴。

谢谢。

英文:

I have an arraylist like this in Java with around 242 elements in it.

ArrayList&lt;String&gt; AL=new ArrayList&lt;&gt;();

AL.add(&quot;Apple&quot;);
AL.add(&quot;Banana&quot;);
AL.add(&quot;BArbie&quot;);
AL.add(&quot;Elephant&quot;);
AL.add(&quot;Zombie&quot;);
and so on...

Once the values are inserted, i sort using

Collections.sort(AL);

Now i want to create an index for this ArrayList based on the first character. In the above example, if i want to locate Z, it would be position 5 and so on. If y is selected, then the nearest previous value to be given , in this case it is Elephant.

This solution is achieved currently by processing the data using 2 database tables and java processing. any suggestions on how to minimize the complexity.

If we get direction on how to get to an alternate solution, we would be really happy.

Thanks.

答案1

得分: 0

Sure, here is the translated content:

  1. 我认为你使用了不正确的“Collection”。如果你没有重复项 - 那么请使用“TreeSet”,否则使用“PriorityQueue”。这些集合有内部排序。
  2. 据我理解,您正在添加新元素并希望检索新索引。为什么不使用Map<Character, Set<String>>呢?
英文:
  1. I think you use incorrect Collection. In case you do not have duplicates - then do use TreeSet, otherwise - PriorityQueue. These collections have internal sortings.
  2. As I understand you add new elements and want to retreive new index. Why do not you use Map&lt;Character, Set&lt;String&gt;&gt; instead?!

答案2

得分: 0

为了使其更加生动,让我们假设您有一个国家列表。Arrays.asList("Afghanistan", "Albania", "Eritrea", ...) 您可以从列表中创建一个 TreeMap<Character, List<String>>,其中以首个字符为键,以以该键开头的字符串列表为值。

我故意建议在这里使用 TreeMap,因为您想要的功能已经在方法 floorEntry()floorKey() 中实现。

以下示例应该为您提供一个起点:

import java.util.Arrays;
import java.util.List;
import java.util.TreeMap;
import java.util.stream.Collectors;

public class Test {

    public static void main(String[] args) throws Exception {
        List<String> countries = Arrays.asList("Afghanistan", "Albania", "Algeria",
                                                "Bahamas", "Bahrain", "Bangladesh",
                                                "Cameroon", "Canada", "Central African Republic",
                                                "Eritrea", "Estonia", "Ethiopia",
                                                "Nigeria", "North Korea", "Norway",
                                                "Sweden", "Switzerland", "Syria",
                                                "Uganda", "Ukraine", "United Kingdom",
                                                "Venezuela", 
                                                "Yemen",
                                                "Zimbabwe");

        TreeMap<Character, List<String>> map = countries.stream()
                .collect(Collectors.groupingBy(s -> s.charAt(0), TreeMap::new, Collectors.toList()));
            
        System.out.println(map.floorEntry('F').getValue().get(0));
    }
}
英文:

To make it more vivid let's assume you have a list of countries. Arrays.asList(&quot;Afghanistan&quot;, &quot;Albania&quot;, &quot;Eritrea&quot;, ...) You could create a map TreeMap&lt;Character,List&lt;String&gt;&gt; out of your list with first char as key and a list of strings which starts with the key as value.

I intentionally suggest treemap here because the functionality you want is already implemented in the methods floorEntry() and floorKey().

The following example should give you a starting point:

import java.util.Arrays;
import java.util.List;
import java.util.TreeMap;
import java.util.stream.Collectors;

public class Test {

    public static void main(String[] args) throws Exception {
        List&lt;String&gt; countries = Arrays.asList(&quot;Afghanistan&quot;, &quot;Albania&quot;, &quot;Algeria&quot;,
                                                &quot;Bahamas&quot;, &quot;Bahrain&quot;, &quot;Bangladesh&quot;,
                                                &quot;Cameroon&quot;, &quot;Canada&quot;, &quot;Central African Republic&quot;,
                                                &quot;Eritrea&quot;, &quot;Estonia&quot;, &quot;Ethiopia&quot;,
                                                &quot;Nigeria&quot;, &quot;North Korea&quot;, &quot;Norway&quot;,
                                                &quot;Sweden&quot;, &quot;Switzerland&quot;, &quot;Syria&quot;,
                                                &quot;Uganda&quot;, &quot;Ukraine&quot;, &quot;United Kingdom&quot;,
                                                &quot;Venezuela&quot;, 
                                                &quot;Yemen&quot;,
                                                &quot;Zimbabwe&quot;);

        TreeMap&lt;Character,List&lt;String&gt;&gt; map = countries.stream()
                .collect(Collectors.groupingBy(s -&gt; s.charAt(0), TreeMap::new, Collectors.toList()));
        
        System.out.println(map.floorEntry(&#39;F&#39;).getValue().get(0));
    }
}

huangapple
  • 本文由 发表于 2020年9月2日 01:49:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63692934.html
匿名

发表评论

匿名网友

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

确定