Logo

Programming-Idioms

Community backlog for Java

Expert validation

It is extremely valuable to curate the Java contents. Would you like to review these implementations?

If they can be improved, please help yourself. If you know a better way, please create a distinct entry. If they're broken, please flag them.

🗘

Idiom #5 Create a 2D Point data structure

Declare a container type for two floating-point numbers x and y



class Point { double x, y; }

Idiom #14 Pick uniformly a random floating point number in [a..b)

Pick a random number greater than or equals to a, strictly inferior to b. Precondition : a < b.



double pick(double a, double b){
	return a + (Math.random() * (b-a));
}

Idiom #254 Replace value in list

Replace all exact occurrences of "foo" with "bar" in the string list x



x = x.stream()
     .map(s -> s.equals("foo") ? "bar" : s)
     .toList();

For many more Java snippets to curate, see the full list.

Docs & demos

🗘
37% of the Java snippets don't have an external link to a documentation page yet. For example, these implementations would need a doc URL:

Idiom #50 Make an infinite loop

Write a loop that has no end clause.


while (true);

Idiom #154 Halfway between two hex color codes

Find color c, the average between colors c1, c2.

c, c1, c2 are strings of hex color codes: 7 chars, beginning with a number sign # .
Assume linear computations, ignore gamma corrections.


StringBuilder sb = new StringBuilder("#");
for(int i=0;i<3;i++) {
  String sub1 = c1.substring(1+2*i,3+2*i);
  String sub2 = c2.substring(1+2*i,3+2*i);
  int v1 = Integer.parseInt(sub1, 16);
  int v2 = Integer.parseInt(sub2, 16);
  int v = (v1 + v2)/2;
  String sub = String.format("%02X", v);
  sb.append(sub);
}
String c = sb.toString();

Idiom #195 Pass a two-dimensional array

Pass an array a of real numbers to the procedure (resp. function) foo. Output the size of the array, and the sum of all its elements when each element is multiplied with the array indices i and j (assuming they start from one).


public static void foo(int[][] a){
       System.out.println("Array a has a size of " + a.length+" by " + a[0].length);
       int sum = 0;
       for (int i = 0; i<a.length; i++){
           for (int j = 0; j<a[0].length; j++){
               sum += ((i+1)*(j+1)*a[i][j]);
           }
       }
       System.out.println("The sum of all elements multiplied by their indices is: " + sum);
    }

81% of the Java snippets don't have a runnable demo yet.

For example, these implementations would need a demo URL:

Idiom #257 Traverse list backwards

Print each index i and value x from the list items, from the last down to the first.


T x;
int i = items.size() - 1;
while (i != -1) {
    x = items.get(i);
    out.printf("%s %s%n", i--, x);
}

Idiom #251 Parse binary digits

Extract integer value i from its binary string representation s (in radix 2)
E.g. "1101" -> 13


Integer i = Integer.valueOf(s, 2);

Idiom #274 Remove all white space characters

Create the string t from the string s, removing all the spaces, newlines, tabulations, etc.


String t = "";
for (char c : s.toCharArray())
    if (!isWhitespace(c)) t = t + c;

Missing implementations

🗘

A Java implementation is missing for 50 idioms out of 371 idioms.

You may help by writing a Java snippet for these idioms:


Idiom #183 Make HTTP PUT request

Make a HTTP request with method PUT to the URL u


Idiom #323 Set HTTP request header

Make an HTTP request with method GET to the URL u, with the request header "accept-encoding: gzip", then store the body of the response in the buffer data.


Idiom #268 User-defined operator

Define a type vector containing three floating point numbers x, y, and z. Write a user-defined operator x that calculates the cross product of two vectors a and b.