Java easyreport Import Excel, TXT data association Collection place Object (iii)

Source: Internet
Author: User
Tags class generator

In the next section, get the student associated teacher collection with only one, how to place multiple.

Custom Studentmatcher Implementation Matchabled method

Package matcher;


Import java.util.List;

Import Java.util.Map;


Import model. Student;


Import com.easyReport.read.MatchAbled;


public class Studentmatcher implements matchabled<student>{


@Override

public boolean Matchrowbean (Student Student, list<student> students) {

if (!students.contains (student)) {

return true;

}

Student stu = Students.get (Students.indexof (Student));

Stu.getteachers (). AddAll (Student.getteachers ());

return false;

}


@Override

public boolean Matchrowmap (map<string, object> Studentmap,

List<map<string, object>> studentmaps) {

return true;

}


}

Student class generator Hashcode and Equals method

@Override

public int hashcode () {

final int prime = 31;

int result = 1;

result = Prime * result + age;

result = Prime * result + ((name = = null)? 0:name.hashcode ());

return result;

}

@Override

public boolean equals (Object obj) {

if (this = = obj)

return true;

if (obj = = null)

return false;

if (GetClass ()! = Obj.getclass ())

return false;

Student other = (Student) obj;

if (age! = other.age)

return false;

if (name = = null) {

if (other.name! = null)

return false;

} else if (!name.equals (other.name))

return false;

return true;

}

Import data

Name Age Subjects Scores Name of teacher Teacher Sex
Bachelor 12 language Zhang Sanfeng
Bachelor 12 language Wang Chongyang
Bachelor 12 language
Lee Lao six 15 language
Li Lao Six 15 Chinese 76 Zhang Mowgli Man
Basictest class

Implementation method

Matcher Matching Device

Protected matchabled<t> Refmatcher () {

return null;

}

Abstractexceltest class Constructs reader modification

Protected Convertreader Buildreader () {

return new Excelreader (New Readercontext (Bulidreadtemp ()), Refmatcher ());

}

Excel Import Demo testexcelmatchable

package example;


Import Java.io.File;

Import Java.io.FileInputStream;

Import java.util.List;

Import Java.util.Map;


Import Matcher. Studentmatcher;

Import model. Student;

Import model. Teacher;

Import Test. Abstractexceltest;


Import com.easyReport.read.MatchAbled;

Import com.easyReport.read.temp.PropertyTemp;

Import com.easyReport.read.temp.ReadTemp;


Import Formater. Sexformater;


public class Testexcelmatchable extends Abstractexceltest<student> {

@Override

public void Test () {

FileInputStream FileInputStream;

try {

FileInputStream = new FileInputStream (New File ("Testfile/test3.xls"));

Testreadbean (FileInputStream);

Testreadmap (FileInputStream);

Fileinputstream.close ();

} catch (Exception e) {

E.printstacktrace ();

}

}

Overriding methods to achieve student matching

@Override

Protected matchabled<student> Refmatcher () {

return new Studentmatcher ();

}


@Override

Protected Readtemp bulidreadtemp () {

Readtemp readtemp = new Readtemp (1);

Readtemp.addpropertytemp (New Propertytemp ("name", "name"));

Readtemp.addpropertytemp (New Propertytemp ("Age", "ages");

Readtemp.addpropertytemp (New Propertytemp ("Account", "Course.name"));

Readtemp.addpropertytemp (New Propertytemp ("Score", "Course.score"));

Readtemp.addpropertytemp (New Propertytemp ("Teacher's name", "Teachers.name"));

Readtemp.addpropertytemp (New Propertytemp ("Teacher Gender", "Teachers.sex", New Sexformater ()));

return readtemp;

}

Iterate and output teacher information

@Override

protected void Writebeanlist (list<student> modellists) {

System.out.println ("--------------bean---------------");

for (Student s:modellists) {

System.out.println (S.getname () + "|"

+ s.getage () +s.getcourse (). GetName () + "|" +s.getcourse (). Getscore ());

System.out.println ("---------Teacher-------------------");

For (Teacher t:s.getteachers ()) {

System.out.println (t);

}

}

}


@Override

protected void Writemaplist (list<map<string, object>> maplists) {

SYSTEM.OUT.PRINTLN ("--------------map---------------");

For (map<string, object> m:maplists) {

System.out.println (M.get ("name") + "|" + m.get ("age") + "|" + M.get ("course.name") + "|" + M.get ("Course.score") +

"|" +m.get ("teachers.name") + "|" +m.get ("Teachers.sex"));

}

}


}

Operation Result:

--------------Bean---------------

Bachelor |12 Chinese |80.0|

---------Teacher-------------------

Zhang Sanfeng | M

Wang Chongyang | M

Hua Tuo | M

Li Lao six |15 Chinese |76.0|

---------Teacher-------------------

Hua Tuo | M

Zhang Mowgli | M



The Matchable interface is used to match the object, which returns true if the match succeeds, the object is joined to the collection, and the false match fails, and the object cannot be added to the collection.

This article is from "Rookie Bobo" blog, please be sure to keep this source http://zhangxiao2.blog.51cto.com/6342789/1586224

Java easyreport Import Excel, TXT data association Collection place Object (iii)

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.