I have a class A like this:
class A {
Long id;
String name;
Long parentId; // refers to another A object's id
}
Now I get the list of A objects, and I want to put them all to a data-structure like "folder tree" in PC, and then view that tree on GUI using JSP but I do not know how to implement this. So could you please help on these 2 problems:
1. How to build a "folder tree" from a given list of objects? Is there any available API support this?
2. How can we browse that whole data tree and view it on JSP as folder tree without using recursion? (I mean what is the best way to display them)
Thank you so much.
Based on your comments, I assume that you can change your A class to look like this:
class A {
Long id;
String name;
Long parentId; // refers to another A object's id
List<A> childrenNodes = new ArrayList();
}
Now, assuming you have a List<A> lstData filled with all the data and you want to convert it into a Tree, you can use the following code/algorithm:
public List<A> convertIntoTree(List<A> lstData) {
for(A parentA : lstData) {
//setting the children nodes for parentA
for(A childA : lstData) {
if (childA.getParentId() == parentA.getId()) {
parentA.getChildrenNodes().add(childA);
}
}
}
//the main tree
List<A> lstParents = new ArrayList<A>();
//filling the tree
for(A parentA : lstData) {
//change this for your check if parent function or another rule
if (parentA.getParentId() == 0) {
lstParents.add(parentA);
}
}
return lstParents;
}
Related
This is mainly a question intended for me to learn about various performant ways of filtering and assigning objects to Lists.
Assume
public class A implements Comparable<A> {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
#Override
public int compareTo(A o) {
return o.getId().compareTo(this.getId());
}
}
public class B implements Comparable<B>{
private String id;
private List<A> aList = new ArrayList<>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void addA(A a)
{
aList.add(a);
}
#Override
public int compareTo(B o) {
return o.getId().compareTo(this.getId());
}
}
public class Main {
public static void main(String[] args) {
SortedSet<A> aSet = new TreeSet<>();
SortedSet<B> bSet = new TreeSet<>();
for(int i=0;i<100000;i++)
{
UUID uuid = UUID.randomUUID();
String uuidAsString = uuid.toString();
A a1 = new A();
a1.setId(uuidAsString);
aSet.add(a1);
A a2 = new A();
a2.setId(uuidAsString);
aSet.add(a2);
B b = new B();
b.setId(uuidAsString);
bSet.add(b);
}
//this is where the performance part comes in
//scenario: For each B I want to find A whose Id matches B's Id, and assign it to B
//assume B can have 1-5 instances of A (even though for this example I only initialized 2)
bSet.parallelStream().forEach(b -> {
aSet.parallelStream().filter(a -> {
return b.getId().equals(a.getId());
}).forEach(a -> {
b.addA(a);
});
});
}
}
The solution I came up with was to combine parallelstreams and filters to find the matching IDs between the two types of objects and then to loops through the filtered results to add the instances of A to B.
I used TreeSets here because I thought the ordered IDs might help speed things up, same reason I used parallelStreams.
This is mostly abstracted out from a scenario from a project I am doing at the office which I cant post here. The classes in the actual project have a lot more variables, and in the worst case - have sublists of lists (I resolved that using flatMaps in streams).
However my inexperienced gut tells me there is a more performant way to solve this problem.
I am primarily looking for practical ways to speed this up.
Some ways I thought of speeding this up:
Switch the lists and sets to Eclipse Collections
Assuming the starting point of these classes are CSV files -> Maybe write an apache spark application that will map these(I assumed that Spark could have some internal clever way of doing this faster than Streams).
I dunno......write them all to sql tables....map them via foreign keys and then query them again?
Speed is the name of the game, solutions using vanilla java, different librarys (like Eclipse Collections), or entire engines like Spark are acceptable
Assume the minimum list size is atleast 50,000
Bonus complexity: You can add another class 'C', with multiple instances of 'B' in it. My inexperienced self can only think of writing another similar streaming operation as A->B and run it after the first stream is done. Is there a way to combine both A->B and B->C operations together so that they happen at once. That will definitely speed things up.
Sorry about my inexperienced self and sorry again if this is a duplicate too
In your code, you use b.addA(a); where b is an instance of B while B doesn't have a method addA(A). Is B supposed to keep a list of A's?
However, the answer to your question is hashing. You are looking for a multimap, to be specific. As a quick fix you can use a TreeMap that stores a List of A's by their id:
public static void main(String[] args) {
TreeMap<String, ArrayList<A>> aSet = new TreeMap<>();
ArrayList<B> bSet = new ArrayList<>();
for (int i = 0; i < 100000; i++) {
UUID uuid = UUID.randomUUID();
String uuidAsString = uuid.toString();
A a1 = new A();
a1.setId(uuidAsString);
ArrayList<A> list = aSet.get(a1.getId());
if (list == null) {
list = new ArrayList<>();
aSet.put(a1.getId(), list);
}
list.add(a1);
A a2 = new A();
a2.setId(uuidAsString);
list = aSet.get(a2.getId());
if (list == null) {
list = new ArrayList<>();
aSet.put(a2.getId(), list);
}
list.add(a2);
B b = new B();
b.setId(uuidAsString);
bSet.add(b);
}
for (B b : bSet) {
System.out.println(aSet.get(b.getId()));
}
}
Please note that this isn't a good implementation and instead you should write your own multimap or use the one in guava
My aim is to recreate the structure of XML in custom Objects to operate with it further. Actually, I want to have XML as input and produce LaTeX as output. For this task I have implemented principles of JAXB library. But don't think that this is a good idea, because it is not convenient to retain the needed structure of document as output in TeX.
Here is an example of my custom class:
public class Section {
private String title;
private List<Par> par;
private List<SubSec> subsec;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = "\\section {" + title + "}";
}
public List<Par> getPar() {
if (par == null) {
par = new ArrayList<Par>();
}
return this.par;
}
public List<SubSec> getSubSec() {
if (subsec == null) {
subsec = new ArrayList<SubSec> ();
}
return this.subsec;
}
}
So I have a list of Section class, which have titles, list of paragraphs (Par) and list of subsections (SubSec) (simplify LaTeX article structure). Paragraphs contain text, but subsection can include also list of paragraphs.
After XML input I transfer all Data from it in objects, instances of this Classes.
As example:
List<Section> listSections = new ArrayList<Section>();
// omitting the actions to recreate the structure and set values to Objects
// now, to retrieve and write:
for (int j = 0; j < listSections.size(); j++) {
List<Par> listParText = listSections.get(j).getPar();
writer.write(listSections.get(j).getTitle());
writer.newLine();
for (Par parList : listParText) {
if (parList.getText() != null) {
writer.write(parList.getText());
writer.newLine();
}
}
}
The problem is, that I can't recreate the structure of the document on the stage custom objects -> TeX. Although the structure is preserved on stage XML - custom objects. In Objects model I have, for example:
Section1(title): Par(text), Par(text), Par(text)
Section2(title): Subsection1(title): Par(text), Par(text), Par(text)
Subsection2(title): Par(text), Par(text)
Section3(title): Par(text)
Is there a way to save this order and get value in the same order to write them to file? Get values with getters and setters is NOT a problem to me, problem to retrieve them with proper order.
Update
To clarify the problem, lets suppose every Section contains paragraphs (Par), subsection (SubSec), Tables, Figures in certain order. But obviously Java not allow to make a list like: List<SubSec, Par, Table, Fig>. I can put information there in certain order, but not retrieve. Or can I?
Would it work to make a parent class, say DocumentComponent, of which SubSec, Par, Table, and Fig were all subclasses, and then say that a document is an ordered list of DocumentComponents?
I am looking to implement a sort feature for my address book application.
I want to sort an ArrayList<Contact> contactArray. Contact is a class which contains four fields: name, home number, mobile number and address. I want to sort on name.
How can I write a custom sort function to do this?
Here's a tutorial about ordering objects:
The Java Tutorials - Collections - Object Ordering
Although I will give some examples, I would recommend to read it anyway.
There are various way to sort an ArrayList. If you want to define a natural (default) ordering, then you need to let Contact implement Comparable. Assuming that you want to sort by default on name, then do (nullchecks omitted for simplicity):
public class Contact implements Comparable<Contact> {
private String name;
private String phone;
private Address address;
#Override
public int compareTo(Contact other) {
return name.compareTo(other.name);
}
// Add/generate getters/setters and other boilerplate.
}
so that you can just do
List<Contact> contacts = new ArrayList<Contact>();
// Fill it.
Collections.sort(contacts);
If you want to define an external controllable ordering (which overrides the natural ordering), then you need to create a Comparator:
List<Contact> contacts = new ArrayList<Contact>();
// Fill it.
// Now sort by address instead of name (default).
Collections.sort(contacts, new Comparator<Contact>() {
public int compare(Contact one, Contact other) {
return one.getAddress().compareTo(other.getAddress());
}
});
You can even define the Comparators in the Contact itself so that you can reuse them instead of recreating them everytime:
public class Contact {
private String name;
private String phone;
private Address address;
// ...
public static Comparator<Contact> COMPARE_BY_PHONE = new Comparator<Contact>() {
public int compare(Contact one, Contact other) {
return one.phone.compareTo(other.phone);
}
};
public static Comparator<Contact> COMPARE_BY_ADDRESS = new Comparator<Contact>() {
public int compare(Contact one, Contact other) {
return one.address.compareTo(other.address);
}
};
}
which can be used as follows:
List<Contact> contacts = new ArrayList<Contact>();
// Fill it.
// Sort by address.
Collections.sort(contacts, Contact.COMPARE_BY_ADDRESS);
// Sort later by phone.
Collections.sort(contacts, Contact.COMPARE_BY_PHONE);
And to cream the top off, you could consider to use a generic javabean comparator:
public class BeanComparator implements Comparator<Object> {
private String getter;
public BeanComparator(String field) {
this.getter = "get" + field.substring(0, 1).toUpperCase() + field.substring(1);
}
public int compare(Object o1, Object o2) {
try {
if (o1 != null && o2 != null) {
o1 = o1.getClass().getMethod(getter, new Class[0]).invoke(o1, new Object[0]);
o2 = o2.getClass().getMethod(getter, new Class[0]).invoke(o2, new Object[0]);
}
} catch (Exception e) {
// If this exception occurs, then it is usually a fault of the developer.
throw new RuntimeException("Cannot compare " + o1 + " with " + o2 + " on " + getter, e);
}
return (o1 == null) ? -1 : ((o2 == null) ? 1 : ((Comparable<Object>) o1).compareTo(o2));
}
}
which you can use as follows:
// Sort on "phone" field of the Contact bean.
Collections.sort(contacts, new BeanComparator("phone"));
(as you see in the code, possibly null fields are already covered to avoid NPE's during sort)
In addition to what was already posted by BalusC it may be worth pointing that since Java 8 we can shorten our code and write it like:
Collection.sort(yourList, Comparator.comparing(YourClass::getSomeComparableField));
or since List now have sort method also like
yourList.sort(Comparator.comparing(YourClass::getSomeComparableField));
Explanation:
Since Java 8, functional interfaces (interfaces with only one abstract method - they can have more default or static methods) can be easily implemented using:
lambdas arguments -> body
or method references source::method.
Since Comparator<T> has only one abstract method int compare(T o1, T o2) it is functional interface.
So instead of (example from #BalusC answer)
Collections.sort(contacts, new Comparator<Contact>() {
public int compare(Contact one, Contact other) {
return one.getAddress().compareTo(other.getAddress());
}
});
we can reduce this code to:
Collections.sort(contacts, (Contact one, Contact other) -> {
return one.getAddress().compareTo(other.getAddress());
});
We can simplify this (or any) lambda by skipping
argument types (Java will infer them based on method signature)
or {return ... }
So instead of
(Contact one, Contact other) -> {
return one.getAddress().compareTo(other.getAddress();
}
we can write
(one, other) -> one.getAddress().compareTo(other.getAddress())
Also now Comparator has static methods like comparing(FunctionToComparableValue) or comparing(FunctionToValue, ValueComparator) which we could use to easily create Comparators which should compare some specific values from objects.
In other words we can rewrite above code as
Collections.sort(contacts, Comparator.comparing(Contact::getAddress));
//assuming that Address implements Comparable (provides default order).
This page tells you all you need to know about sorting collections, such as ArrayList.
Basically you need to
make your Contact class implement the Comparable interface by
creating a method public int compareTo(Contact anotherContact) within it.
Once you do this, you can just call Collections.sort(myContactList);,
where myContactList is ArrayList<Contact> (or any other collection of Contact).
There's another way as well, involving creating a Comparator class, and you can read about that from the linked page as well.
Example:
public class Contact implements Comparable<Contact> {
....
//return -1 for less than, 0 for equals, and 1 for more than
public compareTo(Contact anotherContact) {
int result = 0;
result = getName().compareTo(anotherContact.getName());
if (result != 0)
{
return result;
}
result = getNunmber().compareTo(anotherContact.getNumber());
if (result != 0)
{
return result;
}
...
}
}
BalusC and bguiz have already given very complete answers on how to use Java's built-in Comparators.
I just want to add that google-collections has an Ordering class which is more "powerful" than the standard Comparators.
It might be worth checking out. You can do cool things such as compounding Orderings, reversing them, ordering depending on a function's result for your objects...
Here is a blog post that mentions some of its benefits.
You need make your Contact classes implement Comparable, and then implement the compareTo(Contact) method. That way, the Collections.sort will be able to sort them for you. Per the page I linked to, compareTo 'returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.'
For example, if you wanted to sort by name (A to Z), your class would look like this:
public class Contact implements Comparable<Contact> {
private String name;
// all the other attributes and methods
public compareTo(Contact other) {
return this.name.compareTo(other.name);
}
}
By using lambdaj you can sort a collection of your contacts (for example by their name) as it follows
sort(contacts, on(Contact.class).getName());
or by their address:
sort(contacts, on(Contacts.class).getAddress());
and so on. More in general, it offers a DSL to access and manipulate your collections in many ways, like filtering or grouping your contacts based on some conditions, aggregate some of their property values, etc.
Ok, I know this was answered a long time ago... but, here's some new info:
Say the Contact class in question already has a defined natural ordering via implementing Comparable, but you want to override that ordering, say by name. Here's the modern way to do it:
List<Contact> contacts = ...;
contacts.sort(Comparator.comparing(Contact::getName).reversed().thenComparing(Comparator.naturalOrder());
This way it will sort by name first (in reverse order), and then for name collisions it will fall back to the 'natural' ordering implemented by the Contact class itself.
The Collections.sort is a good sort implementation. If you don't have The comparable implemented for Contact, you will need to pass in a Comparator implementation
Of note:
The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance. The specified list must be modifiable, but need not be resizable. This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place.
The merge sort is probably better than most search algorithm you can do.
I did it by the following way.
number and name are two arraylist. I have to sort name .If any change happen to name arralist order then the number arraylist also change its order.
public void sortval(){
String tempname="",tempnum="";
if (name.size()>1) // check if the number of orders is larger than 1
{
for (int x=0; x<name.size(); x++) // bubble sort outer loop
{
for (int i=0; i < name.size()-x-1; i++) {
if (name.get(i).compareTo(name.get(i+1)) > 0)
{
tempname = name.get(i);
tempnum=number.get(i);
name.set(i,name.get(i+1) );
name.set(i+1, tempname);
number.set(i,number.get(i+1) );
number.set(i+1, tempnum);
}
}
}
}
}
use this method:
private ArrayList<myClass> sortList(ArrayList<myClass> list) {
if (list != null && list.size() > 1) {
Collections.sort(list, new Comparator<myClass>() {
public int compare(myClass o1, myClass o2) {
if (o1.getsortnumber() == o2.getsortnumber()) return 0;
return o1.getsortnumber() < o2.getsortnumber() ? 1 : -1;
}
});
}
return list;
}
`
and use: mySortedlist = sortList(myList);
No need to implement comparator in your class.
If you want inverse order swap 1 and -1
With java 8 feature
List<Contact> contact = contactArray.stream().sorted((c1, c2) -> ((c1.getName().compareTo(c2.getName())))).collect(Collectors.toList());
You shoud use the Arrays.sort function. The containing classes should implement Comparable.
I have class with variables:
public class Items {
public string name;
public string ID;
}
and I have second class when I have List of object of first class
public class MyClass {
public Items cheese;
public List <Items> listOfItems = new List ();
listOfItems.Add (cheese); // I know it should be in method or something but it's just an example
}
and I want get the name of the cheese, but using my list, because I have above 20 items in my list.
In Java I can do it like:
listOfItems.get(1).name; // 1 is an index
How can I do it in C#?
// Please don't try to improve my
listOfItems[0].name;
Just remember to start counting at 0 ;)
Other than that its works the same way as java, just slightly different syntax
The simple way:
listOfItems[1].name;
If you want a specific item try this:
var name = listOfItems.Find(x => x.ID == "1").name;
"1" is the example "Id" that you search.
Sorry for my english
I hope help you.
For example:
I got a table like this at UI side:
And this is the list that has all data showed above:
List<MyBean> myList;
Now I want to search by categoryName, languageID or categoryID using a method like this:
private List<MyBean> search(List<MyBean> myList, String columnName, String criteria)
So then I just do the following to get the results that matches my criteria:
1st case: List<Bean> results = this.search(myList, "categoryName", "Dog H");
2nd case: List<Bean> results = this.search(myList, "categoryName", "Dog House");
(At this point results list will return 3 elements in both cases -- according to the above table).
Is it possible to achieve this? As you guys can see, this is a kind of search similar to the %LIKE% function from SQL but focused on java.util.List
Thanks in advance.
I solved my question using the following:
Hamcrest (hamcrest-all-1.3.jar) -- http://hamcrest.googlecode.com/files/hamcrest-all-1.3.jar
Lambdaj (lambdaj-2.4-with-dependencies.jar) -- http://lambdaj.googlecode.com/files/lambdaj-2.4-with-dependencies.jar
Then I just imported the following namespace by this way:
import static ch.lambdaj.Lambda.*;
And get results from my criteria using this:
List<MyBean> results = select(myList, having(on(MyBean.class).getCategoryName(), org.hamcrest.Matchers.containsString("Dog H")));
So, in that line of code I am passing myList, the "column" and criteria as I required in my question.
The rest of code at that line is easy to understand so I won't write about it.
Hope this helps someone else too.
Here's two approaches
The "forceful" approach ;)
public List<MyBean> search(List<MyBean> lstBeans, String method, String regExp) {
List<MyBean> lstMatch = new ArrayList<>(lstBeans.size());
Pattern pattern = Pattern.compile(regExp);
for (MyBean bean : lstBeans) {
if (method.equals("categoryName")) {
String name = bean.getCategoryName();
if (pattern.matcher(name).matches()) {
lstMatch.add(bean);
}
}
}
return lstMatch;
}
.
.
.
List<MyBean> matches = search(lstBeans, "categoryName", "^Dog.*$");
Basically, this relies on knowing that a given method name will return a given result, this would make it difficult to filter results on category, for example...
There are also problems with people misspelling the method name or not taking into account that it's case sensitive...
OR you could try something a little more variable
public interface Criteria<T> {
public boolean matches(T bean);
}
public class CategoryNameCriteria implements Criteria<MyBean> {
private Pattern pattern;
public CategoryCriteria(String criteria) {
pattern = Pattern.compile(criteria);
}
#Override
public boolean matches(MyBean bean) {
return pattern.matcher(bean.getCategoryName()).matches();
}
}
public <T> List<T> search(List<T> lstBeans, Criteria<T> critera) {
List<T> lstMatch = new ArrayList<>(lstBeans.size());
for (T bean : lstBeans) {
if (critera.matches(bean)) {
lstMatch.add(bean);
}
}
return lstMatch;
}
.
.
.
matches = search(lstBeans, new CategoryNameCriteria("^Dog.*$"));
This would allow you to "define" your own criteria and matching requirements without changing the basic method
This would mean you could to develop up a series of criteria independent of the search method, such as
public class CategoryIDCriteria implements Criteria<MyBean> {
private int id;
public CategoryIDCriteria(int id) {
this.id = id;
}
#Override
public boolean matches(MyBean bean) {
return bean.getCategoryID() == id;
}
}
.
.
.
matches = search(lstBeans, new CategoryIDCriteria(1));
And because the criteria and search method are generic, it would be possible to reuse it with different types of objects
If you are willing to use third party libraries, perhaps JoSQL will suffice? It allows you to filter collections of POJOs using SQL queries...
Also, if you are willing to move away from SQL queries, this old post may be of interest: How do you query object collections in Java (Criteria/SQL-like)?
Instead of a List, why not use Guava's Table?
Table<Integer, Integer, String> table = HashBasedTable.create();
table.put(1, 1, "Advantage Flea");
table.put(1, 2, "Advantage Flea");
table.put(1, 3, "Advantage Flea");
.
.
.
Case #1 requires something like:
Collection<String> values = table.values();
for(String value : values) {
if(value.contains(queryString) {
//found it!
}
}
Case #2 is trivial, and just looks like:
if(table.containsValue("Advantage Flea") {
...
}