[Solved] How can i sort objects in arrayList? [closed]

Use Comparable and Comparator as shown below, you can also visit https://www.journaldev.com/780/comparable-and-comparator-in-java-example for further details. import java.util.Comparator; class Employee implements Comparable<Employee> { private int id; private String name; private int age; private long salary; public int getId() { return id; } public String getName() { return name; } public int getAge() { return age; } … Read more

[Solved] How do I return/implement the toString for the ArrayList? Also, just want to check that I’ve correctly created my objects?

You have a lot of error in your code : First Don’t ever name your variables with Upper letter in beginning, java use camelCase Second Don’t declare your variable with private public in your method. private ArrayList<Object> List = new ArrayList<Object>(); Third You can’t declare a method inside another method you have to close the … Read more

[Solved] Remove duplicates from a Java List [duplicate]

There are two possbile solutions. The first one is to override the equals method. Simply add: public class DataRecord { […..] private String TUN; @Override public boolean equals(Object o) { if (o instanceof DataRecord) { DataRecord that = (DataRecord) o; return Objects.equals(this.TUN, that.TUN); } return false; } @Override public int hashCode() { return Objects.hashCode(TUN); } … Read more

[Solved] how to get substring items with in arraylist in java

you have two approaches: ArrayList<String> ar = new ArrayList(); ar.add(“UserId”); //adding item to arraylist ar.add(“Directory”); ar.add(“Username”); ar.add(“PhoneNumber”); // approach one using iteration for (Iterator<String> it = ar.iterator(); it.hasNext(); ) { String f = it.next(); if (f.equals(“UserId”)) System.out.println(“UserId found”); } // approach two using a colletion which supports fetching element by key Map<String,String> myMap=new HashMap<>(); for(String … Read more

[Solved] ERROR java.lang.ArrayIndexOutOfBoundsException: length=5; index=5

Exception line (java.lang.ArrayIndexOutOfBoundsException: length=5; index=5) clearly mentions that you are Trying to get index=5but the length of the dato is 5(length=5). So, use only proper index i.e. index 0 to 4. OR Make sure that enough indexes exists to access. Note: You have used dato.split(“, “);. Try with dato.split(“,”);. May be the problem is with … Read more