In-built useful Java functions

When I was extremely new to CP, I used to manually sort the arrays(by typing the whole sorting code) instead of using the Java in-built Arrays.sort(array_name) of the util package.

I would do a similar thing for binary search, until I came to know of Arrays.binarySearch(array_name,key) of the util package.

Programmers, I want to know whether there are any other such short-cut array functions, String functions, techniques,tricks, anything that you use that saves a lot of time or code writing and would help newbie coders like me as well as anyone who may not be aware of them.

You can also mentions short-cuts of other languages to help the users coding in them.

Thanks in advance :slight_smile:

Ahh! The memories!

I remember something similar happening with me too.

I was participating in a contest, and the problem included sorting the array and doing some operations, and I was getting TLE because of my slow sorting algo…and in the editorial I saw that “Arrays.sort” function and I was like “WTF?!”

Its good to inquire and know about these things.

However, I am afraid that the list is toooo long to be stated here. Hence I would request you to go through string fucntions list For others, please refer to meow’s answer, nothing that I give could be more detailed than that! . Pay special attention to array and string methods, they are what mostly save your life. (Array.push, pop and sort should be known at least. They are quite useful!)

If, after seeing the list, you have any query/doubt/need clarification, feel free to ask here again. :slight_smile:

1 Like

The best place to find convenient in-built functions is of course the language documentation. Here are Javadoc links for the Arrays and String class. You will find all the available functions listed along with their purpose, parameters, and return value. Go through them and take note of what may be useful to you.
Since you are new you wouldn’t be requiring these at the moment, but in the future you will likely have to use a Collection. Some useful Collections are ArrayList, ArrayDeque, PriorityQueue, HashSet and TreeSet. HashMap and TreeMap are also useful. StringBuilder is another useful class. If all these seem too overwhelming, don’t worry, you don’t need these right now. The Java library is pretty big :stuck_out_tongue:
On the plus side, because it is so big, it will often have something that matches a functionality you are looking for. Hope this helps!


@meooow- THAT LIST HOLYSHIT!! Wow XD Where do you find these stuff? THATS AWESOME!!!

_/_ Java Pro Meow.

Hey, Thanks :slight_smile:

Thanks :slight_smile: I’m sure that others too shall find these useful. Our mere school knowledge often lets us down in CP.

I agree. Whats worse is that if you use these stuff in school papers, they give zero citing “Out of Syllabus” thing. That’s sad, but I hope you enjoy the time you spend on Computer Science and Coding :slight_smile:

That’s true. Yup, I love Computer Science and Maths. Coding and algorithms fascinate me a lot. Competetive Programming has that thrill factor- especially short contests. They are fast and thrilling. Often it’s nail-biting, when only 5-10 minutes are left and you are trying your best to get your solution accepted. Long contests teach us a lot :slight_smile:

1 Like

Yes!! BTW, CS and Maths? You’re gonna go far kid :D. Knowing maths is a good advantage in CS.

Yes short contests are thrilling, and long contests are very, very good for knowledge.

I still remember how anxious I was during last minutes when I scored my final 15 marks (partial marking) out of 50 marked Q, earning a silver medal (hackerrank contest give medals based on ranking. Top 12 % get medal, and by those 15 marks I slipped into that :p)

I am happy you got your passion clear at this age dear! It took me 2 years to find I love CS and coding. :slight_smile:

1 Like

Haha, you praise too much. What’s the thing about passions? A person may have a liking today which may seem like a passion, but may change tomorrow. Passions often deceive :slight_smile:
But yeah, I’ve really liked Computer Science since class 8 and Maths since long back. I’m currently a class 11 Science student and the thing that distinguishes Maths and CS from Chem and Physics is the number of “AHA!” and “WOW!” moments it gives- you know, when you have been thinking about a problem for a long time, staring at it, and then, suddenly, you get the solution! Such moments are priceless…

1 Like

I feel you bro ^^

Passions which are deceptive are not passions, they are just ‘liking’. Its important to differentiate between a ‘liking’ and a ‘passion’. And yes, CS has a LOT of wow moments XD

If you are new in competitive programming then i suggest you to switch your java to C++ language as C++ is best and best among all other languages specially in Competitive programming. You can estimate this by looking submission of languages at any platform like codechef, SPOJ, Hackerearth, COdeforces etc. Even one of the top programmer Gennady Korotkevich also used C++ as a special tool In programming.

You can know more about this here:-

  1. Comparision
  2. Beginners
1 Like

@vijju123 I think you mistakenly set the link in your answer for Javascript functions instead of Java functions.

1 Like

Actually I am from an CISCE school like @akashbhalotia so I have faced his situation too :slight_smile:

Oh my god! shit!! Yes I did. Thanks for pointing it out dear!! Rectifying the error immediately :slight_smile:

(Actually I had the tab of javascript open, cause m learning it along with HTML. Lol me XD)

@bansal1232 I agree with you. c++ is best for competitive coding and easy to learn. There are some useful data structures in c++ you should learn.

  1. array
  2. vector
  3. stack
  4. heap
  5. map
  6. pair

Some Short-Cut functions in c++

vect.sort( vect.begin() , vect.end() );       //   Sorting a vector 

vect.clear();                                 //   Clearing a vector

vect.size();                                  //   Returns the size of a vector

There are a lot of functions I can not list them here. You can go to and search for the reference you want.

1 Like

You can’t forget Petr and uwi. It isn’t bad to use Java either. All that matters is your hold on Algorithms and Data Structures. And the functions that are listed below are available in Java too. Please don’t misguide people or build such stereotypes that C++ is better than Java in competitive programming. There are perks of using Java in comparison to C++.


Exactly. Most sites like Codechef focus on the algorithmic complexity as much as possible by allowing many languages and setting appropriate relative time limits. It is not necessary to learn a completely new language for competitive programming. Choice of language should be upto the programmer.

1 Like