Finding an Array List for the Most Common Strings

I was wondering how I can find ArrayList of Strings to find the most common “destination” in the “Route” object I created (which contains a list of different destinations.)

So far, I:

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 } 

I'm having trouble creating an algorithm to search for an array for the most common string or strings, if there is a connection; and then displays the Route object number (parameter value) the line is in. Any help would be great, thanks!

+4
source share
4 answers

I would do a HashMap<String,Integer> . Then I go through each route, and if the destination was not on the Map, I would create an entry with put (destination, 1), otherwise I would increase the account that was there with put (destination, get (destination) + 1). Subsequently, I looked at the entries in the Map and searched for the one with the highest score.

+8
source

If you don't mind using an external jar, you can use the HashBag from publicly available apache to make this easy.

 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); } } 

Then you can call names.getCount ("destination1") to get the number of occurrences of destination1

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

0
source

Try the group function of the lambdaj library. To solve your problem, you can group Itenarary objects in the destination property and then find the group with the largest size, as in the following example:

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

In statistics, this is called a "mode . " Vanilla Java 8 solution looks like this:

 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λ is a library that supports mode() by stream. The following program:

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

(disclaimer: I work for jOOλ in the company)

0
source

Source: https://habr.com/ru/post/1301741/


All Articles