"Novice Learning Java" 8:list entity collections ensure product names are not duplicated __java

Source: Internet
Author: User

Problem Background:

In doing the embedded page of the app, the minister gave such a need to query the production information from the view of each product has matured a piece of information, in order to better display the effect. Simply put, the cucumber and eggplant were planted in the 1th shed and the 2nd shed, which will produce 4 planting information, we want to search according to the product name under each product name of any planting information can be. For example, cucumber planting information has two, casually take out a, eggplant also so, then we want the result is two (do not specify which two, random).

Problem Analysis :

At first I thought of it by writing an SQL statement, so I started experimenting in SQL Server

First, all the records that match the situation are queried.

<span style= "FONT-SIZE:18PX;" > select * from dbo.sn_plant_product_view where catename = ' processing class ' Andnowplant =0 and Harveststate =1</span>

Then use the GROUP BY clause to query the duplicate product names.

<span style= "FONT-SIZE:18PX;" >select pdtname from  dbo.sn_plant_product_view where catename= ' processing classes ' and Nowplant =0 and Harveststate GROUP by PD Tname</span>

Finally, in the combination of query, found that the use of splicing SQL statements is not workable, no matter how I write, return is all the results, the reason why. The reason is that group by can only group data, but it cannot complete the operation of taking one record in each group, in other words, using SQL statements, you can only group operations, not for you to complete a record in each set of data work. Of course we can count and compute the group by group results, such as counting the number of bars per set of data, and so on.

As for the effect I wanted to do only through the write algorithm, and began to think about how to write a program to achieve. In fact, as long as the above two results of comparison screening, you can get the desired results.

Problem Solving:

After a simple analysis, then the method is finished, test can solve the problem, the code is as follows:

Public list<snplantproductview> Listforapp (string catename) {//1. First query the product name that is not duplicated in the result set String hql = "Select PDTN Ame from Snplantproductview where catename=?
		and Nowplant=0 and harveststate=1 Group by Pdtname ";
		
		List<string> listname=super.listbyhql (HQL, catename);
		2. Query all records that meet the query criteria finder = Finder.create ("from Snplantproductview");
		Finder.append ("Where Catename =:catename and nowplant=0 and harveststate = 1");
		Finder.setparam ("Catename", catename);
		
		list<snplantproductview> list = Listbyfinder (finder); 3. Declare an entity List set variable to hold the duplicate result record list<snplantproductview> resultlist=new arraylist<snplantproductview>
		
		(); 4. Use a dual for loop to take out a record of the names in any matching ListName collection in the result set, put them in the Resultlist collection for (int i=0;i< listname.size (); i++) {for (int j=0;j&
				 Lt;list.size (); j + +) {* * * as long as the names of the products in the list collection have a match in the name of the product and the ListName collection, * Put this record in the resultlist and jump out of the current loop and take a name to match
	* Until all the names are traversed, the number of records in the Resultlist and the number of records in the ListName is the same, thus achieving the purpose of duplication			 */if (Listname.get (i). Equals (List.get (j). Getpdtname ())) {Resultlist.add (List.get (j));
				Break
	}} return resultlist; }

Summary :

In fact, my approach is still flawed, if the amount of data is very large, there will be performance bottlenecks, two for the loop, in the case of large amount of data, will consume a long time, to match, eventually to return the results, the speed will be greatly discounted. I know there are other ways, but I still can not think of, if the great God has any better solution, please do not hesitate to enlighten.

Related Article

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.