Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

You have an interesting approach here...

"I don't want to deal with external libraries because I don't want to create classes to store the properties. I know, I'll use JavaScript!"

Now you have two problems.

###What you are doing

  • Load the entire approx 250kb file "AllSets.json" to a String (250kb memory, poof!)
  • Load the entire 119 byte file "SetList.json" to a String (119 byte memory, more acceptable)
  • Read the two strings you loaded into JSON objects, using Nashhorn
  • When looking up a card, you loop through all sets and all the cards in the set until you find a card with a matching id, then you return three properties on that card.

###Your code

There's one major performance drawback in your code, and that's the getCardData function (JavaScript part).

You loop through all the sets and loop through all the cards in each set until you find what you are looking for.

Instead, loop through the cards once after you load them, and store the cards in an associative arrayassociative array (pretty much a pure old JavaScript object) so that you can quickly lookup the cards in your getCardData method with a lookup speed of \$O(1)\$.

###How would I have done it?

Who said you have to create classes to store the properties? I believe most JSON libraries for Java comes with a method to read the JSON tree, without having to create classes to represent your data.

Just use Jackson's readTree method:

ObjectMapper mapper = new ObjectMapper();
JsonNode allSets = mapper.readTree(CardData.class.getResource("AllSets.json"));
JsonNode setList = mapper.readTree(CardData.class.getResource("SetList.json"));

Note that by using Jackson, and using the most raw data available (the URL), Jackson will not read it into a String first, and then convert to JSON, it will read it into JSON directly.

Now let's see what we can do with it, shall we?

Map<String, CardData> cardMap = new HashMap<>();
setList.forEach(node -> {
    JsonNode setNode = allSets.path(node.asText());
    setNode.forEach(card -> {
        CardData cardData = new CardData(card.get("id").asText(), card.get("name"), Optional.ofNullable(card.get("playerClass")));
        cardMap.put(cardData.getId(), cardData);
    });
});

Voilà. Now all you need is a method to retrieve the card data, very simple:

CardData getCardData(String id) {
    return cardMap.get(id);
}

You have an interesting approach here...

"I don't want to deal with external libraries because I don't want to create classes to store the properties. I know, I'll use JavaScript!"

Now you have two problems.

###What you are doing

  • Load the entire approx 250kb file "AllSets.json" to a String (250kb memory, poof!)
  • Load the entire 119 byte file "SetList.json" to a String (119 byte memory, more acceptable)
  • Read the two strings you loaded into JSON objects, using Nashhorn
  • When looking up a card, you loop through all sets and all the cards in the set until you find a card with a matching id, then you return three properties on that card.

###Your code

There's one major performance drawback in your code, and that's the getCardData function (JavaScript part).

You loop through all the sets and loop through all the cards in each set until you find what you are looking for.

Instead, loop through the cards once after you load them, and store the cards in an associative array (pretty much a pure old JavaScript object) so that you can quickly lookup the cards in your getCardData method with a lookup speed of \$O(1)\$.

###How would I have done it?

Who said you have to create classes to store the properties? I believe most JSON libraries for Java comes with a method to read the JSON tree, without having to create classes to represent your data.

Just use Jackson's readTree method:

ObjectMapper mapper = new ObjectMapper();
JsonNode allSets = mapper.readTree(CardData.class.getResource("AllSets.json"));
JsonNode setList = mapper.readTree(CardData.class.getResource("SetList.json"));

Note that by using Jackson, and using the most raw data available (the URL), Jackson will not read it into a String first, and then convert to JSON, it will read it into JSON directly.

Now let's see what we can do with it, shall we?

Map<String, CardData> cardMap = new HashMap<>();
setList.forEach(node -> {
    JsonNode setNode = allSets.path(node.asText());
    setNode.forEach(card -> {
        CardData cardData = new CardData(card.get("id").asText(), card.get("name"), Optional.ofNullable(card.get("playerClass")));
        cardMap.put(cardData.getId(), cardData);
    });
});

Voilà. Now all you need is a method to retrieve the card data, very simple:

CardData getCardData(String id) {
    return cardMap.get(id);
}

You have an interesting approach here...

"I don't want to deal with external libraries because I don't want to create classes to store the properties. I know, I'll use JavaScript!"

Now you have two problems.

###What you are doing

  • Load the entire approx 250kb file "AllSets.json" to a String (250kb memory, poof!)
  • Load the entire 119 byte file "SetList.json" to a String (119 byte memory, more acceptable)
  • Read the two strings you loaded into JSON objects, using Nashhorn
  • When looking up a card, you loop through all sets and all the cards in the set until you find a card with a matching id, then you return three properties on that card.

###Your code

There's one major performance drawback in your code, and that's the getCardData function (JavaScript part).

