You are not logged in. Please login at to post your questions!


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 :)

asked 05 Mar '17, 08:53

akashbhalotia's gravatar image

accept rate: 14%

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 :P
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!


answered 05 Mar '17, 10:25

meooow's gravatar image

6★meooow ♦
accept rate: 48%

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

_/_ Java Pro Meow.

(05 Mar '17, 10:29) vijju123 ♦♦5★

Hey, Thanks :)

(05 Mar '17, 11:07) akashbhalotia4★

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

(05 Mar '17, 21:23) meooow ♦6★

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. :)


answered 05 Mar '17, 10:03

vijju123's gravatar image

5★vijju123 ♦♦
accept rate: 18%

edited 05 Mar '17, 21:36

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

(05 Mar '17, 11:08) akashbhalotia4★

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 :)

(05 Mar '17, 11:11) vijju123 ♦♦5★

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 :)

(05 Mar '17, 11:25) akashbhalotia4★

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. :)

(05 Mar '17, 11:34) vijju123 ♦♦5★

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 :) 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...

(05 Mar '17, 11:52) akashbhalotia4★

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

(05 Mar '17, 12:44) vijju123 ♦♦5★

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

(05 Mar '17, 21:21) meooow ♦6★

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

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

(05 Mar '17, 21:32) vijju123 ♦♦5★
showing 5 of 8 show all

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

answered 05 Mar '17, 14:45

bansal1232's gravatar image

accept rate: 16%


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++.

(05 Mar '17, 22:10) coderbond0075★

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.

(05 Mar '17, 22:21) meooow ♦6★

@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.


answered 05 Mar '17, 21:37

only4's gravatar image

accept rate: 17%

edited 05 Mar '17, 21:41

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 05 Mar '17, 08:53

question was seen: 1,629 times

last updated: 05 Mar '17, 22:21