Java 基于公共属性组合集合的智能方法

Java 基于公共属性组合集合的智能方法,java,guava,Java,Guava,假设我有以下两门课 public class Computer { private String computerName; private boolean hasTechnicalIssue; } public class TechnicalIssue { private String issueId; private String computerName; } public class ComputerManager { Comput

假设我有以下两门课

public class Computer {
    private String computerName;
    private boolean hasTechnicalIssue;
}   

public class TechnicalIssue {
    private String issueId;
    private String computerName;
}   

public class ComputerManager {

   ComputerDao computerDao;
   IssueDao issueDao;

   public void getComputers {
       List<Computer> computers = computerDao.getComputers();
       List<TechnicalIssues> technicalIssues = issueDao.getTechnicalIssues();  
       // I would like to do something here to set the hasTechnicalIssue flag
   }

}
公共类计算机{
私有字符串名称;
私人布尔哈斯特苏;
}   
公开课{
私有字符串issueId;
私有字符串名称;
}   
公共类计算机管理员{
电脑刀;
发出(IssueDao)发出(IssueDao);;
公共计算机{
List computers=computerDao.getComputers();
List technicalisues=issueDao.gettechnicalisues();
//我想在这里做点什么来树立哈斯特苏的旗帜
}
}
Computer类和TechnicalIssue类都具有computername属性

使用Guava是否有一种聪明的方法,可以通过使用Technicalissue列表中的computerName属性在computers列表中设置HasteTechnicalissue布尔值

例如,如果包含计算机的列表包含一个computerName为computer9999的条目,并且包含技术问题的列表也包含一个computerName相同的条目,则Computer类上的HasteTechnicalIssue应等于true

class Computer {

private String computerName;
private boolean hasTechnicalIssue;

public String getComputerName() {
    return computerName;
}

public void setComputerName(String computerName) {
    this.computerName = computerName;
}

public boolean isHasTechnicalIssue() {
    return hasTechnicalIssue;
}

public void setHasTechnicalIssue(boolean hasTechnicalIssue) {
    this.hasTechnicalIssue = hasTechnicalIssue;
}
}

课堂教学{

private String issueId;
private String computerName;

public String getComputerName() {
    return computerName;
}

public void setComputerName(String computerName) {
    this.computerName = computerName;
}

public String getIssueId() {
    return issueId;
}

public void setIssueId(String issueId) {
    this.issueId = issueId;
}
}

计算机类{

List<Computer> getComputers() {
    List<Computer> clist = new ArrayList<Computer>();
    Computer c = new Computer();
    c.setComputerName("rahul");
    c.setHasTechnicalIssue(true);
    clist.add(c);
    Computer c1 = new Computer();
    c1.setComputerName("rahul1");
    c1.setHasTechnicalIssue(false);
    clist.add(c1);
    return clist;
}
List getComputers(){
List clist=new ArrayList();
计算机c=新计算机();
c、 setComputerName(“rahul”);
c、 Sethue和Sue(对);
添加(c);
计算机c1=新计算机();
c1.setComputerName(“rahul1”);
c1.SET(假);
clist.add(c1);
返回clist;
}
}

类发布AO{

List<TechnicalIssue> getTechnicalIssues() {
    List<TechnicalIssue> clist = new ArrayList<TechnicalIssue>();
    TechnicalIssue c = new TechnicalIssue();
    c.setComputerName("rahul");
    c.setIssueId("111");
    clist.add(c);
    TechnicalIssue c1 = new TechnicalIssue();
    c1.setComputerName("rahul1");
    c1.setIssueId("112");
    clist.add(c1);
    return clist;
}
List gettechnicalsues(){
List clist=new ArrayList();
技术化c=新技术化();
c、 setComputerName(“rahul”);
c、 setIssueId(“111”);
添加(c);
技术化c1=新技术化();
c1.setComputerName(“rahul1”);
c1.setIssueId(“112”);
clist.add(c1);
返回clist;
}
}

公开课{

ComputerDao computerDao;
IssueDao issueDao;

public void getComputers() {
    computerDao = new ComputerDao();
    issueDao = new IssueDao();
    List<Computer> computers = computerDao.getComputers();
    List<TechnicalIssue> technicalIssues = issueDao.getTechnicalIssues();
    for (Iterator<TechnicalIssue> it = technicalIssues.iterator(); it.hasNext();) {
        TechnicalIssue technicalIssue = it.next();
        for (Iterator<Computer> it1 = computers.iterator(); it1.hasNext();) {
            Computer computers1 = it1.next();
            if (technicalIssue.getComputerName().equals(computers1.getComputerName()) && computers1.isHasTechnicalIssue()) {
                System.out.println("Has techincal issue : " + computers1.getComputerName());
                //Print all other details
            }
        }
    }
}

public static void main(String[] args) {
    QuestionClass QC = new QuestionClass();
    QC.getComputers();
}
ComputerDao ComputerDao;
发出(IssueDao)发出(IssueDao);;
公用计算机(){
computerDao=新的computerDao();
issueDao=新的issueDao();
List computers=computerDao.getComputers();
List technicalisues=issueDao.gettechnicalisues();
for(Iterator it=technicalissaues.Iterator();it.hasNext();){
TechnicalIssue TechnicalIssue=it.next();
for(迭代器it1=computers.Iterator();it1.hasNext();){
计算机1=it1.next();
if(technicalisue.getComputerName().equals(computers1.getComputerName())和&computers1.ishasteechnicalisue()){
System.out.println(“有技术问题:+computers1.getComputerName());
//打印所有其他详细信息
}
}
}
}
公共静态void main(字符串[]args){
QuestionClass QC=新的QuestionClass();
QC.getComputers();
}
}

##输出##
有技术问题:rahul
生成成功(总时间:0秒)

}

