英文:
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<String> AL=new ArrayList<>();
AL.add("Apple");
AL.add("Banana");
AL.add("BArbie");
AL.add("Elephant");
AL.add("Zombie");
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:
- 我认为你使用了不正确的“Collection”。如果你没有重复项 - 那么请使用“TreeSet”,否则使用“PriorityQueue”。这些集合有内部排序。
- 据我理解,您正在添加新元素并希望检索新索引。为什么不使用
Map<Character, Set<String>>
呢?
英文:
- I think you use incorrect
Collection
. In case you do not have duplicates - then do useTreeSet
, otherwise -PriorityQueue
. These collections have internal sortings. - As I understand you add new elements and want to retreive new index. Why do not you use
Map<Character, Set<String>>
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("Afghanistan", "Albania", "Eritrea", ...)
You could create a map TreeMap<Character,List<String>>
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<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));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论