Translate

sábado, 6 de agosto de 2016

Equivalencias entre Java y Javascript

Introducción 

A continuaciòn mostraré algunos trucos simples para aquellos que necesiten pasar de un lenguaje a otro

Mensajes de error


System.err.println("Error");

console.log("Error");


Primitivos y Wrappers

Parseo

Integer.valueOf(questarray[4]);

parseInt(questarray[4]);

Compare


compare=Double.compare(variable, 0.0);
if(compare>0) {

if (variable > 0.0) {

Excepciones


throw new RuntimeException("Error");

throw "Error";


 Arreglos


Arrays.copyOfRange y Array.protototype.slice


Arrays.copyOfRange(myArr, myArr.length / 2, myArr.length);

questionMapArr.slice( myArr.length / 2, myArr.length);



Listas


Como en Javascript no hay listas se usan arreglos con sus métodos equivalentes

lista.addAll(Arrays.asList(tokens));

lista.push(...tokens);


Collections.shuffle(questions);

this.questions.sort(() => .5 - Math.random());


Mapas



answerChoicesMap.entrySet().stream().forEach((choiceEntry) => {
  choiceQuestion.setChoice(
     choiceEntry.getKey(), choiceEntry.getValue()
  );
});


for (var [key, value] of answerChoicesMap) {
   choiceQuestion.setChoice(
       key, value
   );
}

No duden en rpeguntar si encesitan otra cosa como entrada por consola. Saludos!

1 comentario:

  1. Casinos Near DC (Uncasville, CT) - Mapyro
    Find Casinos 고양 출장샵 Near 서울특별 출장마사지 DC (Uncasville, CT) with 여수 출장샵 Mapyro. MGM National Harbor (MGM National Harbor), MGM 원주 출장마사지 National 서울특별 출장샵 Harbor, MGM Grand.

    ResponderEliminar