java數據去重
1. java關於順序數組數據去重,效率最高的方式是什麼
JAVA關於順序數組數據去重,效率最高的方式是使用LinkedHashSet也是Set,set的特徵就是對重復的元素只保存一個,LinkedHashSet只是在內部使用鏈表維護元素插入的順序
packagecom.question;
importjava.io.BufferedReader;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.OutputStream;
importjava.util.LinkedHashSet;
/**
*deletetheconflictString.
*
*@authorXxx
*/
publicclassQ16{
/**
*generatethetext.
*
*/
publicvoidinit(){
//writefile
OutputStreamoutputStream=null;
try{
outputStream=newFileOutputStream("C:/init.txt");
for(inti=0;i<100000;i++){
for(intj=0;j<2;j++){
outputStream.write(("Hello"+i).getBytes());
outputStream.write(" ".getBytes());
}
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(outputStream!=null){
outputStream=null;
}
}
}
/**
*filterthestring.
*
*@return
*/
publicLinkedHashSet<String>filter(){
//createaLinkedHashSetproject.
LinkedHashSet<String>linkedHashSet=newLinkedHashSet<String>();
try{
//readthefile.
InputStreaminputStream=newFileInputStream("C:/init.txt");
=newInputStreamReader(inputStream);
BufferedReaderbufferedReader=newBufferedReader(inputStreamReader);
Stringline=bufferedReader.readLine();
//
while(line!=null){
linkedHashSet.add(line);
line=bufferedReader.readLine();
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
//returntheresult.
returnlinkedHashSet;
}
@Deprecated
publicstaticvoidmain(String[]args){
Q16q16=newQ16();
//q16.init();
LinkedHashSet<String>linkedHashSet=q16.filter();
System.out.println(linkedHashSet.size());
}
}
2. 對於java中如何去除重復的數據
set集合就是不可重復的,你可以將數據放在這個數據裡面
3. java去掉數組里的重復的數據
計數方法,你在遍歷數組外邊int 一個數=0,如果某個數和數組【i】相等,就++
刪除方法我用的是笨方法,因為數組不能直接增刪操作,要先算出新數組的長度,在遍歷出去,我是取巧了先排序了,當然你也可以用其他排序方法排序
public static void main(String[] args) {
int [] arr={1,2,2,3,4,5};
int len=arr.length;
Arrays.sort(arr);
int leng=0;
for( int i=0;i<arr.length-1;i++){
if(arr[i]!=arr[i+1]){
leng++;
}
}
if(arr[arr.length-2]!=arr[arr.length-1]){
leng++;
}
System.out.println(leng);
int newlength=0;
int []newArr=new int[leng];
for( int i=0;i<arr.length-1;i++){
if(arr[i]!=arr[i+1]){
newArr[newlength]=arr[i];
newlength++;
}
}
if(arr[arr.length-2]!=arr[arr.length-1]){
newArr[newlength]=arr[arr.length-1];
}
for(int i=0;i<newArr.length;i++){
System.out.println(newArr[i]);
}
}
4. JAVA如何去除數組中每個元素中重復的內容
java數組中去掉重復數據可以使用set集合,set本身內部是不保存重復的數據的,如下代碼:
importjava.util.Arrays;importjava.util.Set;importjava.util.TreeSet;publicclassceshi{publicstaticvoidmain(String[]args){int[]testArr={5,5,1,2,3,6,-7,8,6,45,5};//新建一個int類型數組System.out.println(Arrays.toString(testArr));Set<Integer>set=newTreeSet<Integer>();//新建一個set集合for(inti:testArr){set.add(i);}Integer[]arr2=set.toArray(newInteger[0]);//數組的包裝類型不能轉只能自己轉;吧Integer轉為為int數組;int[]result=newint[arr2.length];for(inti=0;i<result.length;i++){result[i]=arr2[i];}System.out.println(Arrays.toString(arr2));}}
運行結果如下:
5. JAVA數組去重問題
我這有個笨辦法供樓主參考:
把vector中元素都取出來放到一個數組中,
根據數據的實際情況,
選擇不同的時間復雜度為log2N的排序演算法進行排序,
然後新建一個鏈表,
結點為保存數據和頻率的類,
遍歷排序後的數組,
如果鏈表的尾結點與數組中當前元素相同,
將尾結點的頻率加1,
否則append一個頻率為1的結點,
希望大牛們能給出更好的解法
6. JAVA 去掉重復的數據
無非就是檢查是否已經有數據的問題,用List做可能比較簡單點
publicclassSigleList{
List<Integer>nums;
publicSingleList(){
nums=newArrayList<Integer>();
}
publicvoidadd(Integernum){
if(!checkNumExist){
nums.add(num);
}
}
publicbooleancheckNumExist(Integer){
returnnums.contains(Integer);
}
}
這個SingleList就簡單實現了去重增加,可以用它來讀數據
publicclassTest{
publicstaticvoidmain(String[]args){
SingleListnums=newSingleList();
//產生100個數據
for(inti=0;i++;i<100){
//產生隨機數
Integernum=newRandom().nextInt(100000);
nums.add(num);
}
foreach(Integernum:nums){
System.out.println(num);
}
}
}
7. Java數組去重復問題
代碼如下:
public static void main(String[] args) {
int[] Nums = { 100, 2, 2, 3, 4, 5, 5, 5, 7, 7, 8, 9, 9, 23, 45, 75, 75,
78, 80, 99, 99, 199 };
Integer [] aa = getDistinct(Nums);
for(int i : aa){
System.out.println(i);
}
}
static Integer[] getDistinct(int num[]) {
List<Integer> list = new java.util.ArrayList<Integer>();
for (int i = 0; i < num.length; i++) {
if (!list.contains(num[i])) {//如果list數組不包括num[i]中的值的話,就返回true。
list.add(num[i]); //在list數組中加入num[i]的值。已經過濾過。
}
}
return list.toArray(new Integer[0]);
//toArray(數組)方法返回數組。並要指定Integer類型。new
integer[o]的空間大小不用考慮。因為如果list中的長度大於0(你integer的長度),toArray方法會分配一個具有指定數組的運行
時類型和此列表大小的新數組。
}
8. java字元串數組去重並計數
package lianXi;
public class helloWorld {
public static void main(String[] args) {
//初始化
String[][] rstop = { { "a", "b", "c", "d" }, { "b", "a", "b", "b" },
{ "e", "f", "g", "d" }, { "h", "i", "f", "k" } };
String[][] stop = new String[2][16];
for (int i = 0; i < stop.length; i++) {
for (int j = 0; j < stop[0].length; j++) {
stop[i][j] = "0";
}
}
int p = 0;
boolean boo = true;
//執行操作
for (int i = 0; i < rstop.length; i++) {
for (int j = 0; j < rstop[i].length; j++) {
for (int k = 0; k <= p; k++) {
if (stop[0][k].equals(rstop[i][j])) {
int temp = (Integer.parseInt(stop[1][k]) + 1);
stop[1][k] = temp + "";
boo = false;
break;
}
}
if (boo) {
stop[0][p] = rstop[i][j];
stop[1][p] = "1";
p++;
}
boo = true;
}
}
//輸出結果
for (int i = 0; i < stop.length; i++) {
for (int j = 0; j < stop[0].length; j++) {
System.out.print(stop[i][j] + " ");
}
System.out.println();
}
}
}
結果如下:
9. Java數組中重復數據的刪除
java數組中去掉重復數據可以使用set集合,set本身內部是不保存重復的數據的,如下代碼:
importjava.util.Arrays;
importjava.util.Set;
importjava.util.TreeSet;
publicclassceshi{
publicstaticvoidmain(String[]args){
int[]testArr={5,5,1,2,3,6,-7,8,6,45,5};//新建一個int類型數組
System.out.println(Arrays.toString(testArr));
Set<Integer>set=newTreeSet<Integer>();//新建一個set集合
for(inti:testArr){
set.add(i);
}
Integer[]arr2=set.toArray(newInteger[0]);
//數組的包裝類型不能轉只能自己轉;吧Integer轉為為int數組;
int[]result=newint[arr2.length];
for(inti=0;i<result.length;i++){
result[i]=arr2[i];
}
System.out.println(Arrays.toString(arr2));
}
}
運行結果如下: