Sorting String[][] unexpected output - java

I want to sort my String[][] with respect to second column. I tried this
public static String[][] sorting_minn(String[][] list){
double[] temp = new double[list.length];
String[][] tempf = list;
if(list[1][1]!=null){
for(int i = 0; i<list.length; i++){
if(list[i][2]==null){
break;
} else {
temp[i]=Double.parseDouble(list[i][2]);
}
}
Arrays.sort(temp);
for(int f = 0; f<list.length-1;f++){
for(int m = 0; m<list.length;m++){
if(list[m][2]!=null && Double.parseDouble(list[m][2])==temp[f]){
for(int n = 0; n<4; n++){
tempf[list.length-f-1][n]=list[m][n];
}
m = list.length;
}
}
}
}
return tempf;
}
As an output I get this: . I need suggestion on how to improve this code.

try something like:
Arrays.sort(list, new Comparator<String[]>() {
#Override
public int compare(String[] o1, String[] o2) {
String left = o1[1]!=null ? o1[1] : "";
String right = o2[1]!=null ? o2[1] : "";
return left.compareTo(right);
}
});
this treats nulls as empty strings, and exploits the fact that strings are comparable, although lexicographic. if you want the reverse order just do this instead:
right.compareTo(left)
if you want integer ordering you could parse an Integer out of both sides (Integer.MIN for null) and compare 2 Integers

Related

How to print out results in NQueens