课堂教学{

private String issueId;
private String computerName;

public String getComputerName() {
    return computerName;
}

public void setComputerName(String computerName) {
    this.computerName = computerName;
}

public String getIssueId() {
    return issueId;
}

public void setIssueId(String issueId) {
    this.issueId = issueId;
}
}

计算机类{

List<Computer> getComputers() {
    List<Computer> clist = new ArrayList<Computer>();
    Computer c = new Computer();
    c.setComputerName("rahul");
    c.setHasTechnicalIssue(true);
    clist.add(c);
    Computer c1 = new Computer();
    c1.setComputerName("rahul1");
    c1.setHasTechnicalIssue(false);
    clist.add(c1);
    return clist;
}
List getComputers(){
List clist=new ArrayList();
计算机c=新计算机();
c、 setComputerName(“rahul”);
c、 Sethue和Sue(对);
添加(c);
计算机c1=新计算机();
c1.setComputerName(“rahul1”);
c1.SET(假);
clist.add(c1);
返回clist;
}
}

类发布AO{

List<TechnicalIssue> getTechnicalIssues() {
    List<TechnicalIssue> clist = new ArrayList<TechnicalIssue>();
    TechnicalIssue c = new TechnicalIssue();
    c.setComputerName("rahul");
    c.setIssueId("111");
    clist.add(c);
    TechnicalIssue c1 = new TechnicalIssue();
    c1.setComputerName("rahul1");
    c1.setIssueId("112");
    clist.add(c1);
    return clist;
}
List gettechnicalsues(){
List clist=new ArrayList();
技术化c=新技术化();
c、 setComputerName(“rahul”);
c、 setIssueId(“111”);
添加(c);
技术化c1=新技术化();
c1.setComputerName(“rahul1”);
c1.setIssueId(“112”);
clist.add(c1);
返回clist;
}
}

公开课{

ComputerDao computerDao;
IssueDao issueDao;

public void getComputers() {
    computerDao = new ComputerDao();
    issueDao = new IssueDao();
    List<Computer> computers = computerDao.getComputers();
    List<TechnicalIssue> technicalIssues = issueDao.getTechnicalIssues();
    for (Iterator<TechnicalIssue> it = technicalIssues.iterator(); it.hasNext();) {
        TechnicalIssue technicalIssue = it.next();
        for (Iterator<Computer> it1 = computers.iterator(); it1.hasNext();) {
            Computer computers1 = it1.next();
            if (technicalIssue.getComputerName().equals(computers1.getComputerName()) && computers1.isHasTechnicalIssue()) {
                System.out.println("Has techincal issue : " + computers1.getComputerName());
                //Print all other details
            }
        }
    }
}

public static void main(String[] args) {
    QuestionClass QC = new QuestionClass();
    QC.getComputers();
}
ComputerDao ComputerDao;
发出(IssueDao)发出(IssueDao);;
公用计算机(){
computerDao=新的computerDao();
issueDao=新的issueDao();
List computers=computerDao.getComputers();
List technicalisues=issueDao.gettechnicalisues();
for(Iterator it=technicalissaues.Iterator();it.hasNext();){
TechnicalIssue TechnicalIssue=it.next();
for(迭代器it1=computers.Iterator();it1.hasNext();){
计算机1=it1.next();
if(technicalisue.getComputerName().equals(computers1.getComputerName())和&computers1.ishasteechnicalisue()){
System.out.println(“有技术问题:+computers1.getComputerName());
//打印所有其他详细信息
}
}
}
}
公共静态void main(字符串[]args){
QuestionClass QC=新的QuestionClass();
QC.getComputers();
}
}

##输出##
有技术问题:rahul

构建成功(总时间:0秒)

U特别要求提供包含番石榴的解决方案。我从来没用过番石榴。如果你不介意的话,我可以用一种简单的方式告诉你怎么做:

for (int i = 0; i < computers.size(); i++){ 
String cn = computers.get(i).getComputerName(); 
for (int j = 0; j < technicalIssues.size(); j++){ 
if (cn.contentEquals(technicalIssues.get(j).getComputerName())){ 
computers.get(i).setHasTechnicalIssue(true); 
break; 
} 
} 
}
for(inti=0;i
U特别要求提供一种包含番石榴的解决方案。我从来没用过番石榴。如果你不介意的话,我可以用一种简单的方式告诉你怎么做:

for (int i = 0; i < computers.size(); i++){ 
String cn = computers.get(i).getComputerName(); 
for (int j = 0; j < technicalIssues.size(); j++){ 
if (cn.contentEquals(technicalIssues.get(j).getComputerName())){ 
computers.get(i).setHasTechnicalIssue(true); 
break; 
} 
} 
}
for(inti=0;i
这听起来像是一种使用
fluentiable
anyMatch
操作符的方法。需要注意的关键事项:

  • 我们不在乎技术问题列表中的匹配发生在哪里,只要存在匹配
  • 我们需要遍历计算机中的所有元素