Reputation: 1517
I have TreeSet
containing customer objects. It contains 10 circle objects. I want to run two loops on the set so that I can compare each element with all the elements down below . Here is the example I can do with List
, but I am not getting how can I do it with Set
for (int i = 0; i < set.size(); i++) {
// Circle circle = set.get(i);
for (int j = i+1; i < set.size(); j++) {
// Circle circle = set.get(j);
}
}
I can do it with List<String> list=new ArrayList<>(set)
where i can achieve both sorting(through set) and run two loops on list but it will be extra memory .
What i want to achieve :-
for each circle in outer loop(sorted by radius in descending order), i want to calculate the area of each circle in inner loop if that circle is valid(please assume there is a property in circle object which tells whether its valid or not). If valid, i need to calculate the multiplication of circle area in outer and inner loop and find the max result
Upvotes: 1
Views: 219
Reputation: 1213
You can implement Comparator interface for the same. It will do sort by default based on your requirement.
Please find below code for the same.
import java.util.Comparator;
import java.util.TreeSet;
public class Main {
public static void main(String a[]){
//I am sorting list by name.
TreeSet<Empl> empls = new TreeSet<Empl>(new EmplNameComp());
empls.add(new Empl("Jitendra",3000));
empls.add(new Empl("Kumar",6000));
empls.add(new Empl("Balla",2000));
empls.add(new Empl("Puja",2400));
for(Empl e:empls){
System.out.println(e);
}
System.out.println("=====END======");
}
}
class EmplNameComp implements Comparator<Empl>{
@Override
public int compare(Empl e1, Empl e2) {
return e1.getName().compareTo(e2.getName());
}
}
class Empl{
private String name;
private int salary;
public Empl(String n, int s){
this.name = n;
this.salary = s;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String toString(){
return "Name: "+this.name+" AND Salary: "+this.salary;
}
}
Upvotes: -1
Reputation: 131456
Use a foreach :
for (Customer customer : set ) {
for (Customer otherCustomer : set ) {
// do your processing
}
}
You cannot retrieve an element from a Set
with the get(int index)
method that is specific to the List
interface.
Sorry, I don't make attention to your inner loop condition :
for (int j = i+1; i < set.size(); j++) {
You want indeed the inner loop starts after the current element of the external loop.
You could achieve that by declaring your set with the TreeSet
class and by using the tailSet()
method :
/**
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException if {@code fromElement} is null and
* this set uses natural ordering, or its comparator does
* not permit null elements
* @throws IllegalArgumentException {@inheritDoc}
* @since 1.6
*/
public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
return new TreeSet<>(m.tailMap(fromElement, inclusive));
}
So you could start the second loop after the current element of the external loop :
TreeSet<Customer> set = new TreeSet();
for (Customer customer : set) {
for (Customer otherCustomer : set.tailSet(customer, false) {
// do your processing
System.out.println("");
}
}
Upvotes: 6
Reputation: 726809
You can use tailSet
to get all items starting with the current one:
for (Customer outer : treeSet ) {
for (Customer inner : treeSet.tailSet(outer, false)) {
// ...
}
}
Note the second parameter to which you need to pass false
. It tells the set to exclude the outer
element.
Upvotes: 3