3

I want to filter a single array into parts. Suppose I have an ArrayList of String :

 Array1  = ["2015-01-06","2015-04-06",
    "2016-06-06","2016-01-06",
    "2016-05-06","2017-02-06",
    "2017-04-06","2017-03-06",
    "2015-03-06","2016-04-06",
    "2016-02-06","2015-05-06",
    "2015-01-06","2016-06-06"]

I want it to filter in new arrays according to their years, so that the output will look like :

arrayA = ["2015-01-06","2015-04-06","2015-03-06","2015-05-06","2015-01-06"]
arrayB = ["2016-06-06","2016-01-06","2016-05-06","2016-04-06","2016-02-06","2016-06-06"]
arrayC = [""2017-02-06","2017-04-06","2017-03-06""]

The arrays are based on years now. I don't know the proper way for accomplishing this task.

4
  • I´d use a Map<String, String[]> to represent the years. Commented Mar 10, 2016 at 14:31
  • hey @KevinEsche thanks a lot man for responding , yeah it'll be much better but am getting the in this pattern from my source Commented Mar 10, 2016 at 14:32
  • Why Map<String, String[]> instead of Map<String, List<String>? Commented Mar 10, 2016 at 14:33
  • 1
    You'd need to parse the individual array elements to some degree (if year is always 4 digit and at the start, a substring might suffice) and then add the element to a list. Use a map for the individual lists as already suggested. Commented Mar 10, 2016 at 14:37

3 Answers 3

10

The question was edited to include the Android tag, making this inapplicable for the OP since Android doesn't support Java 8 yet, I'll leave it in case it helps someone not using Android though.


Using the Stream API, you can easily build a Map<Integer, List<String>> where each key is a year. You can use groupingBy(classifier) where the classifier extracts the year from the date. In this case, the date is parsed with LocalDate.parse but you could easily extend that by giving your own formatter.

public static void main(String[] args) {
    String[] array = {"2015-01-06","2015-04-06",
                      "2016-06-06","2016-01-06",
                      "2016-05-06","2017-02-06",
                      "2017-04-06","2017-03-06",
                      "2015-03-06","2016-04-06",
                      "2016-02-06","2015-05-06",
                      "2015-01-06","2016-06-06"};

    Map<Integer, List<String>> map =
        Arrays.stream(array)
              .collect(Collectors.groupingBy(s -> LocalDate.parse(s).getYear()));
              // or a simple s -> Integer.valueOf(s.substring(0, s.indexOf('-'))) in this case
}

You can then access the list you want from that map.

Sign up to request clarification or add additional context in comments.

5 Comments

hey @Tunaki thanks a lot for responding man , wasn't expected that much fast and so many responses i tried your answer and am getting some unresolved method exceptions in and in Collectors , Arrays.stream and in index.of i know that i have to use Stream API but i never used it before i tried to find some getting started but didn't get what i'm expecting ? is it what i need for using Stream Api ? github.com/aNNiMON/Android-Java-8-Stream-Example
@remyboys It's an API that is included in Java 8. It's built-in, you don't need another library.
okay but i tried to rebuild my project so many times still android studio is not recognising it :( , and my java version is already updated
but Android doesn't support Java 8, that's why you can't use it I suppose
@remyboys Android doesn't support Java 8 so, yes, it won't work. You should have added the android tag from the start. I edited to reflect that.
4

This will create an ArrayList each year

ArrayList<String> array1 = new ArrayList<String>();
Map<String, List<String>> mapForYear = new TreeMap<String, List<String>>();
for(String date : array1)
{
    String year = date.substring(0,4);
    if(!mapForYear.containsKey(year))
        mapForYear.put(year, new ArrayList<String>());
    mapForYear.get(year).add(date);
}

2 Comments

as a suggestion better use HashMap instead of TreeMap :)
I've used TreeMap because this will garantee you that your Map is sorted by year. If you don't need that you can use HashMap as you want ;)
0

That's easy if you use java 8 and Stream API. I wrote the following piece of code:

public List<String> getListByYear(String[] array, int year) {
    return Arrays.stream(array)
                 .filter(s -> s.contains(Integer.toString(year)))
                 .collect(Collectors.toList());
}

As to the question, there is such solution:

for (String s : array) {
    int key = LocalDate.parse(s).getYear();
    List<String> value = map.get(key);
    map.put(key, value == null
             ? new ArrayList<String>() { { add(s); } }
             : new ArrayList<String>(value) { { add(s); } }
    );
}

The same result as @Tunaki provided, but a less pretty. If Java 8 is not supported, it will be as the alternative as the most compact solution.

Output is here:

{
    2016 = [2016-06-06, 2016-01-06, 2016-05-06, 2016-04-06, 2016-02-06, 2016-06-06], 
    2017 = [2017-02-06, 2017-04-06, 2017-03-06],
    2015 = [2015-01-06, 2015-04-06, 2015-03-06, 2015-05-06, 2015-01-06]
} 

2 Comments

Not sure how helpful this is. He doesn't want to specify a year - he wants to build a data structures that is already "sliced" for the different years.
@Jägermeister yeah man thats true , cause i can't predict the year it could be anything

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.