一张记录表 [只有两个字段]
-------------------------
姓名      工资
..      ...
-------------------------
要求用JAVA算法实现工资相同的的姓名,算法效率越高越好!小弟初学愚笨,在此请教高人!

解决方案 »

  1.   

    用sort排序工资,然后相邻的工资比较
      

  2.   

    select 工资,姓名 from 表 group by 工资java 里面按照顺序读取就可以了。
      

  3.   


    //数据集合
            List<List<HashMap<String,Object>>> tab = new ArrayList<List<HashMap<String,Object>>>();
           List<HashMap<String,Object>> row = null;
           HashMap<String,Object> cell = new HashMap<String,Object>();
           int i = 0;
           for(int c = 0;c< 2 ; c++)
           {
            row = new ArrayList<HashMap<String,Object>>();
            for(int r = 0;r<100;r++)
            {
            i++;
            cell = new HashMap<String,Object>();
            cell.put("姓名", "p-"+i);
            cell.put("工资", 2432.00+i);
            row.add(cell);
            }
            tab.add(row);
           }
           
           row = new ArrayList<HashMap<String,Object>>();
           cell = new HashMap<String,Object>();
           cell.put("姓名", "test");
           cell.put("工资", 2436.00);
           row.add(cell);
           tab.add(row);
           
           
           //打印
           for(List<HashMap<String,Object>> temp :tab)
           {
               for(HashMap<String,Object> tempHas : temp)
               {
               System.out.println("姓名: "+tempHas.get("姓名")+" 工资: "+tempHas.get("工资"));
               }
           }
      

  4.   


    //数据集合 
                    List <List <HashMap <String,Object> > >   tab   =   new   ArrayList <List <HashMap <String,Object> > > (); 
                  List <HashMap <String,Object> >   row   =   null; 
                  HashMap <String,Object>   cell   =   new   HashMap <String,Object> (); 
                  int   i   =   0; 
                  for(int   c   =   0;c <   2   ;   c++) 
                  { 
                  row   =   new   ArrayList <HashMap <String,Object> > (); 
                  for(int   r   =   0;r <100;r++) 
                  { 
                  i++; 
                  cell   =   new   HashMap <String,Object> (); 
                  cell.put("姓名",   "p-"+i); 
                  cell.put("工资",   2432.00+i); 
                  row.add(cell); 
                  } 
                  tab.add(row); 
                  } 
                  
                  //添加一条工资相同的数据
                  row   =   new   ArrayList <HashMap <String,Object> > (); 
                  cell   =   new   HashMap <String,Object> (); 
                  cell.put("姓名",   "test"); 
                  cell.put("工资",   2436.00); 
                  row.add(cell); 
                  tab.add(row); 
                  
                  
                  //打印 
                  for(List <HashMap <String,Object> >   temp   :tab) 
                  { 
                          for(HashMap <String,Object>   tempHas   :   temp) 
                          { 
                        System.out.println("姓名:   "+tempHas.get("姓名")+"   工资:   "+tempHas.get("工资")); 
                          } 
                  } 
      

  5.   

    我想要的就是,把上面这个数据集合中[tab],工资相同的姓名打印出来~
      

  6.   

    --------------- 结果 ----------------工资相等的数据一共有xxx组第一组:xxxx元
    包含姓名:xxx,xxx,xxx,xxx,xxx第二组:xxxx元
    包含姓名:xxx,xxx,xxx,xxx,xxx
    ...--------------------------------------
    来点代码撒,我加分++++++++++++
      

  7.   

    上面的打印结果是:
    -----------------------
    ...
    姓名: p-188 工资: 2620.0
    姓名: p-189 工资: 2621.0
    姓名: p-190 工资: 2622.0
    姓名: p-191 工资: 2623.0
    姓名: p-192 工资: 2624.0
    姓名: p-193 工资: 2625.0
    姓名: p-194 工资: 2626.0
    姓名: p-195 工资: 2627.0
    姓名: p-196 工资: 2628.0
    姓名: p-197 工资: 2629.0
    姓名: p-198 工资: 2630.0
    姓名: p-199 工资: 2631.0
    姓名: p-200 工资: 2632.0
    姓名: test  工资: 2436.0
    ...
      

  8.   


    package org.rut.util.algorithm;
    import org.rut.util.algorithm.support.BubbleSort;
    import org.rut.util.algorithm.support.HeapSort;
    import org.rut.util.algorithm.support.ImprovedMergeSort;
    import org.rut.util.algorithm.support.ImprovedQuickSort;
    import org.rut.util.algorithm.support.InsertSort;
    import org.rut.util.algorithm.support.MergeSort;
    import org.rut.util.algorithm.support.QuickSort;
    import org.rut.util.algorithm.support.SelectionSort;
    import org.rut.util.algorithm.support.ShellSort;
    public class SortUtil {
        public final static int INSERT = 1;
        public final static int BUBBLE = 2;
        public final static int SELECTION = 3;
        public final static int SHELL = 4;
        public final static int QUICK = 5;
        public final static int IMPROVED_QUICK = 6;
        public final static int MERGE = 7;
        public final static int IMPROVED_MERGE = 8;
        public final static int HEAP = 9;
        public static void sort(int[] data) {
            sort(data, IMPROVED_QUICK);
        }
        private static String[] name={
                "insert","bubble","selection","shell","quick","improved_quick","merge","improved_merge","heap"
        };
        private static Sort[] impl=new Sort[]{
                new InsertSort(),
                new BubbleSort(),
                new SelectionSort(),
                new ShellSort(),
                new QuickSort(),
                new ImprovedQuickSort(),
                new MergeSort(),
                new ImprovedMergeSort(),
                new HeapSort()
        };
        public static String toString(int algorithm){
            return name[algorithm-1];
        }    public static void sort(int[] data, int algorithm) {
            impl[algorithm-1].sort(data);
        }
        public static interface Sort {
            public void sort(int[] data);
        }
        public static void swap(int[] data, int i, int j) {
            int temp = data[i];
            data[i] = data[j];
            data[j] = temp;
        }
    }
     插入排序:
    package org.rut.util.algorithm.support;
    import org.rut.util.algorithm.SortUtil;
    public class InsertSort implements SortUtil.Sort{
        /* (non-Javadoc)
         * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
         */
        public void sort(int[] data) {
            int temp;
            for(int i=1;i<data.length;i++){
                for(int j=i;(j>0)&&(data[j]<data[j-1]);j--){
                    SortUtil.swap(data,j,j-1);
                }
            }        
        }
    }
      

  9.   

    冒泡排序:
    package org.rut.util.algorithm.support;
    import org.rut.util.algorithm.SortUtil;
    public class BubbleSort implements SortUtil.Sort{
        public void sort(int[] data) {
            int temp;
            for(int i=0;i<data.length;i++){
                for(int j=data.length-1;j>i;j--){
                    if(data[j]<data[j-1]){
                        SortUtil.swap(data,j,j-1);
                    }
                }
            }
        }
    }
      

  10.   

    选择排序:
    package org.rut.util.algorithm.support;
    import org.rut.util.algorithm.SortUtil;
    public class SelectionSort implements SortUtil.Sort {
        public void sort(int[] data) {
            int temp;
            for (int i = 0; i < data.length; i++) {
                int lowIndex = i;
                for (int j = data.length - 1; j > i; j--) {
                    if (data[j] < data[lowIndex]) {
                        lowIndex = j;
                    }
                }
                SortUtil.swap(data,i,lowIndex);
            }
        }
    }
      

  11.   

    选择排序:
    package org.rut.util.algorithm.support;
    import org.rut.util.algorithm.SortUtil;
    public class SelectionSort implements SortUtil.Sort {
        public void sort(int[] data) {
            int temp;
            for (int i = 0; i < data.length; i++) {
                int lowIndex = i;
                for (int j = data.length - 1; j > i; j--) {
                    if (data[j] < data[lowIndex]) {
                        lowIndex = j;
                    }
                }
                SortUtil.swap(data,i,lowIndex);
            }
        }
    }
      

  12.   

    [code=Java]快速排序:
    package org.rut.util.algorithm.support;
    import org.rut.util.algorithm.SortUtil;
    public class QuickSort implements SortUtil.Sort{
        public void sort(int[] data) {
            quickSort(data,0,data.length-1);        
        }
        private void quickSort(int[] data,int i,int j){
            int pivotIndex=(i+j)/2;
            //swap
            SortUtil.swap(data,pivotIndex,j);
            
            int k=partition(data,i-1,j,data[j]);
            SortUtil.swap(data,k,j);
            if((k-i)>1) quickSort(data,i,k-1);
            if((j-k)>1) quickSort(data,k+1,j);
        }
        private int partition(int[] data, int l, int r,int pivot) {
            do{
               while(data[++l]<pivot);
               while((r!=0)&&data[--r]>pivot);
               SortUtil.swap(data,l,r);
            }
            while(l<r);
            SortUtil.swap(data,l,r);        
            return l;
        }
    }
    code]
      

  13.   

    归并排序:
    package org.rut.util.algorithm.support;
    import org.rut.util.algorithm.SortUtil;
    public class MergeSort implements SortUtil.Sort{
        public void sort(int[] data) {
            int[] temp=new int[data.length];
            mergeSort(data,temp,0,data.length-1);
        }
        private void mergeSort(int[] data,int[] temp,int l,int r){
            int mid=(l+r)/2;
            if(l==r) return ;
            mergeSort(data,temp,l,mid);
            mergeSort(data,temp,mid+1,r);
            for(int i=l;i<=r;i++){
                temp[i]=data[i];
            }
            int i1=l;
            int i2=mid+1;
            for(int cur=l;cur<=r;cur++){
                if(i1==mid+1)
                    data[cur]=temp[i2++];
                else if(i2>r)
                    data[cur]=temp[i1++];
                else if(temp[i1]<temp[i2])
                    data[cur]=temp[i1++];
                else
                    data[cur]=temp[i2++];            
            }
        }
    }