Popular Posts

이은한. Powered by Blogger.

레이블이 Java Code인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Java Code인 게시물을 표시합니다. 모든 게시물 표시

2022년 3월 16일 수요일

Delete all spaces in String at JAVA


By String method

yourString.replaceAll(" ", "");

By regular expression

yourString.replaceAll("\\p{Z}", "");

Explanation

Most situations, using the string method will be good enough to delete all spaces in String. 


However, computers have some charsets that represent spaces such as IDEOGRAPHIC SPACE. 


In this case, use the regular expression to delete all the spaces.


Example

        String yourString = " this is\u3000test ru n";
        System.out.println(yourString);

        String byMethod = yourString.replaceAll(" ", "");
        System.out.println("byMethod: "+byMethod);

        String byRegularExpression = yourString.replaceAll("\\p{Z}", "");
        System.out.println("byRegularExpression: "+byRegularExpression);

Result

 this is test ru n // original String
thisis testrun // by method
thisistestrun // by regular expression

2022년 3월 7일 월요일

How to find the Largest Difference in an Array


How to find the Largest Difference in an Array

Example

[7,1,5,3,6] -> 6 (7-1)

JAVA code

    public static int getMaxDifferNumFromArray(int[] input) {
        return getMaxFromArray(input) - getMinFromArray(input);
    }
    public static int getMaxFromArray(int[] input) {
        int max = Integer.MIN_VALUE;
        for (int temp : input) {
            if (max < temp) {
                max = temp;
            }
        }
        return max;
    }
    public static int getMinFromArray(int[] input) {
        int min = Integer.MAX_VALUE;
        for (int temp : input) {
            if (min > temp) {
                min = temp;
            }
        }
        return min;
    }

the smallest element has to be before the biggest element

Example

[7,1,5,3,6] -> 5 (1-6)

JAVA code

    public static int getMaxDifferNumSmallFirstFromArray(int[] input) {
        int min = Integer.MAX_VALUE;
        int rtn = 0;
        int temp = 0;

        for (int element : input) {
            if (min < element) {
                temp = element - min;
                if (temp > rtn)
                    rtn = temp;
            } else {
                min = element;
            }
        }
        return rtn;
    }

the biggest element has to be before the smallest element

Example

[1,7,5,3,6] -> 4 (7-3)

JAVA code

    public static int getMaxDifferNumBigFirstFromArray(int[] input) {
        int max = Integer.MIN_VALUE;
        int rtn = 0;
        int temp = 0;

        for (int element : input) {
            if (max > element) {
                element = max - element;
                if (element > rtn)
                    rtn = element;
            } else {
                max = element;
            }
        }
        return rtn;
    }

2022년 2월 12일 토요일

7 Ways of Remove Duplicates from ArrayList in Java


what is fastest removing deplicates from ArrayList in Java?

The Best Winner

    private static ArrayList<String> rmDupliArrayList(ArrayList<String> input) {
        ArrayList<String> resultList = new ArrayList<String>();
        for (String temp : input) {
            if (!resultList.contains(temp)) {
                resultList.add(temp);
            }
        }
        return resultList;
    }

Seven Ways of Remove Duplicates from ArrayList in Java

1. Logic 1

import java.util.ArrayList;
    private static ArrayList<String> rmDupliArrayList(ArrayList<String> input) {
        ArrayList<String> resultList = new ArrayList<String>();
        for (String temp : input) {
            if (!resultList.contains(temp)) {
                resultList.add(temp);
            }
        }

        return resultList;
    }

2. HashSet

import java.util.ArrayList;
import java.util.HashSet;
    private static ArrayList<String> rmDupliArrayList(ArrayList<String> input) {
        return new ArrayList<String>( new HashSet<String>(input));
    }

3. TreeSet

import java.util.ArrayList;
import java.util.TreeSet;
    private static ArrayList<String> rmDupliArrayList(ArrayList<String> input) {
        return new ArrayList<String>(new TreeSet<String>(input));
    }

4. guava

google guava github link

import java.util.ArrayList;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
    private static ArrayList<String> rmDupliArrayList(ArrayList<String> input) {
        return Lists.newArrayList(Sets.newHashSet(input));
    }

5. stream

import java.util.ArrayList;
import java.util.stream.Collectors;
    private static ArrayList<String> rmDupliArrayList(ArrayList<String> input) {
        return (ArrayList<String>) input
                .parallelStream()
                .distinct()
                .collect(Collectors.toList());
    }

6. LinkedHashSet

import java.util.ArrayList;
import java.util.LinkedHashSet;
    private static ArrayList<String> rmDupliArrayList(ArrayList<String> input) {
        return new ArrayList<String>(new LinkedHashSet<String>(input));
    }

7. Logic 2

import java.util.ArrayList;
    private static ArrayList<String> rmDupliArrayList(ArrayList<String> input) {
        ArrayList<String> resultList = new ArrayList<String>(input);

        for (int i = 0; i < resultList.size(); i++) {
            for (int j = i+1; j < resultList.size(); j++) {
                if (resultList.get(j).equals(resultList.get(i))) {
                    resultList.remove(i);
                }
            }
        }
        return resultList;
    }

How to compare

public class Main {
    public static void main(String[] args) {
        ArrayList<String> dataList = new ArrayList<String>();
        ArrayList<String> resultList = new ArrayList<String>();
        dataList.add("1111");
        dataList.add("2222");
        dataList.add("3333");
        dataList.add("3333");
        dataList.add("aaaa");
        dataList.add("bbbb");
        dataList.add("eeee");
        dataList.add("bbbb");
        dataList.add("Hi");
        dataList.add("Hi?");
        dataList.add("Hi");
        dataList.add("This is not code");

        int x = 0;
        while (x < 10000) {
            long starttime = System.nanoTime();

            resultList = rmDupliArrayList(dataList);

            long endtime = System.nanoTime();
            long estimatedTime = endtime - starttime;
            System.out.println(estimatedTime);
            ++x;
        }
        System.out.println(resultList);
    }

loop 10,000 time and calculate the average.

Result

Conclusion

Only compare removing duplicates.
Logic is best. However, if you need sort, there may other better ways.

Addition

2017

2022

I have tested this 5years ago and I notice that computer speed is much faster.
5 years ago, I tested 1,000 times instead of 10,000