Base Algorithm 7: Take the most frequently occurring data from a field in a database to form "Hot XXX"

Source: Internet
Author: User

As title shows, sometimes we need to take data from a table in a database and display "Hot City", "Hot item" and other information in the foreground page based on how often it appears. For example, if we want to take out all the "hot cities" in the course, we can do this:

(1) The data is the same in the database:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7F/FD/wKiom1czVEbxkDl0AABsbJonIwU594.png "title=" 20160428165209891.png "alt=" Wkiom1czvebxkdl0aabsbjoniwu594.png "/>

Here, we need to find out all the "departure city" and "Arrival City", and count each city according to the number of occurrences to remove the most frequently seen cities as the top cities

Note: You can use the following SQL statement to combine data from multiple fields and not be re-processed:

Select Departure_airport from usr_air_line UNION ALL select Arrival_airport from Usr_air_line;
(2) count, sort, and find the last "Hot City" for the data that was taken out:
package cn.zifangsky.base;import java.io.bufferedreader;import java.io.file;import  java.io.filereader;import java.util.arraylist;import java.util.collections;import  Java.util.comparator;import java.util.hashmap;import java.util.list;import java.util.map;import  java.util.map.entry;public class popairportdemo {public static void main ( String[] args)  throws Exception {//  Read file bufferedreader reader = new  bufferedreader (New filereader (New file ("C:/users/administrator/desktop/airport.txt")); string temp =  ""; List<string> airportcodes = new arraylist<string> (); //  City three loadline set while   ((Temp = reader.readline ())  != null)  {airportcodes.add (temp);} Reader.close (); Map<string, integer> countmap = new hashmap<string, integer> ();   //< City three Loadline, total number of times >//traversal list formation < City three loadline, total number of times > key value to for  (string code : airportcodes)  {if   (!countmap.containskey (code))  {countmap.put (code, 1);}  else {countmap.put (Code, countmap.get (code)  + 1);}}   Sort list<map.entry<string, integer>> list = new arraylist< Map.entry<string, integer>> (Countmap.entryset ()); Collections.sort (list, new comparator<map.entry<string, integer>> ()  {// Custom Sort Public int compare (entry<string, integer> o1, entry<string,  INTEGER&GT;&NBSP;O2)  {return o2.getvalue (). CompareTo (O1.getvalue ());}); List<string> result = new arraylist<string> ();   //Popular Cities Collection int  point = 0;for  (map.entry<string, integer> mapping : list)  {//  system.out.println (Mapping.getkey ()  +  " -> "   +// mapping.getvalue ()); Result.add (Mapping.getkey ());p oint++;//here to take 5 popular cities if  (point >= 5 ) break;} SYSTEM.OUT.PRINTLN (result);}}

Note: In order to simplify the operation, I have saved all the city's three loadline to the "airport.txt" file, which focuses only on the entire algorithm flow, regardless of JDBC.


This article is from "Zifangsky's personal blog" blog, make sure to keep this source http://983836259.blog.51cto.com/7311475/1772447

Base Algorithm 7: Take the most frequently occurring data from a field in a database to form "Hot XXX"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.