Change swt dropdown list element order - java

I am new to SWT, and there is a project which demand me to change the order of element in a drop down list, the list contains data stocking in the database which is ordered by the order alphabet, while I would like to change the order of elements in the drop down list,can somebody tell me how the dropdown list obtain the data list from the database and how to change the order? Thank you very much.
Here comes the code of creating an object, and I would like to know how to change the order of the list. Thank you.
public final Object[] getElements(final Object inputElement) {
if (inputElement != null) {
if (inputElement instanceof Test) {
return getTest((EngdynoRequest) engDynoRequest).toArray();
}
}
return null;
}

make your own list of objects, after that you can order it in any way which you need, after that you can make your dropdown list with ordered elements.

If you have correctly located the method used for populating the combobox you can just sort the list of objects here.
return getTest((EngdynoRequest) engDynoRequest).toArray();
can be replaced with
Object[] unsorted = getTest((EngdynoRequest) engDynoRequest).toArray();
return Arrays.sort(unsorted);
If you want custom sorting order you can pass your own comparator via
Arrays.sort

There are couple of methods on ComboViewer that you may want to look at
StructuredViewer.java
public void setComparer(IElementComparer comparer)
public void setComparator(ViewerComparator comparator)
public void setSorter(ViewerSorter sorter)

This might make it easier to understand:
// Re-sort
String[] items = combo.getItems();
Arrays.sort(items);
combo.setItems(items);

Related

How to sort the dropdown list before displaying in dropdown box

The below image will show the code for that drop down box. It will return a list of object, after that I'm creating new SelectItem object. Is there any way to sort the list of object before I'm creating new SelectItem object.
Sorry, I'm not able to send the entire code, due to confidential
Use Collections.sort(..). If your want to sort particular field of your object, then you need to implements your own Comparator. See sample
Collections.sort(yourList,new Comparator<Object>() {
public int compare(Object obj1,Object obj2) {
return ((YourObject) obj1).getName().compareTo(((YourObject) obj2).getName);
}
});
Or you can simply uses as default sorting
Collections.sort(list, Collections.sort(list);

Assigning List with preserving reference

Suppose I have two Lists and I want to copy/assign all of one list to another list with preserving reference to the original list. I use this code
List<String> mylist = new List<String>();
List<String> another = getSomeList();
// I have to do
mylist.clear();
mylist.addAll(another);
This works fine, but my question is, is there any better way to do this?
Thanks
I don't think that there is an easier way. You can just implement your own list that has e.g. a setAll() method.
class MyArrayList<E> extends ArrayList<E> {
public void setAll(Collection<E> collection) {
clear();
addAll(collection);
}
}
But this only moves the the clear() and addAll() invokation into another method. Sure from a clients prespective it makes the call easier
MyArrayList<String> mylist = new MyArrayList<String>();
mylist.setAll(another);
but at the price that you use a special list implementation. Maybe you only use this implementation inside of a class and your api does not expose that you use a MyArrayList. Than it might be ok. I would just do it the way you already do.
is there any better way to do this?
No, not for your specifications. You can delete mylist = another;.
And there's no need to call mylist.clear() when you just assigned mylist = new List<String>();
So really all you need is that last line where you addAll(another).
You can loop through first and add all elements into another:
for(String s : mylist){
another.add(s);
}

How to remove duplicates of ArrayList<ArrayList<String>> and sort array

Lets say I have an array of an array of strings:
ArrayList<ArrayList<String>> arrayOfArray= new ArrayList<ArrayList<String>>();
Maybe it could look something like this(eg.):
arrayOfArray = [[A,1,B,2],[C,1,D,2],[C,1,D,2],[A,1,B,2]]
In the end I want this(duplicates was removed):
arrayOfArrayNoDuplicates = [[C,1,D,2],[A,1,B,2]]
Then as a final step I want this array of array to be sorted on the first item in the array.
Looks like this array of array maybe was sorted on the A or the B.
arrayOfArraySorted = [[A,1,B,2],[C,1,D,2]]
Is this possible without also sorting the inner array? I want to keep the order within the array "A,1,B,2".
I hope you understand want I want to do:)
/M
You use a set
It functions like an array, it dedupes
You can use a Set with a Comparator fot that.
Set<List<String>> list = new TreeSet<ArrayList<String>>(
new Comparator<List<String>>() {
public int compare(List<String> left, List<String> right) {
//check here if equal, before or after...
}
}
);
You should really use a HashSet or similar to remove duplicates from your collection of data (sets can only contain unique elements). I'm unsure how effective this will be against ArrayList's of varying contents though, so you might be best off extending ArrayList<String> and implementing your own bool equals(Object a) method (for your inner arrays).
To then sort your collection you should using Collections.sort(), you can then pass this a custom Comparator to sort them by whichever order you please. (A comparator lets you provide a method that takes 2 objects and rates their order.) Or if you've extended ArrayList<String> simply add the compare method to your class and add implements Comparator.
An example Comparator would be;
import java.util.*;
class ALComparator implements Comparator<ArrayList<String>>
{
#Override
public int compare(ArrayList<String> a, ArrayList<String> b)
{
if(a.size()==b.size())
return 1;
if(a.size()==0)
return -1;
if(b.size()==0)
return 1;
return a.get(0).compareTo(b.get(0));
}
}

