-2

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

Eric
  • 1,271
  • 4
  • 14
  • 21
  • Related: [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Sotirios Delimanolis Sep 20 '13 at 17:24
  • 2
    Don't understand your question but for comparing objects for observational equality you **MUST** use `equals()` – nachokk Sep 20 '13 at 17:24
  • If you have `List` , you can't add a plain `String` but you need to add a `String[]`. Also, *I think it is time consumption* do not really *think* on this unless you have demonstrated this is a bottleneck using a profiler. – Luiggi Mendoza Sep 20 '13 at 17:25
  • You are adding the same `result` for each different list has no sense your example, please provide a [SSCCE](http://www.sscce.org) – nachokk Sep 20 '13 at 17:28
  • Sorry guys, it is not the really code just pseudo-code. The code is running very well. I am going to edit it again. I will put more details. I don't have a lot of experience in Array. So could you guys cancel the downvote, I think it is a question. – Eric Sep 20 '13 at 17:32
  • *could you guys cancel the downvote* sadly downvotes are anonymous unless the downvoter clearly claims he/she did it. Just edit your question to show the real specific programming problem/question and if it is right people anonymously will upvote it. – Luiggi Mendoza Sep 20 '13 at 17:35
  • Thanks Luiggi, I got your point. – Eric Sep 20 '13 at 17:58

5 Answers5

3

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.

0

You can return a list of lists.

List<List<String[]>> ll = new ArrayList<>();
ll.add(list1);
ll.add(list2);
return ll;
Joni
  • 108,737
  • 14
  • 143
  • 193
0

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"))
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

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.

Trey Jonn
  • 350
  • 3
  • 17
0

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.

ajb
  • 31,309
  • 3
  • 58
  • 84
  • Thank you for your solution. I learn a lot from your answer. However, it the return lists are to much, this method could be a problem. – Eric Sep 20 '13 at 17:59
  • I'm not sure what you mean "if the return lists are too much". If you know you will only have three lists, then it doesn't matter how long they are, since my code example does not go through the lists to make copies of them; they store *references* to the lists which takes no time at all and very little additional space. If there really are a lot of lists, or you're looking for more than 3 keywords, then you will want to use some other structure like a `List>` or `Map>` where the "keyword" is the map key. – ajb Sep 20 '13 at 18:04
  • Thanks again, I am going to use Map> – Eric Sep 20 '13 at 18:14