Java List Sort sortByOccurencies(List lsSame)

Here you can find the source of sortByOccurencies(List lsSame)

Description

sort By Occurencies

License

Apache License

Declaration

private static ArrayList<String> sortByOccurencies(List<String> lsSame) 

Method Source Code

//package com.java2s;
/*//from w ww . j  a v  a 2  s. co  m
 * Copyright 2013 SciFY NPO <info@scify.org>.
 *
 * This product is part of the NewSum Free Software.
 * For more information about NewSum visit
 * 
 *    http://www.scify.gr/site/en/our-projects/completed-projects/newsum-menu-en
 *
 * Licensed 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.
 * 
 * If this code or its output is used, extended, re-engineered, integrated, 
 * or embedded to any extent in another software or hardware, there MUST be 
 * an explicit attribution to this work in the resulting source code, 
 * the packaging (where such packaging exists), or user interface 
 * (where such an interface exists). 
 * The attribution must be of the form "Powered by NewSum, SciFY"
 */

import java.util.ArrayList;

import java.util.Collections;
import java.util.Comparator;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

public class Main {
    private static ArrayList<String> sortByOccurencies(List<String> lsSame) {
        HashMap<String, Integer> hsOccs = new HashMap<String, Integer>();
        for (String each : lsSame) {
            if (!hsOccs.containsKey(each)) {
                hsOccs.put(each, 1);
            } else {
                hsOccs.put(each, hsOccs.get(each) + 1);
            }
        }
        Iterator it = hsOccs.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry mp = (Map.Entry) it.next();
            Integer val = (Integer) mp.getValue();
            if (val < 4) {
                it.remove();
            }
        }
        if (hsOccs.isEmpty()) {
            return null;
        }
        ArrayList<Map.Entry<String, Integer>> lsRes = new ArrayList<Map.Entry<String, Integer>>(hsOccs.entrySet());
        Collections.sort(lsRes, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        ArrayList<String> lsRet = new ArrayList<String>();
        for (Map.Entry mp : lsRes) {
            lsRet.add((String) mp.getKey() + "=" + (String) Integer.toString((Integer) mp.getValue()));
        }
        return lsRet;
    }
}

Related

  1. sortBy(List> list, final Map orderBy)
  2. sortByByteDeux(List symbolAL, int delimiter)
  3. sortByClosestMatch(final List strings, final String toMatch)
  4. sortByLength(List li, String text)
  5. sortByLength(List list, final boolean ascending)
  6. sorted(List l, Comparator comparator)
  7. sorted(List l, Comparator comparator)
  8. sorted(List l)
  9. sortedArray(List list)

    HOME | Copyright © www.java2s.com 2016