Home > Back-end >  Java written title
Java written title

Time:11-27

Statistical subscription number:
At the same time there are N goods (expressed in long product id), each item can be any of the users (user id) long subscribe, every by subscription, the subscription Numbers on the item 1, for the same goods from the same user can subscribe to an editor to write a class, use three methods to provide the following functions (the three methods are in single cut in a multithreaded environment) : 1) for the specified user id to subscribe to the specified commodity id 2) total return all goods to subscribe to 3) according to the commodities id to return to the subscription number

CodePudding user response:

Package com. Cn;

import java.util.ArrayList;
Import the Java. Util. Enumeration;
import java.util.List;
import java.util.Map;
Import the Java. Util. Concurrent. ConcurrentHashMap;
Import the Java. Util. Stream. The Collector;
Import the Java. Util. Stream. Collectors;
Import the Java. Util. Stream. The stream;

The import com. Cn. Demo2. The Product;
The import com. Cn. Demo2. User;

/* *
Statistical subscription number: * at the same time there are N goods (expressed in long product id), each item can be any of the users (user id) long subscribe,
* every by subscription, the subscription Numbers on the item 1, for the same goods from the same user can subscribe to an editor to write a class, use three methods to provide the following functions (the three methods are in single cut in a multithreaded environment) :
* 1) for the specified user id to subscribe to the specified commodities id 2) return all subscribe to the total number of 3) according to the commodities id returns this subscription number
*
* @ author jack
*
*/
Public class Demo2 {

Public static void main (String [] args) {

ArrayList PList=new ArrayList (a);

/*
* add N commodity
* bindingProduct (2, 2, pList);
* bindingProduct (3, 3, pList);
* bindingProduct (4, 4, pList);
* bindingProduct (5, 5, pList);
* bindingProduct (6, 6, pList);
*/


Long uid=1;
Long pId=1;
//1) for the specified user id to subscribe to the specified commodity id
BindingProduct (uid, pId, pList);


//2) return all goods total subscription
Int userNumTotal=getUserNumTotal (pList);
System. The out. Println (userNumTotal);
//3) according to the commodities ID returns this subscription number
Int userNum=getUserNum (pId, pList);
System. The out. Println (userNum);

}



Private static int getUserNumTotal (ArrayList PList) {

Int sum=pList. Stream (). MapToInt (p - & gt; Used p.u sers. The size ()). The sum ();
return sum;
}



Private static int getUserNum (long pId, ArrayList PList) {

List Collect=pList. Stream (). The filter (p - & gt; P.p Id==pId). Collect (Collectors. ToList ());

ConcurrentHashMap The users=pList. Stream (). The filter (p - & gt; P.p Id==pId). Collect (Collectors. ToList ()), get (0). The getUsers ();

Int size=users. The size ();

Return the size;

}



Private static void bindingProduct (uId, long long pId, ArrayList PList) {


The Product the Product=new Product ();
Product. SetpId (pId);
ConcurrentHashMap The users=new ConcurrentHashMap (a);
Users. The put (uId, 1);
Product. SetUsers (users);
PList. Add (product);
}



Static class Product {

Private long pId;

Private ConcurrentHashMap The users;

Public long getpId () {
Return the pId;
}

Public void setpId (pId) {
This. PId=pId;
}

Public ConcurrentHashMap GetUsers () {
Return the users;
}

Public void setUsers (ConcurrentHashMap The users) {
Enclosing the users=the users;
}
}

Static class User {

Private long uId;

Public long getuId () {
Return the uId.
}

Public void setuId (uId) {
UId=uId; this.
}


}
}
  • Related