You loop through all the sets and loop through all the cards in each set until you find what you are looking for.

Instead, loop through the cards once after you load them, and store the cards in an associative array (pretty much a pure old JavaScript object) so that you can quickly lookup the cards in your getCardData method with a lookup speed of \$O(1)\$.

###How would I have done it?

Who said you have to create classes to store the properties? I believe most JSON libraries for Java comes with a method to read the JSON tree, without having to create classes to represent your data.

Just use Jackson's readTree method:

ObjectMapper mapper = new ObjectMapper();
JsonNode allSets = mapper.readTree(CardData.class.getResource("AllSets.json"));
JsonNode setList = mapper.readTree(CardData.class.getResource("SetList.json"));

Note that by using Jackson, and using the most raw data available (the URL), Jackson will not read it into a String first, and then convert to JSON, it will read it into JSON directly.

Now let's see what we can do with it, shall we?

Map<String, CardData> cardMap = new HashMap<>();
setList.forEach(node -> {
    JsonNode setNode = allSets.path(node.asText());
    setNode.forEach(card -> {
        CardData cardData = new CardData(card.get("id").asText(), card.get("name"), Optional.ofNullable(card.get("playerClass")));
        cardMap.put(cardData.getId(), cardData);
    });
});

Voilà. Now all you need is a method to retrieve the card data, very simple:

CardData getCardData(String id) {
    return cardMap.get(id);
}
typos and formatting and grammar
Source Link
Simon Forsberg
  • 59.7k
  • 9
  • 160
  • 312

You have an interesting approach here...

"I don't want to deal with external libraries because I don't want to create classes to store the properties. I know, I'll use JavaScript!"

Now you have two problems.

###What you are doing

  • Load the entire approx 250kb file "AllSets.json" to a String (250kb memory, poof!)
  • Load the entire 119 byte file "SetList.json" to a String (119 byte memory, more acceptable)
  • Read the two strings you loaded into JSON objects, using Nashhorn
  • When looking up a filecard, you loop through all sets and all the cards in the set until you find a card with a matching id, then you return three properties on that card.

###Your code

There's one major performance drawback in your code, and that's the getCardData function (JavaScript part).

You loop through all the sets and loop through all the cards in each set until you find what you are looking for.

Instead, loop through the cards once after you load them, and store the cards in an associative array (pretty much a pure old JavaScript object) so that you can quickly lookup the cards in your getCardData method with a lookup speed of \$O(1)\$.

###How would I have done it?

Who said you have to create classes to store the properties? I believe most JSON libraries for Java comes with a method to read the JSON tree, without having to create classes to represent your data.

Just use Jackson's readTree method:

    ObjectMapper mapper = new ObjectMapper();
 
    JsonNode allSets = mapper.readTree(CardData.class.getResource("AllSets.json"));
    JsonNode setList = mapper.readTree(CardData.class.getResource("SetList.json"));

Note that by using Jackson, and using the most raw data available (the URL), Jackson will not read it into a String first, and then convert to JSON, it will read it into JSON directly.

Now let's see what we can do with it, shall we?

Map<String, CardData> cardMap = new HashMap<>();
setList.forEach(node -> {
    JsonNode setNode = allSets.path(node.asText());
    setNode.forEach(card -> {
        CardData cardData = new CardData(card.get("id").asText(), card.get("name"), Optional.ofNullable(card.get("playerClass")));
        cardMap.put(cardData.getId(), cardData);
    });
});

Voilà. Now all you need is a method to retrieve the card data, very simple:

CardData getCardData(String id) {
    return cardMap.get(id);
}

You have an interesting approach here...

"I don't want to deal with external libraries because I don't want to create classes to store the properties. I know, I'll use JavaScript!"

Now you have two problems.

###What you are doing

  • Load the entire approx 250kb file "AllSets.json" to a String (250kb memory, poof!)
  • Load the entire 119 byte file "SetList.json" to a String (119 byte memory, more acceptable)
  • Read the two strings you loaded into JSON objects, using Nashhorn
  • When looking up a file, loop through all sets and all the cards in the set until you find a card with a matching id, return three properties on that card.

###Your code

There's one major performance drawback in your code, and that's the getCardData function (JavaScript part).

You loop through all the sets and loop through all the cards in each set until you find what you are looking for.

Instead, loop through the cards once after you load them, and store the cards in an associative array (pretty much a pure old JavaScript object) so that you can quickly lookup the cards in your getCardData method with a lookup speed of \$O(1)\$.

###How would I have done it?

Who said you have to create classes to store the properties? I believe most JSON libraries for Java comes with a method to read the JSON tree, without having to create classes to represent your data.

Just use Jackson's readTree method:

    ObjectMapper mapper = new ObjectMapper();
 
    JsonNode allSets = mapper.readTree(CardData.class.getResource("AllSets.json"));
    JsonNode setList = mapper.readTree(CardData.class.getResource("SetList.json"));

Note that by using Jackson, and using the most raw data available (the URL), Jackson will not read it into a String first, and then convert to JSON, it will read it into JSON directly.

Now let's see what we can do with it, shall we?

Map<String, CardData> cardMap = new HashMap<>();
setList.forEach(node -> {
    JsonNode setNode = allSets.path(node.asText());
    setNode.forEach(card -> {
        CardData cardData = new CardData(card.get("id").asText(), card.get("name"), Optional.ofNullable(card.get("playerClass")));
        cardMap.put(cardData.getId(), cardData);
    });
});

Voilà. Now all you need is a method to retrieve the card data, very simple:

CardData getCardData(String id) {
    return cardMap.get(id);
}

You have an interesting approach here...

"I don't want to deal with external libraries because I don't want to create classes to store the properties. I know, I'll use JavaScript!"

Now you have two problems.

###What you are doing

  • Load the entire approx 250kb file "AllSets.json" to a String (250kb memory, poof!)
  • Load the entire 119 byte file "SetList.json" to a String (119 byte memory, more acceptable)
  • Read the two strings you loaded into JSON objects, using Nashhorn
  • When looking up a card, you loop through all sets and all the cards in the set until you find a card with a matching id, then you return three properties on that card.

###Your code

There's one major performance drawback in your code, and that's the getCardData function (JavaScript part).

You loop through all the sets and loop through all the cards in each set until you find what you are looking for.

Instead, loop through the cards once after you load them, and store the cards in an associative array (pretty much a pure old JavaScript object) so that you can quickly lookup the cards in your getCardData method with a lookup speed of \$O(1)\$.

###How would I have done it?

Who said you have to create classes to store the properties? I believe most JSON libraries for Java comes with a method to read the JSON tree, without having to create classes to represent your data.

Just use Jackson's readTree method:

ObjectMapper mapper = new ObjectMapper();
JsonNode allSets = mapper.readTree(CardData.class.getResource("AllSets.json"));
JsonNode setList = mapper.readTree(CardData.class.getResource("SetList.json"));

Note that by using Jackson, and using the most raw data available (the URL), Jackson will not read it into a String first, and then convert to JSON, it will read it into JSON directly.

Now let's see what we can do with it, shall we?

Map<String, CardData> cardMap = new HashMap<>();
setList.forEach(node -> {
    JsonNode setNode = allSets.path(node.asText());
    setNode.forEach(card -> {
        CardData cardData = new CardData(card.get("id").asText(), card.get("name"), Optional.ofNullable(card.get("playerClass")));
        cardMap.put(cardData.getId(), cardData);
    });
});

Voilà. Now all you need is a method to retrieve the card data, very simple:

CardData getCardData(String id) {
    return cardMap.get(id);
}
edited body
Source Link
Simon Forsberg
  • 59.7k
  • 9
  • 160
  • 312

You have an interesting approach here...

"I don't want to deal with external libraries because I don't want to create classes to store the properties. I know, I'll use JavaScript!"

Now you have two problems.

###What you are doing

  • Load the entire approx 250kb file "AllSets.json" to a String (250kb memory, poof!)
  • Load the entire 119 byte file "SetList.json" to a String (119 byte memory, more acceptable)
  • Read the two strings you loaded into JSON objects, using Nashhorn
  • When looking up a file, loop through all sets and all the cards in the set until you find a card with a matching id, return three properties on that card.

###Your code

There's one major performance drawback in your code, and that's the getCardData function (JavaScript part).

You loop through all the sets and loop through all the cards in each set until you find what you are looking for.

Instead, loop through the cards once after you load them, and store the cards in an associative array (pretty much a pure old JavaScript object) so that you can quickly lookup the cards in your getCardData method with a lookup speed of \$O(1)\$.

###How would I have done it?

Who said you have to create classes to store the properties? I believe most JSON libraries for Java comes with a method to read the JSON tree, without having to create classes to represent your data.

Just use Jackson's readTree method:

    ObjectMapper mapper = new ObjectMapper();

    JsonNode allSets = mapper.readTree(CardData.class.getResource("AllSets.json"));
    JsonNode setList = mapper.readTree(CardData.class.getResource("SetList.json"));

Note that by using Jackson, and using the most raw data available (the URL), Jackson will not read it into a String first, and then convert to JSON, it will read it into JSON directly.

Now let's see what we can do with it, shall we?

Map<String, CardData> cardMap = new HashMap<>();
setList.forEach(node -> {
    JsonNode setNode = allSets.path(node.asText());
    setNode.forEach(card -> {
        CardData cardData = new CardData(card.get("id").asText(), card.get("name"), Optional.ofNullable(card.get("playerClass")));
        cardMap.put(cardData.getId(), cardData);
    });
});

ViolàVoilà. Now all you need is a method to retrieve the card data, very simple:

CardData getCardData(String id) {
    return cardMap.get(id);
}

You have an interesting approach here...

"I don't want to deal with external libraries because I don't want to create classes to store the properties. I know, I'll use JavaScript!"

Now you have two problems.

###What you are doing

  • Load the entire approx 250kb file "AllSets.json" to a String (250kb memory, poof!)
  • Load the entire 119 byte file "SetList.json" to a String (119 byte memory, more acceptable)
  • Read the two strings you loaded into JSON objects, using Nashhorn
  • When looking up a file, loop through all sets and all the cards in the set until you find a card with a matching id, return three properties on that card.

###Your code

There's one major performance drawback in your code, and that's the getCardData function (JavaScript part).

You loop through all the sets and loop through all the cards in each set until you find what you are looking for.

Instead, loop through the cards once after you load them, and store the cards in an associative array (pretty much a pure old JavaScript object) so that you can quickly lookup the cards in your getCardData method with a lookup speed of \$O(1)\$.

###How would I have done it?

Who said you have to create classes to store the properties? I believe most JSON libraries for Java comes with a method to read the JSON tree, without having to create classes to represent your data.

Just use Jackson's readTree method:

    ObjectMapper mapper = new ObjectMapper();

    JsonNode allSets = mapper.readTree(CardData.class.getResource("AllSets.json"));
    JsonNode setList = mapper.readTree(CardData.class.getResource("SetList.json"));

Note that by using Jackson, and using the most raw data available (the URL), Jackson will not read it into a String first, and then convert to JSON, it will read it into JSON directly.

Now let's see what we can do with it, shall we?

Map<String, CardData> cardMap = new HashMap<>();
setList.forEach(node -> {
    JsonNode setNode = allSets.path(node.asText());
    setNode.forEach(card -> {
        CardData cardData = new CardData(card.get("id").asText(), card.get("name"), Optional.ofNullable(card.get("playerClass")));
        cardMap.put(cardData.getId(), cardData);
    });
});

Violà. Now all you need is a method to retrieve the card data, very simple:

CardData getCardData(String id) {
    return cardMap.get(id);
}

You have an interesting approach here...

"I don't want to deal with external libraries because I don't want to create classes to store the properties. I know, I'll use JavaScript!"

Now you have two problems.

###What you are doing

  • Load the entire approx 250kb file "AllSets.json" to a String (250kb memory, poof!)
  • Load the entire 119 byte file "SetList.json" to a String (119 byte memory, more acceptable)
  • Read the two strings you loaded into JSON objects, using Nashhorn
  • When looking up a file, loop through all sets and all the cards in the set until you find a card with a matching id, return three properties on that card.

###Your code

There's one major performance drawback in your code, and that's the getCardData function (JavaScript part).

You loop through all the sets and loop through all the cards in each set until you find what you are looking for.

Instead, loop through the cards once after you load them, and store the cards in an associative array (pretty much a pure old JavaScript object) so that you can quickly lookup the cards in your getCardData method with a lookup speed of \$O(1)\$.

###How would I have done it?

Who said you have to create classes to store the properties? I believe most JSON libraries for Java comes with a method to read the JSON tree, without having to create classes to represent your data.

Just use Jackson's readTree method:

    ObjectMapper mapper = new ObjectMapper();

    JsonNode allSets = mapper.readTree(CardData.class.getResource("AllSets.json"));
    JsonNode setList = mapper.readTree(CardData.class.getResource("SetList.json"));

Note that by using Jackson, and using the most raw data available (the URL), Jackson will not read it into a String first, and then convert to JSON, it will read it into JSON directly.

Now let's see what we can do with it, shall we?

Map<String, CardData> cardMap = new HashMap<>();
setList.forEach(node -> {
    JsonNode setNode = allSets.path(node.asText());
    setNode.forEach(card -> {
        CardData cardData = new CardData(card.get("id").asText(), card.get("name"), Optional.ofNullable(card.get("playerClass")));
        cardMap.put(cardData.getId(), cardData);
    });
});

Voilà. Now all you need is a method to retrieve the card data, very simple:

CardData getCardData(String id) {
    return cardMap.get(id);
}
added 150 characters in body
Source Link
Simon Forsberg
  • 59.7k
  • 9
  • 160
  • 312
Loading
Source Link
Simon Forsberg
  • 59.7k
  • 9
  • 160
  • 312
Loading