BucketSort in Java [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to get my head round the bucketsorting algorithm, but failed to do so.
Looked at numerous examples... but can't get it working...
Let's say I have this:
public class Employee {
int id; /// example: 52015
String firstname,lastname;
String department;
}
I have a huge list of employees, I then strip the list of all employees to sublists for each department. And the goal is to bucketsort these lists, on employee id. So I have my arraylists of employees, ready to pass on. I just can't seem to understand it.
THANK YOU!

Instead of bucket-sorting use Comparable<Employee> interface.
public class Employee implement Comparable<Employee> {
int id; /// example: 52015
String firstname,lastname;
String department;
public int compareTo(Employee compareEmployee) {
return this.id - compareEmployee.getID();
}
Anyway U can read this article to understand this mechanism better.

Related

How can I change a method to pass a proper object property? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a method:
public void dealDmg(int hp) {
this.hp = hp;
this.hp -= this.dmg;
}
But when i use it as dmg_dealer.dealDmg(dmg_receiver.hp);, it doesn't work properly as in the dmg_receiver's hp is not decreased, but the dmg_dealer's hp becomes of the same amount as of the dmg_receiver, and then dmg_dealer's hp gets reduced by the dmg amount. How can I change a body of the method so that it works in such a way?
You need to pass in the dmg_receiver object to the method instead of dmg_receiver.hp, that way you can update hp in dmg_receiver as well. The updated method should look like below:
public void dealDmg(DmgReceiver dmg_receiver) {
this.hp = dmg_receiver.hp;
dmg_receiver.hp -= this.dmg;
}
If the object represented by this is dealing damage to another object, the dealDmg needs to the object being dealt damage to (assuming that dmg is a member):
public void dealDmg(Unit other) {
other.hp -= this.dmg;
}

How to call method recursively with java? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
There is java bean with list of elements and discount. I have to apply some calculate logic in those elements using recursion until that discount value gets zero.
Current implementation
for(CustomClass custom : customList) {
Pair<CustomClass, Integer> returnVal = myMethod(custom, discount);
}
private Pair<CustomClass, Integer> myMethod(CustomClass custom, Integer discount) {
pair.getKey().add(custom.setAmount(custom.getAmount - discount));
pair.getValue().add(discount - custom.getAmount);
return pair;
}
I have to do something like above and have to reuse the discount in multiple elements in the customList until it is zero. I want to make it recursive so that I get the discounted value in the CustomClass and discount is reduced.
returnVal has updated discount but it is not being used in next iteration.
Could someone give me good approach to solve this issue.?
To make recursive call, you need to call same method inside
private Pair<CustomClass, Integer> myMethod(CustomClass custom, Integer discount) {
int dis = discount - custom.getAmount;
pair.getKey().add(custom.setAmount(custom.getAmount - discount))
pair.getValue().add(dis);
if (dis <= 0)
{
return pair;
}
return myMethod (custom, dis);
}

Looping through Object of Objects in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have an object which in turn contains other objects. Now I have to iterate through this main object and then pick each object and then iterate through them to find out whether any empty fields are present in them. If the object itself is empty, I have to cut it out of main object. Any thoughts on this please.
public class Transactions {
private Integer totalTransactionCount = null;
private List<Transaction> transactionsList = new ArrayList<Transaction>();
}
public class Transaction {
private String amount = null;
private Foreign foreign = null;
}
public class Foreign {
private String amount = null;
private String commissionAmount = null;
private String exchangeRate = null;
}
Now I have a Transaction object with me and I have to loop throught each of its fields and in turn loop through their fields to find out any null/empty fields.
pseudo code for looping through a list of lists:
for each (innerList in outerList) do
if(innerlist.size == 0) then
//Code for removing empty inner lists.
else
for each ( object in innerList) do
//Check if objects are empty as well and remove it
end for
end if
end for
EDIT: Pointing out lack of research.
I would like to point out that you haven't really done your research properly, simply by googling iterate list of object as well as iterate list of list of object I got plenty of solutions.
Not to mention a question already asked here on Stack Overflow, please read the first answer of this post

The best data structure and sorting algorithm to use for this particular situation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I'm writing a tiny java application that will read a person's details from a txt file(tab delimited)(id, First Name, Last Name, DoB, Address)
The txt file is already sorted by id and contains around 30,000 records. My program should provide the user the choice to sort either by First Name, Last Name or Address.
What i've done already is that I made a class Person and made an arraylist of type Person to store all the records and then used JAVA's collection sort to sort it by using different comparators like this:
public static Comparator<Person> addComp = new Comparator<Person>(){
public int compare(Person P1,Person P2){
String add1 = P1.getadd().toUpperCase();
String add2 = P2.getadd().toUpperCase();
return add1.compareTo(add2);
}
};
It works and all but what I wanted to know was if there was a faster algorithm to do it like in terms of running time and complexity.
Like if there was any other data structure that would be more suitable like a hash table or something
I think this is the best way to go. Using hashtables won't help here. In terms of asymptotic time complexity, you can't get any faster in general.
However you can improve user experience by pre-ordering the data after loading from file, so you only have to change a list reference on user interaction:
private List<Person> persons_by_id;
private List<Person> persons_by_name;
private List<Person> persons_by_address;
// ...
persons_by_id = readPersonsFromFile();
persons_by_name = new ArrayList<Person>(persons_by_id);
persons_by_address = new ArrayList<Person>(persons_by_id);
Collections.sort(persons_by_name, Person.ORDER_BY_NAME);
Collections.sort(persons_by_address, Person.ORDER_BY_ADDRESS);
You can make your comparators shorter with java8:
class Person {
// ...
public static final Comparator<Person> ORDER_BY_NAME =
Comparator.comparing((Person p) -> p.getName().toUpperCase());
public static final Comparator<Person> ORDER_BY_ADDRESS =
Comparator.comparing((Person p) -> p.getAddress().toUpperCase());
}

Adding item to the quizQuestion ArrayList [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hey guys so I have the method
public void addQuestion(Question question)
{
}
I have created an ArrayList in the class section
private ArrayList<Question> quizQuestion;
and I have initialised the ArrayList in the constructor by typing
quizQuestion = new ArrayList<Question>();
What do I type in the method above so I can add questions to the Question ArrayList. :)
public void addQuestion(Question question)
{
quizQuestion.add(question);
}
public void addQuestion(Question question)
{
quizQuestion.add(question);
}
Not sure, what you really want :)
but if you just want to add parameter Question to arraylist quizQuestion
quizQuestion.add(question);
I hope this helps...
Keep Leanrning :)

Categories