Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9002211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:32:23+00:00 2026-06-16T00:32:23+00:00

I have an ArrayList(String) which contains a list of formatted dates like this: element

  • 0

I have an ArrayList(String) which contains a list of formatted dates like this:

element 1: "2012-5-1"
element 2: "2012-8-10"
element 3: "2012-12-5"
element 4: "2013-12-21"
element 5: "2013-12-13"
element 6: "2014-5-8"

What is the most efficient/framework way to create another list or normal primitive array that contains the unique year entries? For example my new list would contain:

element 1: "2012"
element 2: "2013"
element 3: "2014"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:32:24+00:00Added an answer on June 16, 2026 at 12:32 am

    Try this

    ArrayList<String> yearsOnlylist = new ArrayList<String> ();
    for(String s : elements) {
        String yearExtracted = s.substring(0,4);
        yearsOnlylist.add(yearExtracted);
    }
    

    Where elements is the name of your list of date in the extended form.

    Using as destination list

     LinkedList<String> yearsOnlylist = new LinkedList<String> ();
    

    instead of an ArrayList could sightly improve the conversion efficiency (because the add is O(1) in LinkedList) but access a specific position in a second time, has a lower efficiency (O(n) vs O(1)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ArrayList of HashMap like this: ArrayList<HashMap<String, String>> playListSongs = new ArrayList<HashMap<String,
I have wriiten a method like this public ArrayList<T> GetDoctorDetail(String name) { if (name!=null
Say I have a List like: List<String> list = new ArrayList<>(); list.add(a); list.add(h); list.add(f);
I have this code: FVDTO.setStatus(fail); List<String[]> invalidFields = new ArrayList<String[]>(); Iterator<ConstraintViolation<HazardSubmission>> iterator = cv.iterator();
I have a String which contains some formatted content with LineFeed after each line.
I have arraylist myorderdata . I want to retrieve this arraylist to one String
I have the following ArrayList ArrayList<HashMap<String, String>> list; HashMap<String, String> map; with the following
I have this method to display the contact numbers in my inbox: public ArrayList<String>
I have an object in my Action class which contains an arraylist of objects
I have a list of albums which I got using this: private List<Album> getAlbums()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.