java queue中佇列的使用示例及常用方法示例和比較
阿新 • • 發佈:2019-01-02
package com.yanshu.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import com.alibaba.fastjson.JSON;
import com.yanshu.dao.UserDao;
import com.yanshu.pojo.Business;
import com.yanshu.pojo.User;
import com.yanshu.pojo.Users;
import com.yanshu.service.UserService;
@RestController
@CrossOrigin
public class UserController {
//Queue<User> queue = new ArrayList<>();
Queue<Business> queue = new LinkedList<Business>();
@Autowired
private UserDao userdao;
@Autowired
private UserService userService;
///================
@RequestMapping("/getushow")
public String getU()
{
//得到的資料
//List<User> listuser=userdao.getUs();
List<Business> listuser=userdao.getUserww();
System.out.println(listuser);
//把所有的資料放到Queue中
for (Business object : listuser) {
queue.offer(object);
}
System.out.println(queue.size());
//每次請求獲取10個狀態為0 的關鍵詞。佇列中儲存200關鍵詞。
List<Business> returnUsers=new ArrayList<>();
for(int i=1;i<=3;i++)
{
returnUsers.add(queue.poll());
System.out.println(queue.size());
}
System.out.println(JSON.toJSONString(returnUsers));
//return JSON.toJSONString(returnUsers);
return JSON.toJSONString(returnUsers);
}
//================
/**
* 一對一條件查詢 service
* @return
*/
@RequestMapping("/userser")
public String getUserser()
{
//System.out.println("------一對一條件查詢-------");
List<Users> use=userService.getUserService();
System.out.println(use);
return JSON.toJSONString(use);
}
@RequestMapping("/userser1")
public String getUserService()
{
List<Users> use=userService.findUserOne();
System.out.println(use);
return JSON.toJSONString(use);
}
@RequestMapping("/moreuser")
public String getUserFind()
{
List<Users> use=userService.findMore();
System.out.println(use);
return JSON.toJSONString(use);
}
//在java5中新增加了java.util.Queue介面,用以支援佇列的常見操作。該介面擴充套件了java.util.Collection介面。
//
public static void main(String[] args) {
/* Queue queue = new LinkedList();
queue.offer("Hello");
queue.offer("World!");
queue.offer("你好!");
System.out.println(queue.size());
Object str;
while((str=queue.poll())!=null){
System.out.print(str);
}
System.out.println();
System.out.println(queue.size());*/
//return JSON.toJSONString(map);
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import com.alibaba.fastjson.JSON;
import com.yanshu.dao.UserDao;
import com.yanshu.pojo.Business;
import com.yanshu.pojo.User;
import com.yanshu.pojo.Users;
import com.yanshu.service.UserService;
@RestController
@CrossOrigin
public class UserController {
//Queue<User> queue = new ArrayList<>();
Queue<Business> queue = new LinkedList<Business>();
@Autowired
private UserDao userdao;
@Autowired
private UserService userService;
///================
@RequestMapping("/getushow")
public String getU()
{
//得到的資料
//List<User> listuser=userdao.getUs();
List<Business> listuser=userdao.getUserww();
System.out.println(listuser);
//把所有的資料放到Queue中
for (Business object : listuser) {
queue.offer(object);
}
System.out.println(queue.size());
//每次請求獲取10個狀態為0 的關鍵詞。佇列中儲存200關鍵詞。
List<Business> returnUsers=new ArrayList<>();
for(int i=1;i<=3;i++)
{
returnUsers.add(queue.poll());
System.out.println(queue.size());
}
System.out.println(JSON.toJSONString(returnUsers));
//return JSON.toJSONString(returnUsers);
return JSON.toJSONString(returnUsers);
}
//================
/**
* 一對一條件查詢 service
* @return
*/
@RequestMapping("/userser")
public String getUserser()
{
//System.out.println("------一對一條件查詢-------");
List<Users> use=userService.getUserService();
System.out.println(use);
return JSON.toJSONString(use);
}
@RequestMapping("/userser1")
public String getUserService()
{
List<Users> use=userService.findUserOne();
System.out.println(use);
return JSON.toJSONString(use);
}
@RequestMapping("/moreuser")
public String getUserFind()
{
List<Users> use=userService.findMore();
System.out.println(use);
return JSON.toJSONString(use);
}
//在java5中新增加了java.util.Queue介面,用以支援佇列的常見操作。該介面擴充套件了java.util.Collection介面。
//
public static void main(String[] args) {
/* Queue queue = new LinkedList();
queue.offer("Hello");
queue.offer("World!");
queue.offer("你好!");
System.out.println(queue.size());
Object str;
while((str=queue.poll())!=null){
System.out.print(str);
}
System.out.println();
System.out.println(queue.size());*/
//return JSON.toJSONString(map);
}
}