首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将文件内容复制到链接列表的数组中并对其进行排序

将文件内容复制到链接列表的数组中并对其进行排序
EN

Stack Overflow用户
提问于 2013-02-13 01:04:51
回答 3查看 1K关注 0票数 4

我有一个逗号分隔的文件,其中包含员工姓名、公司、年数。

一名雇员可能与多家公司有关联。

比如,

约翰,谷歌,2 约翰,微软,1 詹姆斯,特斯拉,1岁 詹姆斯,苹果,5岁

我已经使用java扫描器检索了这些信息。

代码语言:javascript
运行
复制
scanner.useDelimiter(",|\\n");
    while (scanner.hasNext()) {
        String line = scanner.next()

我是Java新手,我试图使用链接列表或数组数组按排序顺序(使用经验作为排序标准)插入上述内容。所以

雇员-> Company1 -> Company2。(按雇员经验而定)

因此,在上面的例子中,应该是:

约翰->微软->谷歌 詹姆斯->特斯拉->苹果

有人能给我指明正确的方向吗?

注:如果经验相同,那么哪个公司是第一位并不重要。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-02-13 10:58:45

为个人使用此类

公共阶层人士{

代码语言:javascript
运行
复制
@Getter @Setter
private String name;

@Getter @Setter
private TreeMap<String, String> companyExperience;

public Person(){
    companyExperience = new TreeMap<String, String>();
}

}

使用经验作为TreeMap中的键,将自动按升序对公司进行排序。

你的主课应该是这样的

代码语言:javascript
运行
复制
public class App 
{
    public static void main( String[] args )
    {
        HashMap<String, Person> persons = new HashMap<String, Person>();

        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("C:\\Users\\Public Administrator\\test.txt"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String line = null;

        try {
            while ((line = br.readLine()) != null) {
                String[] fields = line.split(",");
                String personName = fields[0];
                Person existingPerson = persons.get(personName);
                if (existingPerson==null){
                    Person newPerson = new Person();
                    newPerson.setName(personName);
                    newPerson.getCompanyExperience().put(Integer.parseInt(fields[2])+fields[1], fields[1]);
                    persons.put(personName, newPerson);
                } else{
                    existingPerson.getCompanyExperience().put(Integer.parseInt(fields[2])+fields[1], fields[1]);
                }
             }
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }    

        //output
        Iterator<Map.Entry<String, Person>> entries = persons.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Person> entry = entries.next();
            Person _person = entry.getValue();
            System.out.print(_person.getName());

            Iterator<Map.Entry<String, String>> companyExperiences = _person.getCompanyExperience().entrySet().iterator();
            while (companyExperiences.hasNext()) {
                Map.Entry<String, String> companyExperience = companyExperiences.next();

                System.out.print(" > "+companyExperience.getValue());
            }
            System.out.println();

        }
    }
}

我已经测试过了,看起来很漂亮,对我来说很不错。

顺便说一下,@Getter和@Setter注释来自Lombok项目。您可以使用它,也可以创建自己的getter/setter。

票数 1
EN

Stack Overflow用户

发布于 2013-02-13 01:14:44

使用readLine()读取您的文件,并使用split获取数据的每个字段,例如:

代码语言:javascript
运行
复制
BufferedReader br = new BufferedReader(new FileReader("FileName"));
String line = null;
ArrayList<Person> list = new ArrayList<Person>();

while ((line = br.readLine()) != null) {
    String[] fields = line.split(",");
    list.add(new Person(fields[0], fields[1], Integer.parseInt(fields[2])));
 } 

然后,您可以将数据保存在接受自定义类的ArrayList中,例如存储个人信息的Person,并在执行排序逻辑的地方实现Comparable

如果您需要根据人名对数据进行分组,您可以考虑使用一个Hashtable,其中键是人名,值是经验的ArrayList

您可以为您的数据定义一个类。

代码语言:javascript
运行
复制
class Person implements Comparable<Person> {
    private String name;
    private String company;
    private int experience;

    public Person(String name, String company, int experience) {

        this.name = name;
        this.company = company;
        this.experience = experience;
    }

    public int getExperience() {
        return experience;
    }

    @Override
    public int compareTo(Person person) {
        return new Integer(experience).compareTo(person.getExperience());
    }
}

要对列表进行排序,只需调用Collections.sort(list);;但是,此列表将包含所有数据,因此修改代码,将数据按员工名称分组,并为每个员工设置一个列表。

票数 0
EN

Stack Overflow用户

发布于 2013-02-13 03:43:03

为了你的目的,听起来你真的想要一个对象来代表一个人,他有一定的经验。由于您的输入源具有非规范化的数据,所以最简单的方法是在解析文件时填充Map<String,Person>

代码语言:javascript
运行
复制
scanner.useDelimiter(",|\\n");
while (scanner.hasNext()) {
    String line = scanner.next();
    String[] fields = line.split(",");

    String name = fields[0];
    Person person = map.get(name);
    if (person == null) {
        person = new Person(name);
        map.put(name, person);
    }
    person.addJob(fields[1], Integer.parseInt(fields[2]));
}

List<Person> people = new ArrayList<Person>(map.values());

在这个过程之后,您将得到一个人员列表,没有特定的顺序。对于每个人来说,由于您希望按照经验排序他们的工作,所以您需要实现Person.addJob,以便保持其有序性。SortedSet确实是一种很好的方法,但是您不能插入副本,而且由于您希望根据经验进行排序,而且一个人在两份工作中的时间与使用替代方法所需的时间相同。有几种方法可以做到这一点,但如果不对数据进行假设,我建议保留一个排序的List of Job对象:

代码语言:javascript
运行
复制
class Person {
    private final List<Job> jobs = new LinkedList<Job>();

    // Constructor, etc... 

    public void addJob(String companyName, int yearsOfExperience) {
        Job newJob = new Job(companyName, yearsOfExperience);
        int insertionIndex = Collections.binarySearch(jobs, newJob);
        if (insertionIndex < 0) {
            insertionIndex = (-(insertionIndex) - 1);
        }
        jobs.add(insertionIndex, newJob);
    }
}

最后,Job应该实现Comparable<Job>,这样您就可以查找它:

代码语言:javascript
运行
复制
class Job implements Comparable<Job> {
    private final String companyName;
    private final int yearsOfExperience;

    // Constructor, etc... 

    public int compareTo(Job otherJob) {
        return Integer.compare(yearsOfExperience,otherJob.yearsOfExperience);
    }
}

Person.addJob中的一些诡计将使List始终按作业的“自然秩序”排序。(见Collections.binarySearch)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14844787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档