Copying the contents of a file into an array of a linked list and sorting it

I have a comma-delimited file containing Employee Name, Company, years.

An employee can be associated with several companies.

For instance,

John, Google, 2
John, Microsoft, 1
James, Tesla, 1
James, Apple 5

I got information using a java scanner

scanner.useDelimiter(",|\\n"); while (scanner.hasNext()) { String line = scanner.next() 

I am new to Java and I am trying to insert the above in sorted order (using experience as sorting criteria) using an array of linked lists or an array of arrays. So

employee → Company1 → Company2 .... (by order of the employee)

So, in the example above, this would be:

John-> Microsoft-> ​​Google
James-> Tesla-> Apple

Can someone point me in the right direction?

NOTE. If the experience is the same, it does not matter which company will be the first.

+4
source share
3 answers

Use this class for Person.

public class Person {

 @Getter @Setter private String name; @Getter @Setter private TreeMap<String, String> companyExperience; public Person(){ companyExperience = new TreeMap<String, String>(); } 

}

Using experience as a key in TreeMap automatically sorts companies for the Person in ascending order.

Your main shoud class is as follows

 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(); } } } 

I tested it and looked pretty, pretty good for me.

By the way, the annotations @Getter and @Setter are taken from the Lombok project. You can either use it or create your own getters / setters.

+1
source

read your file with readLine() and use split to get every field of your data, for example:

 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]))); } 

Then you can save your data in an ArrayList , which takes its own class, for example. Person , which stores information about a person and implements Comparable , where you execute sorting logic.

If you need to group your data by person’s name, you might consider having a Hashtable , where the key is the person’s name and the value is an ArrayList experience.

You can define a class for your data, for example.

 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()); } } 

To sort your list, just call Collections.sort(list); ; however, this list will contain all the data, so change the code to group the data by employee name and specify a list for each employee.

0
source

For your purposes, it sounds as if you really want the object to represent a person who has some experience. Since your input source has denormalized data, the easiest way to do this is to populate Map<String,Person> when parsing the file:

 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()); 

After this process, you will receive a list of people who do not have a special order. For each Person, since you want to keep your work in the order ordered by experience, you need to implement Person.addJob in such a way that it is ordered. A SortedSet is a really good way to do this, but you cannot insert duplicates, and since you want to sort by experience and the person could be at two workstations as much time as you need to use an alternative approach. There are several ways to do this, but without assuming your data, I would suggest saving sorted List from Job objects:

 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); } } 

and finally, a Job should implement Comparable<Job> so that you can view it:

 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); } } 

This cheat bit in Person.addJob will support a List always sorted by 'natural order' Job. (see Collections.binarySearch ).

0
source

All Articles