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
/*
* 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
Int sum=pList. Stream (). MapToInt (p - & gt; Used p.u sers. The size ()). The sum ();
return sum;
}
Private static int getUserNum (long pId, ArrayList
List
ConcurrentHashMap
Int size=users. The size ();
Return the size;
}
Private static void bindingProduct (uId, long long pId, ArrayList
The Product the Product=new Product ();
Product. SetpId (pId);
ConcurrentHashMap
Users. The put (uId, 1);
Product. SetUsers (users);
PList. Add (product);
}
Static class Product {
Private long pId;
Private ConcurrentHashMap
Public long getpId () {
Return the pId;
}
Public void setpId (pId) {
This. PId=pId;
}
Public ConcurrentHashMap
Return the users;
}
Public void setUsers (ConcurrentHashMap
Enclosing the users=the users;
}
}
Static class User {
Private long uId;
Public long getuId () {
Return the uId.
}
Public void setuId (uId) {
UId=uId; this.
}
}
}