Note that there are some explanatory texts on larger screens.

plurals
  1. POjava: Sorting objects in collection
    primarykey
    data
    text
    <p>I wrote a code like this which creates 150 employee objects from text files(150 text files) in a folder and stores it in a collection.</p> <p>Those text files contains id,name and age of employess.</p> <p>My problem is i want to sort id,name and age of those 150 employees..how should i write it..should i implement comparator or comparable interface? and to implement it. Please guide me</p> <p>The code is below:</p> <pre><code>package com.fulcrum.emp; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; public class TestingColections { public static void main(String[] args) { File folder = new File("D:\\employee files"); File[] listOfFiles = folder.listFiles(); ArrayList&lt;Employee&gt; emp= new ArrayList&lt;Employee&gt;();; int id = 0; String name = null; int age = 0; for (File file : listOfFiles) { try { Scanner scanner = new Scanner(file); String tokens = ""; String[] newtokens = null; while (scanner.hasNext()) { tokens = tokens.concat(scanner.nextLine()).concat(" "); tokens = tokens.replace("=", "|"); newtokens = tokens.split("[|\\s]"); } id = Integer.parseInt(newtokens[1]); name = (newtokens[3] + " " + newtokens[4]); age = Integer.parseInt(newtokens[6]); emp.add(new Employee(id, name, age)); } catch (FileNotFoundException e) { e.printStackTrace(); } } for(int i=0;i&lt;emp.size();i++) { System.out.println(emp.get(i)); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload