|
Java example source code file (Frequency.java)
The Frequency.java Java example source code/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math3.stat; import java.io.Serializable; import java.text.NumberFormat; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.SortedMap; import java.util.TreeMap; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.MathUtils; /** * Maintains a frequency distribution. * <p> * Accepts int, long, char or Comparable values. New values added must be * comparable to those that have been added, otherwise the add method will * throw an IllegalArgumentException.</p> * <p> * Integer values (int, long, Integer, Long) are not distinguished by type -- * i.e. <code>addValue(Long.valueOf(2)), addValue(2), addValue(2l) all have * the same effect (similarly for arguments to <code>getCount, etc.). * <p>NOTE: byte and short values will be implicitly converted to int values * by the compiler, thus there are no explicit overloaded methods for these * primitive types.</p> * <p> * char values are converted by <code>addValue to Character instances. * As such, these values are not comparable to integral values, so attempts * to combine integral types with chars in a frequency distribution will fail. * </p> * <p> * Float is not coerced to Double. * Since they are not Comparable with each other the user must do any necessary coercion. * Float.NaN and Double.NaN are not treated specially; they may occur in input and will * occur in output if appropriate. * </b> * <p> * The values are ordered using the default (natural order), unless a * <code>Comparator is supplied in the constructor. * */ public class Frequency implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -3845586908418844111L; /** underlying collection */ private final SortedMap<Comparable freqTable; /** * Default constructor. */ public Frequency() { freqTable = new TreeMap<Comparable(); } /** * Constructor allowing values Comparator to be specified. * * @param comparator Comparator used to order values */ @SuppressWarnings("unchecked") // TODO is the cast OK? public Frequency(Comparator<?> comparator) { freqTable = new TreeMap<Comparable((Comparator>) comparator); } /** * Return a string representation of this frequency distribution. * * @return a string representation. */ @Override public String toString() { NumberFormat nf = NumberFormat.getPercentInstance(); StringBuilder outBuffer = new StringBuilder(); outBuffer.append("Value \t Freq. \t Pct. \t Cum Pct. \n"); Iterator<Comparable iter = freqTable.keySet().iterator(); while (iter.hasNext()) { Comparable<?> value = iter.next(); outBuffer.append(value); outBuffer.append('\t'); outBuffer.append(getCount(value)); outBuffer.append('\t'); outBuffer.append(nf.format(getPct(value))); outBuffer.append('\t'); outBuffer.append(nf.format(getCumPct(value))); outBuffer.append('\n'); } return outBuffer.toString(); } /** * Adds 1 to the frequency count for v. * <p> * If other objects have already been added to this Frequency, v must * be comparable to those that have already been added. * </p> * * @param v the value to add. * @throws MathIllegalArgumentException if <code>v is not comparable with previous entries */ public void addValue(Comparable<?> v) throws MathIllegalArgumentException { incrementValue(v, 1); } /** * Adds 1 to the frequency count for v. * * @param v the value to add. * @throws MathIllegalArgumentException if the table contains entries not * comparable to Long */ public void addValue(int v) throws MathIllegalArgumentException { addValue(Long.valueOf(v)); } /** * Adds 1 to the frequency count for v. * * @param v the value to add. * @throws MathIllegalArgumentException if the table contains entries not * comparable to Long */ public void addValue(long v) throws MathIllegalArgumentException { addValue(Long.valueOf(v)); } /** * Adds 1 to the frequency count for v. * * @param v the value to add. * @throws MathIllegalArgumentException if the table contains entries not * comparable to Char */ public void addValue(char v) throws MathIllegalArgumentException { addValue(Character.valueOf(v)); } /** * Increments the frequency count for v. * <p> * If other objects have already been added to this Frequency, v must * be comparable to those that have already been added. * </p> * * @param v the value to add. * @param increment the amount by which the value should be incremented * @throws MathIllegalArgumentException if <code>v is not comparable with previous entries * @since 3.1 */ public void incrementValue(Comparable<?> v, long increment) throws MathIllegalArgumentException { Comparable<?> obj = v; if (v instanceof Integer) { obj = Long.valueOf(((Integer) v).longValue()); } try { Long count = freqTable.get(obj); if (count == null) { freqTable.put(obj, Long.valueOf(increment)); } else { freqTable.put(obj, Long.valueOf(count.longValue() + increment)); } } catch (ClassCastException ex) { //TreeMap will throw ClassCastException if v is not comparable throw new MathIllegalArgumentException( LocalizedFormats.INSTANCES_NOT_COMPARABLE_TO_EXISTING_VALUES, v.getClass().getName()); } } /** * Increments the frequency count for v. * <p> * If other objects have already been added to this Frequency, v must * be comparable to those that have already been added. * </p> * * @param v the value to add. * @param increment the amount by which the value should be incremented * @throws MathIllegalArgumentException if the table contains entries not * comparable to Long * @since 3.3 */ public void incrementValue(int v, long increment) throws MathIllegalArgumentException { incrementValue(Long.valueOf(v), increment); } /** * Increments the frequency count for v. * <p> * If other objects have already been added to this Frequency, v must * be comparable to those that have already been added. * </p> * * @param v the value to add. * @param increment the amount by which the value should be incremented * @throws MathIllegalArgumentException if the table contains entries not * comparable to Long * @since 3.3 */ public void incrementValue(long v, long increment) throws MathIllegalArgumentException { incrementValue(Long.valueOf(v), increment); } /** * Increments the frequency count for v. * <p> * If other objects have already been added to this Frequency, v must * be comparable to those that have already been added. * </p> * * @param v the value to add. * @param increment the amount by which the value should be incremented * @throws MathIllegalArgumentException if the table contains entries not * comparable to Char * @since 3.3 */ public void incrementValue(char v, long increment) throws MathIllegalArgumentException { incrementValue(Character.valueOf(v), increment); } /** Clears the frequency table */ public void clear() { freqTable.clear(); } /** * Returns an Iterator over the set of values that have been added. * <p> * If added values are integral (i.e., integers, longs, Integers, or Longs), * they are converted to Longs when they are added, so the objects returned * by the Iterator will in this case be Longs.</p> * * @return values Iterator */ public Iterator<Comparable valuesIterator() { return freqTable.keySet().iterator(); } /** * Return an Iterator over the set of keys and values that have been added. * Using the entry set to iterate is more efficient in the case where you * need to access respective counts as well as values, since it doesn't * require a "get" for every key...the value is provided in the Map.Entry. * <p> * If added values are integral (i.e., integers, longs, Integers, or Longs), * they are converted to Longs when they are added, so the values of the * map entries returned by the Iterator will in this case be Longs.</p> * * @return entry set Iterator * @since 3.1 */ public Iterator<Map.Entry* * @param o1 the first object * @param o2 the second object * @return result of comparison * @throws NullPointerException when <i>o1 is null ,
* or when <code>((Comparable)o1).compareTo(o2) does
* @throws ClassCastException when <i>o1 is not a {@link Comparable Comparable},
* or when <code>((Comparable)o1).compareTo(o2) does
*/
@SuppressWarnings("unchecked") // cast to (T) may throw ClassCastException, see Javadoc
public int compare(Comparable<T> o1, ComparableOther Java examples (source code examples)Here is a short list of links related to this Java Frequency.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.