<List<String[]>> issue

Here is my situation:
I have a method which sorts a file based on keywords and save the result into almost 21 List<String[]> variables.
List<String[]> sortKeyword(List<String[]> csvList, String[] keywords)
So call this function, I get 21 List<String[]> variables, but I don't know how to return the these lists.
After receive the return result, I also need to do for loop to update Database.
Thus I have two questions:
1. How to return the lists.
2. How to sort the return lists.
Could someone give me some advice. I really appreciate it.
Thank you
You can't return more than one object in Java but that returned object can be a collection of objects.
For your purpose you can add all those lists to a new List and return the new List.
You can return a list of lists.
List<List<String[]>> ll = new ArrayList<>();
ll.add(list1);
ll.add(list2);
return ll;
To return the three lists, either you create a wrapper class to add all the three lists to it or simply create a list of lists.
Another problem in your code, :
Use equals for string comparisoon:
if(kWord =="kWord1")
should be replaced to
if(kWord.equals("kWord1"))
Change the return type from List<String[]> to Map<String, ArrayList<String[]>>. The map String key will store the keyword and the ArrayList value will store the result line.
If I understand your problem, you're doing the above in a function and want to get the function to return all three lists, list1, list2, and list3.
In this case, you could return an array of three lists, or an ArrayList of three lists. In general, if I want a function that returns multiple values (that may not all be the same type), I usually just create a small class to contain them:
private static class KeywordSearchResults {
List<String[]> list1;
List<String[]> list2;
List<String[]> list3;
KeywordResults (List<String[]> list1, List<String[]> list2,
List<String[]> list3) {
this.list1 = list1; this.list2 = list2; this.list3 = list3;
}
}
(I usually do this as a nested class; whether to make it private or public depends on your needs.)
Normally it's a bad idea to declare a class with non-private fields, but I think if you're just using it as a wrapper just to return multiple values from a function or to create an object whose only purpose is to pass certain fields around together, and there aren't any other methods in the class, I think it's fine because the class doesn't really represent some "higher concept". Still, this is the kind of solution you'd adopt only when it's really appropriate; I think it's best to look around to see if there's a better design.

List that can only contain a value once

This is most certainly a noob question, but I haven't been able to find a good answer on Google or here, so I have to ask:
What kinda list should I use in Java, when I just want a value to be added once?
The problem is that I'm doing a web technology project in college (a webshop), and I have this cloud I connect too. I can the request the customer ID´s from those who bought items in my shop. What I want to do is extract these ID´s and add them to a list. But when extracting them I get the ID returned for every item they have bought, so I want a list that can check: "This value is already in this list, do nothing", or "This ID is not in the list, lets add the ID"
Is there a list that can do this, or a way to do it with a list without it getting too complicated?
You want a Set, this is the data structure that prevents duplicates. This is a Collection so you can define a function like so:
public Collection<MyObject> foo()
{
return new HashSet<MyObject>();
}
and at a later time change the return internally to this:
public Collection<MyObject> foo()
{
return new ArrayList<MyObject>();
}
And your API won't break.
A Set contains every value only once.
Though, the problem with HashSet is that the order in which the elements were added gets lost. So if you want to preserve the order of elements, I would suggest using a LinkedHashSet.
With a LinkedHashSet, iterating over the elements will return them in the order they were inserted.
public static void main(String[] args) {
Set<String> hashSet = new HashSet<>();
hashSet.add("first");
hashSet.add("second");
hashSet.add("third");
for (String s : hashSet) {
System.out.println(s); // no particular order
}
Set<String> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("first");
linkedHashSet.add("second");
linkedHashSet.add("third");
for (String s : linkedHashSet) {
System.out.println(s); // "first", "second", "third"
}
}
public boolean insertRecord(Programmer targetProgrammer, List programmerList) {
boolean flag = false;
for (Programmer p : programmerList){
if (targetProgrammer.getId() == p.getId()) {
return true;
}
}
return flag;
}
// Then when you invoke:
Programmer target = new Programmer(1,"Dev","Java");
if (!insertRecord(target, myList)) {
myList.add(target);
}
What you will be looking for is a Set, as a Set is a Collection that contains no duplicates.
There are a few types that you could use depending on your needs:
HashSet
LinkedHashSet
CopyOnWriteArraySet
EnumSet
TreeSet
ConcurrentSkipListSet
Better use HashSet as it takes care of your problem of unique IDs implicitly. Still better is SortedSet where you can have the unique elements printed in sorted order automatically.

Categories