2010-02-18 81 views
4

我想知道如何搜索字符串的ArrayList,以查找我创建的“行程”对象中最常见的“目标”(其中包含不同目标的列表。 )在数组列表中搜索最常见的字符串

到目前为止,我有:

public static String commonName(ArrayList<Itinerary> itinerary){ 

    int count = 0; 
    int total = 0; 

    ArrayList<String> names = new ArrayList<String>(); 
    Iterator<String>itr2 = names.iterator(); 

    while(itr.hasNext()){ 

     Itinerary temp = itr.next(); 

     if(temp.iterator().hasNext()){ //if its has destinations 

       // Destination object in itinerary object 
       Destination temp2 = temp.iterator().next(); 
       String name = temp2.getDestination().toLowerCase().replace(" ", ""); 

       if(names.contains(name)){ 
        count = count + 1; 
        //do something with counting the occurence of string name here 
       } 

我在做一个算法来搜索最常出现的字符串或字符串数​​组,如果出现平局的问题;然后显示找到该字符串的'行程对象'的数量(参数值)。任何帮助都会很棒,谢谢!

+0

添加到每一行文字描述的Java代码说什么评论是没有用的,因为事实上它是分心和丑陋。 – 2010-02-18 19:57:21

+0

为了增加保罗的观点,当你改变代码但不更新评论以反映改变 – mfeingold 2010-02-18 20:07:19

回答

8

我会做一个HashMap<String,Integer>。然后我会查看每个行程,如果目的地在地图上没有创建一个条目(目的地,1),否则我会增加存在的数量(目的地,获取(目的地)+ 1)。之后,我会浏览Map条目并查找最高点的条目。

+0

它可能不是最有效的(基于CPU周期),但它绝对是第一个想到的最少量完成工作的代码。 +1。 – 2010-02-18 20:01:53

+0

如果您在加入get(destingation)+1步骤时保持“最大”值,则可以跳过最后一步(遍历映射条目并查找最高计数)。如果新值比任何看到的都大,则存储指向该条目的指针。 (我认为这是最快的解决方案,因为它是O(n)。肯定比排序nLogn快) – 2010-02-18 20:10:20

+0

我的解决方案是O(N) - 您遍历行程列表一次,然后遍历列表的目的地一次。 – 2010-02-18 20:12:51

0

如果您不介意使用外部jar,那么您可以使用Apache commons中的HashBag轻松完成此操作。

public static String commonName(ArrayList<Itinerary> itinerary){ 

int count = 0; 
int total = 0; 
Bag names = new HashBag(); 

while(itr.hasNext()){ //while array of Itinerary object has next 
    Itinerary temp = itr.next(); //temp = 1st itineray object 
    if(temp.iterator().hasNext()){ //if its has destinations 
      Destination temp2 = temp.iterator().next(); //n Destination object in itinerary object 
      String name = temp2.getDestination().toLowerCase().replace(" ", ""); 
      names.add(name, 1); 
    } 
} 

再后来,你可以调用names.getCount( “DESTINATION1”),以获得

http://commons.apache.org/collections/userguide.html#Bags

+0

谢谢大家,非常快速和有益的答案!我刚开始学习Java,虽然我没有在使用HashBag之前一定要在Java API中查找它!再次感谢你! – LeighA 2010-02-18 23:43:17

0

尝试lambdaj库组特征DESTINATION1的出现的次数。为了解决你的问题,你能集团的目标属性的Itenarary对象,然后找到最大规模的集团,如下面的例子:

Group<Sale> group = selectMax(group(itineraries, 
    by(on(Itenarary.class).getDestination())).subgroups(), on(Group.class).getSize()); 
0

In statistics, this is called the "mode"。香草的Java 8的解决方案是这样的:

itinerary 
     .stream() 
     .flatMap(i -> StreamSupport.stream(
      Spliterators.spliteratorUnknownSize(i.iterator(), 0) 
    )) 
     .collect(Collectors.groupingBy(
      s -> s.getDestination().toLowerCase().replace(" ", ""), 
      Collectors.counting() 
    )) 
     .entrySet() 
     .stream() 
     .max(Comparator.comparing(Entry::getValue)) 
     .ifPresent(System.out::println); 

jOOλ是支持流mode()库。下面的程序:

System.out.println(
    Seq.seq(itinerary) 
     .flatMap(i -> Seq.seq(i.iterator())) 
     .map(s -> s.getDestination().toLowerCase().replace(" ", "")) 
     .mode() 
); 

(声明:我身后jOOλ公司工作)