I am new to JAVA. Below is my code of NQueens problem. The results are [[Ljava.lang.String;#123a439b, [Ljava.lang.String;#7de26db8].
Can anyone please help? Thank you very much! I copyied the code from other's blog. The code should be correct. I just don't know how to print out the results. I think this should be not very hard. My background is not computer science, that's maybe why I have the trouble. Thank you!
import java.util.ArrayList;
public class Solution {
public ArrayList<String[]> solveNQueens(int n) {
ArrayList<String[]> res = new ArrayList<String[]>();
if(n<=0)
return res;
int [] columnVal = new int[n];
DFS_helper(n,res,0,columnVal);
return res;
}
public void DFS_helper(int nQueens, ArrayList<String[]> res, int row, int[] columnVal){
if(row == nQueens){
String[] unit = new String[nQueens];
for(int i = 0; i < nQueens; i++){
StringBuilder s = new StringBuilder();
for(int j = 0; j < nQueens; j++){
if(j == columnVal[i])
s.append("Q ");
else
s.append("+ ");
}
unit[i] = s.toString();
//System.out.println(unit[i]);
}
//System.out.println();
res.add(unit);
// System.out.println(Arrays.toString(res));
//return;
}
else{
for(int i = 0; i < nQueens; i++){
columnVal[row] = i;//(row,columnVal[row)==>(row,i)
if(isValid(row,columnVal))
DFS_helper(nQueens, res, row+1, columnVal);
}
}
}
public boolean isValid(int row, int [] columnVal){
for(int i = 0; i < row; i++){
if(columnVal[row] == columnVal[i]
||Math.abs(columnVal[row]-columnVal[i]) == row-i)
return false;
}
return true;
}
public static void main(String[] args) {
Solution su = new Solution();
int n = 4;
System.out.println(su.solveNQueens(n));
}
}
While you do System.out.println(su.solveNQueens(n)); it prints list contents which are array and it just prints array object, but not it's contents. so, to print array contents you need to iterate through them.
Arrays.toString Returns a string representation of the contents of the specified array.
https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(java.lang.Object[])
get result into a list and then print it:
List<String[]> res = su.solveNQueens(n);
for(String strs[] : res) {
System.out.println(Arrays.toString(strs));
}
You can do like this too as the above prints with ,. Here iterating through the list and then array.
List<String[]> res = su.solveNQueens(n);
for(String strs[] : res) {
for(String s: strs) {
System.out.print(s+ " ");
}
System.out.println();
}
in java 8, you can make use of stream and lambda:
ArrayList<String[]> result = su.solveNQueens(n)
result.stream().forEach(i->System.out.println(Arrays.toString(i)));

Array.sort not sorting all element [duplicate]

I have String[] array like
{"3","2","4","10","11","6","5","8","9","7"}
I want to sort it in numerical order, not in alphabetical order.
If I use
Arrays.sort(myarray);
I obtain
{"10","11","2","3","4","5","6","7","8","9"}
instead of
{"2","3","4","5","6","7","8","9","10","11"}
Try a custom Comparator, like this:
Arrays.sort(myarray, new Comparator<String>() {
#Override
public int compare(String o1, String o2) {
return Integer.valueOf(o1).compareTo(Integer.valueOf(o2));
}
});
Hope you like it!
I think by far the easiest and most efficient way it to convert the Strings to ints:
int[] myIntArray = new int[myarray.length];
for (int i = 0; i < myarray.length; i++) {
myIntArray[i] = Integer.parseInt(myarray[i]);
}
And then sort the integer array. If you really need to, you can always convert back afterwards:
for (int i = 0; i < myIntArray.length; i++) {
myarray[i] = "" + myIntArray[i];
}
An alternative method would be to use the Comparator interface to dictate exactly how elements are compared, but that would probably amount to converting each String value to an int anyway - making the above approach much more efficient.
I found this article about sorting strings by numeric sorting also for strings that may or may not contain numbers:
The Alphanum Algorithm
There is a Java implementation example linked from the article.
With that class you should be able to sort your arrays numerically like this:
Arrays.sort(myarray, new AlphanumComparator());
U can use sol-1 if it contains only numbers in string format.
Solution-1: -
String []arr = {"3","2","4","10","11","6","5","8","9","7"};
Set<Integer> set = new TreeSet<Integer>();
Arrays.sort(arr);
for(String s:arr){
System.out.print(s+" ");
set.add(Integer.parseInt(s));
}
System.out.println(set);
Integer i = new Integer("4f");
System.out.println(i);
Solution-2:-
String []arr = {"3","2","4","10","11","6","5","8","9","7","jgj","ek"};
Set<Integer> intSet = new TreeSet<Integer>();
Set<String> strSet = new TreeSet<String>();
Arrays.sort(arr);
for(String s:arr){
try {
int i = Integer.parseInt(s);
intSet.add(i);
} catch (NumberFormatException e) {
strSet.add(s);
}
}
List<String> result = new ArrayList<String>();
for(int val:intSet){
result.add(val+"");
}
result.addAll(strSet);
System.out.println(result);
}
Solution-3:-
Write one CustomComparator class and pass it to the sort() method.
public class CustomComparator implements Comparator<String>{
#Override
public int compare(String s1, String s2) {
Integer i1=null;
Integer i2=null;
try {
i1 = Integer.parseInt(s1);
} catch (NumberFormatException e) {
}
try {
i2 = Integer.parseInt(s2);
} catch (NumberFormatException e) {
}
if(i1!=null && i2!=null){
return i1.compareTo(i2);
}else{
return s1.compareTo(s2);
}
}
}
public static void main(){
String []arr = {"3","2","4","10","11","6","5","8","9","7","jgj","ek"};
Arrays.sort(arr, new CustomComparator());
for(String s:arr){
System.out.print(s+" ");
}
}
If all elements if your String array represent numbers, and if the numbers are always positive, then there is a simple way to sort numerically without a limit to the value of the number.
This is based on the fact that a number with a larger number of digits is, in that case, always higher than a number with a smaller number of digits.
You first compare the number of digits, and then (only if the number of digits is the same) you compare the value alphabetically:
Arrays.sort(array,
Comparator.comparing(String::length).thenComparing(Function.identity()));
in jdk8, you can write this code with lambda.
List<String> list = Arrays.asList("3", "2", "4", "10", "11", "6", "5", "8", "9", "7");
list.sort(Comparator.comparingInt(Integer::valueOf));
list.forEach(System.out::println);
especially such as input
String[]{"3.b", "2.c", "4.d", "10.u", "11.a", "6.p", "5.i", "8.t", "9.e", "7.i"}
you can use string.subString to chose which value is you really want to sort.
like
files.sort(Comparator.comparingInt(a -> Integer.valueOf(a.substring(0, a.indexOf(".")))));
Your desired output contains the numerical order of corresponding integers of your strings. So simply you cannot avoid conversion of strings to integers. As an alternative comparator to vikingsteve's you can use this:
Arrays.sort(array, new Comparator<String>() {
#Override
public int compare(String str1, String str2) {
return Integer.parseInt(str1) - Integer.parseInt(str2);
}
});
public class test1 {
public static void main(String[] args)
{
String[] str = {"3","2","4","10","11","6","5","8","9","7"};
int[] a = new int[str.length];
for(int i=0;i<a.length;i++)
{
a[i]=Integer.parseInt(str[i]);
}
Arrays.sort(a);
for(int i=0;i<a.length;i++)
{
str[i]=String.valueOf(a[i]);
}
}
}
This is the best solution I could come with, we can't convert to integer when the string is huge, this method sort an array of strings
public static void sortListOfStringsAsNumbers(List<String> unsorted) {
String min = "";
for (int i = 0; i < unsorted.size(); i++){
min = unsorted.get(i);
int indexMin = i;
for (int j = i + 1; j < unsorted.size(); j++){
if (unsorted.get(j).length() < min.length()){
min = unsorted.get(j);
indexMin = j;
}else if (unsorted.get(j).length() == min.length()){
for (int x = 0; x < unsorted.get(j).length(); x ++){
if (unsorted.get(j).charAt(x) < min.charAt(x)){
min = unsorted.get(j);
indexMin = j;
}else if (unsorted.get(j).charAt(x) > min.charAt(x)){
break;
}
}
}
}
if (indexMin != i){
String temp = unsorted.get(i);
unsorted.set(i, min);
unsorted.set(indexMin, temp);
}
}
}
All the solutions are for only integer numbers. what if the array contains floating numbers as well?
Here is the best solution which allows you to sort any type of value in the string.
import java.math.BigDecimal;
import java.util.*;
class Sort
{
public static void main(String...arg)
{
String s[]={"-100","50","0","56.6","90","0.12",".12","02.34","000.000"};
System.out.println("\n"+Arrays.toString(s)); //print before sorting
Arrays.sort(s, new Comparator<String>() {
public int compare(String a1, String a2) {
BigDecimal a = new BigDecimal(a1);
BigDecimal b = new BigDecimal(a2);
return a.compareTo(b);
}
});
System.out.println("\n"+Arrays.toString(s)); //print after sorting
}
}

Remove duplicates in 2d array

I want to remove duplicate row in a 2d array . i tried the below code .but it is not working . please help me .
Input :
1,ram,mech
1,ram,mech
2,gopi,csc
2.gopi,civil
output should be :
1,ram,mech
2,gopi,csc
2.gopi,civil
Code :
package employee_dup;
import java.util.*;
public class Employee_dup {
public static void main(String[] args)
{
boolean Switch = true;
System.out.println("Name ID Dept ");
String[][] employee_t = {{"1","ram","Mech"},{"1","siva","Mech"},{"1","gopi","Mech"},{"4","jenkat","Mech"},{"5","linda","Mech"},{"1","velu","Mech"}};
int g = employee_t[0].length;
String[][] array2 = new String[10][g];
int rows = employee_t.length;
Arrays.sort(employee_t, new sort(0));
for(int i=0;i<employee_t.length;i++){
for(int j=0;j<employee_t[0].length;j++){
System.out.print(employee_t[i][j]+" ");
}
System.out.println();
}
List<String[]> l = new ArrayList<String[]>(Arrays.asList(employee_t));
for(int k = 0 ;k < employee_t.length-1;k++)
{
if(employee_t[k][0] == employee_t[k+1][0])
{
System.out.println("same value is present");
l.remove(1);
array2 = l.toArray(new String[][]{});
}
}
System.out.println("Name ID Dept ");
for(int i=0;i<array2.length;i++){
for(int j=0;j<array2[0].length;j++){
System.out.print(array2[i][j]+" ");
}
System.out.println();
}
}
}
class sort implements Comparator {
int j;
sort(int columnToSort) {
this.j = columnToSort;
}
//overriding compare method
public int compare(Object o1, Object o2) {
String[] row1 = (String[]) o1;
String[] row2 = (String[]) o2;
//compare the columns to sort
return row1[j].compareTo(row2[j]);
}
}
First I sorted the array based on column one ,then tried to remove duplicates by checking the first column elements and seconds column elements but it is not removing the required column but remove other columns.
You may give this solution a try:
public static void main(String[] args) {
String[][] employee_t = {
{"1","ram","Mech"},
{"1","ram","Mech"},
{"1","siva","Mech"},
{"1","siva","Mech"},
{"1","gopi","Mech"},
{"1","gopi","Mech"} };
System.out.println("ID Name Dept");
Arrays.stream(employee_t)
.map(Arrays::asList)
.distinct()
.forEach(row -> System.out.printf("%-3s%-7s%s\n", row.get(0), row.get(1), row.get(2)));
}
Output
ID Name Dept
1 ram Mech
1 siva Mech
1 gopi Mech
How it works: comparing arrays does rely on instance equality and not on comparing contained elements by equals. Hence converting each row of your 2D array into a List will enable you to compare lists, which takes equals of the elements contained into account.
The Java Stream API does provide a method distinct which relies on equals and will remove all duplicates for you.
Based on your code. Maybe it is not the BEST solution but it works.
public static void main(String[] args) {
System.out.println("Name ID Dept ");
// I added duplicated rows
String[][] inputArray = {
{ "1", "ram", "Mech" },
{ "1", "siva", "Mech" },
{ "1", "gopi", "Mech" },
{ "1", "gopi", "Mech" },
{ "4", "jenkat", "Mech" },
{ "5", "linda", "Mech" },
{ "1", "velu", "Mech" },
{ "1", "velu", "Mech" }
};
// I will add all rows in a Set as it doesn't store duplicate values
Set<String> solutionSet = new LinkedHashSet<String>();
// I get all rows, create a string and insert into Set
for (int i = 0 ; i < inputArray.length ; i++) {
String input = inputArray[i][0]+","+inputArray[i][1]+","+inputArray[i][2];
solutionSet.add(input);
}
// You know the final size of the output array
String[][] outputArray = new String[solutionSet.size()][3];
// I get the results without duplicated values and reconvert it to your format
int position = 0;
for(String solution : solutionSet) {
String[] solutionArray = solution.split(",");
outputArray[position][0] = solutionArray[0];
outputArray[position][1] = solutionArray[1];
outputArray[position][2] = solutionArray[2];
position++;
}
System.out.println("Name ID Dept ");
for (int i = 0; i < outputArray.length; i++) {
for (int j = 0; j < outputArray[0].length; j++) {
System.out.print(outputArray[i][j] + " ");
}
System.out.println();
}
}
I have posted what I think is a readable and easy to maintain solution.
I decided to use distinct from Stream which is part of Java 8
Returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream. - https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#distinct--
Main.class
class Main {
public static void main(String[] args)
{
//Create a list of Employee objects
List<Employee> employeeList = new ArrayList<Employee>();
Employee e1 = new Employee(1, "ram", "mech");
Employee e2 = new Employee(1, "ram", "mech");
Employee e3 = new Employee(2, "gopi", "csc");
Employee e4 = new Employee(2, "gopi", "civil");
employeeList.add(e1);
employeeList.add(e2);
employeeList.add(e3);
employeeList.add(e4);
System.out.println("Before removing duplicates");
employeeList.stream().forEach(System.out::println);
//This is where all the magic happens.
employeeList = employeeList.stream().distinct().collect(Collectors.toList());
System.out.println("\nAfter removing duplicates");
employeeList.stream().forEach(System.out::println);
}
}
Output:
Before removing duplicates
Employee [valA=1, valB=ram, valC=mech]
Employee [valA=1, valB=ram, valC=mech]
Employee [valA=2, valB=gopi, valC=csc]
Employee [valA=2, valB=gopi, valC=civil]
After removing duplicates
Employee [valA=1, valB=ram, valC=mech]
Employee [valA=2, valB=gopi, valC=csc]
Employee [valA=2, valB=gopi, valC=civil]
Employee.class
//This is just a regular POJO class.
class Employee {
int valA;
String valB, valC;
public Employee(int valA, String valB, String valC){
this.valA = valA;
this.valB = valB;
this.valC = valC;
}
public Employee(Employee e) {
this.valA = e.valA;
this.valB = e.valB;
this.valC = e.valC;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + valA;
result = prime * result + ((valB == null) ? 0 : valB.hashCode());
result = prime * result + ((valC == null) ? 0 : valC.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if(obj instanceof Employee && ((Employee)obj).hashCode() == this.hashCode()){
return true;
}
return false;
}
#Override
public String toString() {
return "Employee [valA=" + valA + ", valB=" + valB + ", valC=" + valC + "]";
}
}
Pre Java - 8 solution. May not be the best way. But a quick solution which works..
String[][] records = {
{"1","ram","Mech"},
{"1","ram","Mech"},
{"1","gopi","csc"},
{"1","gopi","civil"} };
List<String[]> distinctRecordsList = new ArrayList<String[]>();
for(String[] record : records){
if(distinctRecordsList.size()>0){
boolean sameValue = false;
for(String[] distinctRecord : distinctRecordsList){
int distinctRecordFields = distinctRecord.length;
if(record.length==distinctRecordFields){
for(int k=0;k<distinctRecordFields;k++){
sameValue = record[k].equalsIgnoreCase(distinctRecord[k]);
if(!sameValue)
break;
}
}else
throw new Exception("Can't compare the records");
}
if(!sameValue)
distinctRecordsList.add(record);
}else if(distinctRecordsList.size()==0)
distinctRecordsList.add(record);
}
Object[] distRecObjects = distinctRecordsList.toArray();
String[][] distinctRecordsArray = new String[distRecObjects.length][];
int i=0;
for(Object distRecObject : distRecObjects){
distinctRecordsArray[i] = (String[]) distRecObject;
i++;
}
Contrary to some other answers I will try to explain what went wrong in your own code and how to fix it within your code (I agree very much with kkflf that an Employee class would be a huge benefit: it’s more object-oriented and it will help structure the code and give better overview of it).
The issues I see in your code are:
You are not removing the correct element when you detect a duplicate, but always the element at index 1 (the second element since indices count from 0). This isn’t trivial, though, because indices shift as you remove elements. The trick is to iterate backward so only indices that you are finished with shift when you remove an element.
You are using == to compare the first element of the subarrays you are comparing. If you wanted to compare just the first element, you should use equals() for comparison. However, I believe you want to compare the entire row so 2,gopi,csc and 2.gopi,civil are recognized as different and both preserved. Arrays.equals() can do the job.
You need to create array2 only after the loop. As your code stands, if no duplicates are detected, arrays2 is never created.
So your loop becomes:
for (int k = employee_t.length - 1; k >= 1; k--)
{
if (Arrays.equals(employee_t[k], employee_t[k - 1]))
{
System.out.println("same value is present");
l.remove(k);
}
}
array2 = l.toArray(new String[][]{});
This gives you the output you asked for.
Further tips:
Your comparator only compares one field in the inner arrays, which is not enough to guarantee that identical rows come right after each other in the sorted array. You should compare all elements, and also require that the inner arrays have the same length.
Use generics: class Sort extends Comparator<String[]>, and you won’t need the casts in compare()
According to Java naming conventions it should be class EmployeeDup, boolean doSwitch (since switch is a reserved word) and class Sort.
You are not using the variables Switch and rows; delete them.
I have wrote a solution for me. This may not be the best but it works.
public static String[][] removeDuplicate(String[][] matrix) {
String[][] newMatrix = new String[matrix.length][matrix[0].length];
int newMatrixRow = 1;
for (int i = 0; i < matrix[0].length; i++)
newMatrix[0][i] = matrix[0][i];
for (int j = 1; j < matrix.length; j++) {
List<Boolean> list = new ArrayList<>();
for (int i = 0; newMatrix[i][0] != null; i++) {
boolean same = true;
for (int col = 2; col < matrix[j].length; col++) {
if (!newMatrix[i][col].equals(matrix[j][col])) {
same = false;
break;
}
}
list.add(same);
}
if (!list.contains(true)) {
for (int i = 0; i < matrix[j].length; i++) {
newMatrix[newMatrixRow][i] = matrix[j][i];
}
newMatrixRow++;
}
}
int i;
for(i = 0; newMatrix[i][0] != null; i++);
String finalMatrix[][] = new String[i][newMatrix[0].length];
for (i = 0; i < finalMatrix.length; i++) {
for (int j = 0; j < finalMatrix[i].length; j++)
finalMatrix[i][j] = newMatrix[i][j];
}
return finalMatrix;
}
This method will return a matrix without any duplicate rows.

Getting data from a given String separated by (,,-) in java

I am having a String like this "5006,3030,8080-8083".
I want each element separately from the String:
5006
3030
8080
8081
8082
8083
Here's my code:
int i=0,j=0;
String delim = "[,]";
String hyphon = "[-]";
String example = "5006,3030,8080-8083";
String p[] = example.split(delim);
int len = p.length;
for(i=0;i<len;i++) {
String ps[]=p[i].split(hyphon);
if(ps.length>1) {
int start = Integer.parseInt(ps[0]);
int finish = Integer.parseInt(ps[1]);
int diff = finish-start+1;
for(j=0;j<diff;j++) {
System.out.println(start+j);
}
} else if(ps.length==1) {
System.out.println(ps[0]);
}
}
Is there any better solution or any class that simplifies my code?
I also want the numbers in a ascending order.
Try this code :
public static void main(String[] args) {
String input = "5006,3030,8080-8083";
List<Integer> list = new ArrayList<Integer>();
String[] numbers = input.split(",");
for (String s : numbers) {
if (s.contains("-")) {
String[] range = s.split("-");
int from = Integer.parseInt(range[0]);
int to = Integer.parseInt(range[1]);
for (int i = from; i <= to; i++) {
list.add(i);
}
}
else {
list.add(Integer.parseInt(s));
}
}
System.out.println("in asc order");
Collections.sort(list);
System.out.println(list.toString());
System.out.println("in desc order");
Collections.reverse(list);
System.out.println(list.toString());
}
My output :
in asc order
[3030, 5006, 8080, 8081, 8082, 8083]
in desc order
[8083, 8082, 8081, 8080, 5006, 3030]
I also want the numbers in a ascending order.
This adds an unexpected twist to your whole program, because once you realize that printing-as-you-go no longer works, you need to start almost from scratch.
The first thing to do is picking an appropriate representation. It appears that you represent ranges of integers, so start by defining a class for them:
class IntRange : Comparable<IntRange> {
private int low, high;
public int getLow() {return low;}
public int getHigh() {return high;}
public IntRange(int low, int high) {
// Add range check to see if low <= high
this.low = low; this.high = high;
}
public IntRange(int point) {low = high = point;}
#Override
public void print() {
for (int i = low ; i <= high ; i++) {
System.out.println(i);
}
}
#Override
public int compareTo(IntRange other) {
...
}
}
Now you can use your code to split on [,], then split on [-], construct IntRange, and put it into an ArrayList<IntRange>. After that you can use sort() method to sort the ranges, and print them in the desired order.
But wait, there is more to your problem than meets the eye. Think what would happen for input like this:
1,5,3-7,6
Where should 5 and 6 be printed? It is not good to print it before or after 3-7, so the trick is to remove points inside ranges.
But even that's not all: what do you do about this input?
1-5,3-7
You should print numbers 1 through 7, inclusive, but this would require merging two ranges. There is a good data structure for doing this efficiently. It is called a range tree. If your input is expected to be large, you should consider using range tree representation.
You are good to go; you can minimize the counter variables using enhanced for loop and while loop.
String example = "5006,3030,8080-8083";
String[] parts=example.split(",")
ArrayList<Integer> numbers = new ArrayList<Integer>();
for(String part: parts)
{
if(part.contains("-"))
{
String subParts[]=part.split("-");
int start = Integer.parseInt(subParts[0]);
int finish = Integer.parseInt(subParts[1]);
while(start <= finish)
{
numbers.add(start);
System.out.println(start++);
}
}
else {
System.out.println(part);
numbers.add(Integer.parseInt(part));
}
}
Integer[] sortedNumbers = new Integer[numbers.size()];
sortedNumbers = Arrays.sort(numbers.toArray(sortedNumbers));
Update (from comment):
Numbers are sorted now.
Try this
String str = "5006,3030,8080-8083";
String[] array = str.split(",");
String ans = "";
for(int i = 0; i < array.length; i++){
if(array[i].contains("-")){
String[] array2 = array[i].split("-");
int start = Integer.parseInt(array2[0]);
int end = Integer.parseInt(array2[array2.length - 1]);
for(int j = start; j <= end; j++){
ans = ans + j + ",";
}
}
else{
ans = ans + array[i] + ",";
}
}
System.out.print(ans);
This code assumes all integers are positive.
public static void main(String[] args) {
String testValue="5006,3030,8080-8083";
Integer[]result=parseElements(testValue);
for (Integer i:result){
System.out.println(i);
}
}
/**
* NumberList is a string of comma-separated elements that are either integers, or a range of integers of the form a-b.
* #param numberList
* #return all the integers in the list, and in ranges in the list, in a sorted list
*/
private static Integer[] parseElements(String integerList) {
ArrayList<Integer> integers=new ArrayList<Integer>();
String[] csvs=integerList.split(",");
for(String csv : csvs){
if(csv.contains("-")){
String[] range=csv.split("-");
Integer left=Integer.decode(range[0]);
Integer right=Integer.decode(range[1]);
for(Integer i=left;i<=right;i++){
integers.add(i);
}
} else {
integers.add(Integer.decode(csv));
}
}
Collections.sort(integers);
return integers.toArray(new Integer[0]);
}
Using Guava's functional idioms you can achive this declaratively, avoiding the verbose, imperative for-loops. First declare a tokenizing function which converts each token in the comma-delimited string into an Iterable<Integer>:
private static final Function<String, Iterable<Integer>> TOKENIZER =
new Function<String, Iterable<Integer>>() {
/**
* Converts each token (e.g. "5006" or "8060-8083") in the input string
* into an Iterable<Integer>; either a ContiguousSet or a List with a
* single element
*/
#Override
public Iterable<Integer> apply(String token) {
if (token.contains("-")) {
String[] range = token.trim().split("-");
return ContiguousSet.create(
Range.closed(Integer.parseInt(range[0]),
Integer.parseInt(range[1])),
DiscreteDomain.integers());
} else {
return Arrays.asList(Integer.parseInt(token.trim()));
}
}
};
then apply the function to the input:
String input = "5006,3030,8080-8083";
Iterable<String> tokens = Splitter.on(',').trimResults().split(input);
SortedSet<Integer> numbers = Sets.newTreeSet();
Iterables.addAll(numbers,
// concat flattens the Iterable<Iterable<Integer>>
// into an Iterable<Integer>
Iterables.concat(Iterables.transform(tokens, TOKENIZER)));
As all of the logic is basically coded into the Function, the client code only needs to tokenize the string into an Iterable<String> (with Splitter), apply the Function through Iterables.transform, flatten the result of the transformation using Iterables.concat and finally add the resulting Iterable<Integer> into a SortedSet<Integer> which keeps the numbers in ascending order.
with java 8 stream api :
public static void main(String[] args) {
String s = "5006,3030,8080-8083";
Arrays.stream(s.split(","))
.flatMap(el -> el.contains("-") ? rangeToStream(el) : Stream.of(Integer.valueOf(el)))
.sorted()
.forEachOrdered(e -> System.out.println(e));
}
private static Stream<? extends Integer> rangeToStream(String el) {
AtomicInteger[] bounds = Arrays.stream(el.split("-")).map(e -> new AtomicInteger(Integer.parseInt(e))).toArray(size -> new AtomicInteger[2]);
return Arrays.stream(new Integer[bounds[1].get() - bounds[0].get() + 1]).map(e -> bounds[0].getAndIncrement());
}
U can code something like this -
String s="5006,3030,8080-8083";
String s2[]=s.split(",");
List<Integer> li= new ArrayList<Integer>();
List<Integer> numbers= new ArrayList<Integer>();
for(int i=0;i<s2.length;i++){
if(s2[i].contains("-")){
li.add(i);
}
else{
numbers.add(Integer.parseInt(s2[i]));
}
}
for(Integer i:li){
String str=s2[i];
String strArr[]=str.split("-");
for(int j=Integer.parseInt(strArr[0]);j<=Integer.parseInt(strArr[1]);j++){
numbers.add(j);
}
}
Collections.sort(numbers);
for(Integer k:numbers){
System.out.println(k);
}
public static void main(String[] args)
{
String example = "5006,3030,8080-8083";
String[] splitString = example.split(",");
List<Integer> soretedNumbers = new ArrayList<>();
for(String str : splitString)
{
String[] split2 = str.split("-");
if(split2.length == 1)
{
soretedNumbers.add(Integer.parseInt(str));
}
else
{
int num1 = Integer.parseInt(split2[0]);
int num2 = Integer.parseInt(split2[1]);
for(int i = num1;i <= num2; i++)
{
soretedNumbers.add(i);
}
}
}
Collections.sort(soretedNumbers);
for(int i : soretedNumbers)
{
System.out.println(i);
}
}

Sort a String array, whose strings represent int

I have String[] array like
{"3","2","4","10","11","6","5","8","9","7"}
I want to sort it in numerical order, not in alphabetical order.
If I use
Arrays.sort(myarray);
I obtain
{"10","11","2","3","4","5","6","7","8","9"}
instead of
{"2","3","4","5","6","7","8","9","10","11"}
Try a custom Comparator, like this:
Arrays.sort(myarray, new Comparator<String>() {
#Override
public int compare(String o1, String o2) {
return Integer.valueOf(o1).compareTo(Integer.valueOf(o2));
}
});
Hope you like it!
I think by far the easiest and most efficient way it to convert the Strings to ints:
int[] myIntArray = new int[myarray.length];
for (int i = 0; i < myarray.length; i++) {
myIntArray[i] = Integer.parseInt(myarray[i]);
}
And then sort the integer array. If you really need to, you can always convert back afterwards:
for (int i = 0; i < myIntArray.length; i++) {
myarray[i] = "" + myIntArray[i];
}
An alternative method would be to use the Comparator interface to dictate exactly how elements are compared, but that would probably amount to converting each String value to an int anyway - making the above approach much more efficient.
I found this article about sorting strings by numeric sorting also for strings that may or may not contain numbers:
The Alphanum Algorithm
There is a Java implementation example linked from the article.
With that class you should be able to sort your arrays numerically like this:
Arrays.sort(myarray, new AlphanumComparator());
U can use sol-1 if it contains only numbers in string format.
Solution-1: -
String []arr = {"3","2","4","10","11","6","5","8","9","7"};
Set<Integer> set = new TreeSet<Integer>();
Arrays.sort(arr);
for(String s:arr){
System.out.print(s+" ");
set.add(Integer.parseInt(s));
}
System.out.println(set);
Integer i = new Integer("4f");
System.out.println(i);
Solution-2:-
String []arr = {"3","2","4","10","11","6","5","8","9","7","jgj","ek"};
Set<Integer> intSet = new TreeSet<Integer>();
Set<String> strSet = new TreeSet<String>();
Arrays.sort(arr);
for(String s:arr){
try {
int i = Integer.parseInt(s);
intSet.add(i);
} catch (NumberFormatException e) {
strSet.add(s);
}
}
List<String> result = new ArrayList<String>();
for(int val:intSet){
result.add(val+"");
}
result.addAll(strSet);
System.out.println(result);
}
Solution-3:-
Write one CustomComparator class and pass it to the sort() method.
public class CustomComparator implements Comparator<String>{
#Override
public int compare(String s1, String s2) {
Integer i1=null;
Integer i2=null;
try {
i1 = Integer.parseInt(s1);
} catch (NumberFormatException e) {
}
try {
i2 = Integer.parseInt(s2);
} catch (NumberFormatException e) {
}
if(i1!=null && i2!=null){
return i1.compareTo(i2);
}else{
return s1.compareTo(s2);
}
}
}
public static void main(){
String []arr = {"3","2","4","10","11","6","5","8","9","7","jgj","ek"};
Arrays.sort(arr, new CustomComparator());
for(String s:arr){
System.out.print(s+" ");
}
}
If all elements if your String array represent numbers, and if the numbers are always positive, then there is a simple way to sort numerically without a limit to the value of the number.
This is based on the fact that a number with a larger number of digits is, in that case, always higher than a number with a smaller number of digits.
You first compare the number of digits, and then (only if the number of digits is the same) you compare the value alphabetically:
Arrays.sort(array,
Comparator.comparing(String::length).thenComparing(Function.identity()));
in jdk8, you can write this code with lambda.
List<String> list = Arrays.asList("3", "2", "4", "10", "11", "6", "5", "8", "9", "7");
list.sort(Comparator.comparingInt(Integer::valueOf));
list.forEach(System.out::println);
especially such as input
String[]{"3.b", "2.c", "4.d", "10.u", "11.a", "6.p", "5.i", "8.t", "9.e", "7.i"}
you can use string.subString to chose which value is you really want to sort.
like
files.sort(Comparator.comparingInt(a -> Integer.valueOf(a.substring(0, a.indexOf(".")))));
Your desired output contains the numerical order of corresponding integers of your strings. So simply you cannot avoid conversion of strings to integers. As an alternative comparator to vikingsteve's you can use this:
Arrays.sort(array, new Comparator<String>() {
#Override
public int compare(String str1, String str2) {
return Integer.parseInt(str1) - Integer.parseInt(str2);
}
});
public class test1 {
public static void main(String[] args)
{
String[] str = {"3","2","4","10","11","6","5","8","9","7"};
int[] a = new int[str.length];
for(int i=0;i<a.length;i++)
{
a[i]=Integer.parseInt(str[i]);
}
Arrays.sort(a);
for(int i=0;i<a.length;i++)
{
str[i]=String.valueOf(a[i]);
}
}
}
This is the best solution I could come with, we can't convert to integer when the string is huge, this method sort an array of strings
public static void sortListOfStringsAsNumbers(List<String> unsorted) {
String min = "";
for (int i = 0; i < unsorted.size(); i++){
min = unsorted.get(i);
int indexMin = i;
for (int j = i + 1; j < unsorted.size(); j++){
if (unsorted.get(j).length() < min.length()){
min = unsorted.get(j);
indexMin = j;
}else if (unsorted.get(j).length() == min.length()){
for (int x = 0; x < unsorted.get(j).length(); x ++){
if (unsorted.get(j).charAt(x) < min.charAt(x)){
min = unsorted.get(j);
indexMin = j;
}else if (unsorted.get(j).charAt(x) > min.charAt(x)){
break;
}
}
}
}
if (indexMin != i){
String temp = unsorted.get(i);
unsorted.set(i, min);
unsorted.set(indexMin, temp);
}
}
}
All the solutions are for only integer numbers. what if the array contains floating numbers as well?
Here is the best solution which allows you to sort any type of value in the string.
import java.math.BigDecimal;
import java.util.*;
class Sort
{
public static void main(String...arg)
{
String s[]={"-100","50","0","56.6","90","0.12",".12","02.34","000.000"};
System.out.println("\n"+Arrays.toString(s)); //print before sorting
Arrays.sort(s, new Comparator<String>() {
public int compare(String a1, String a2) {
BigDecimal a = new BigDecimal(a1);
BigDecimal b = new BigDecimal(a2);
return a.compareTo(b);
}
});
System.out.println("\n"+Arrays.toString(s)); //print after sorting
}
}

Categories