Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Copyright (C) Luxoft.
- * All rights reserved.
- */
- package net.avalter.mutableInteger;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Random;
- /**
- * @author avalter.
- * @version 1.0
- */
- public class SimpleInteger {
- private static Random rnd = new Random();
- public static final int SIZE = 1000000000;
- public static void main(String[] args) throws InterruptedException {
- Thread.sleep(10000);
- for (int i = 0; i < 10; ++i) {
- long start = System.nanoTime();
- //Map<Byte, Integer> res = countStrings();
- //Map<Byte, MutableInteger> res = countMutable();
- Map<Byte, Integer> res = countArray();
- long finish = System.nanoTime();
- System.out.println(res.keySet());
- System.out.println(finish - start);
- }
- }
- public static Map<Byte, Integer> countStrings() {
- Map<Byte, Integer> counts = new HashMap<Byte, Integer>();
- for (int i = 0; i < SIZE; ++i) {
- byte next = getNextByte();
- Integer val = counts.get(next);
- if (val == null) {
- counts.put(next, 1);
- } else {
- counts.put(next, val + 1);
- }
- }
- return counts;
- }
- public static Map<Byte, MutableInteger> countMutable() {
- Map<Byte, MutableInteger> counts = new HashMap<Byte, MutableInteger>();
- for (int i = 0; i < SIZE; ++i) {
- byte next = getNextByte();
- MutableInteger val = counts.get(next);
- if(val == null) counts.put(next, new MutableInteger(1));
- else val.increment();
- }
- return counts;
- }
- public static Map<Byte, Integer> countArray() {
- Map<Byte, Integer> pointer = new HashMap<Byte, Integer>();
- int freeIndex = 0;
- int[] data = new int[10];
- for (int i = 0; i < SIZE; ++i) {
- byte next = getNextByte();
- Integer index = pointer.get(next);
- if (index == null) {
- pointer.put(next, freeIndex);
- index = freeIndex;
- freeIndex++;
- }
- data[index]++;
- }
- for (Byte b : pointer.keySet()) {
- pointer.put(b, data[pointer.get(b)]);
- }
- return pointer;
- }
- public static byte getNextByte() {
- return (byte) rnd.nextInt(4);
- }
- public static class MutableInteger {
- private int value;
- public MutableInteger(int value) {
- this.value = value;
- }
- public int intValue() {
- return value;
- }
- public void set(int value) {
- this.value = value;
- }
- public void increment() {
- value++;
- }
- public String toString() {
- return String.valueOf(value);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement